Module: Synvert::Core::Rewriter::Helper
- Included in:
- Instance
- Defined in:
- lib/synvert/core/rewriter/helper.rb
Overview
Rewriter::Helper provides some helper methods to make it easier to write a snippet.
Instance Method Summary collapse
-
#add_arguments_with_parenthesis_if_necessary ⇒ String
Add arguments with parenthesis if necessary.
-
#add_curly_brackets_if_necessary(code) ⇒ String
Add curly brackets to code if necessary.
-
#add_receiver_if_necessary(code) ⇒ String
Add receiver to code if necessary.
-
#strip_brackets(code) ⇒ String
Remove leading and trailing brackets.
Instance Method Details
#add_arguments_with_parenthesis_if_necessary ⇒ String
Add arguments with parenthesis if necessary.
35 36 37 38 39 40 41 |
# File 'lib/synvert/core/rewriter/helper.rb', line 35 def add_arguments_with_parenthesis_if_necessary if node.arguments.size > 0 '({{arguments}})' else '' end end |
#add_curly_brackets_if_necessary(code) ⇒ String
Add curly brackets to code if necessary.
51 52 53 54 55 56 57 |
# File 'lib/synvert/core/rewriter/helper.rb', line 51 def add_curly_brackets_if_necessary(code) if code.start_with?('{') && code.end_with?('}') code else "{ #{code} }" end end |
#add_receiver_if_necessary(code) ⇒ String
Add receiver to code if necessary.
17 18 19 20 21 22 23 |
# File 'lib/synvert/core/rewriter/helper.rb', line 17 def add_receiver_if_necessary(code) if node.receiver "{{receiver}}.#{code}" else code end end |
#strip_brackets(code) ⇒ String
Remove leading and trailing brackets.
67 68 69 70 71 72 73 |
# File 'lib/synvert/core/rewriter/helper.rb', line 67 def strip_brackets(code) code.sub(/^\((.*)\)$/) { Regexp.last_match(1) } .sub(/^\[(.*)\]$/) { Regexp.last_match(1) } .sub(/^{(.*)}$/) { Regexp.last_match(1).strip } end |