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

Instance Method Details

#add_receiver_if_necessary(code) ⇒ String

Add receiver to code if necessary.

Examples:


add_receiver_if_necessary("{{message}} {{arguments}}")

if current_node doesn't have a receiver, it returns "{{message}} {{arguments}}"
if current_node has a receiver, it returns "{{receiver}}.{{message}} {{arguments}}"

Parameters:

  • code (String)

    old code

Returns:

  • (String)

    new code



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

#process_with_node(node) { ... } ⇒ Object

Set current_node to node and process.

Parameters:

Yields:

  • process



43
44
45
46
47
# File 'lib/synvert/core/rewriter/helper.rb', line 43

def process_with_node(node)
  self.current_node = node
  yield
  self.current_node = node
end

#process_with_other_node(node) { ... } ⇒ Object

Set current_node properly, process and set current_node back to original current_node.

Parameters:

Yields:

  • process



53
54
55
56
57
58
# File 'lib/synvert/core/rewriter/helper.rb', line 53

def process_with_other_node(node)
  original_node = self.current_node
  self.current_node = node
  yield
  self.current_node = original_node
end

#strip_brackets(code) ⇒ String

Remove leading and trailing brackets.

Examples:


strip_brackets("(1..100)") #=> "1..100"

Parameters:

  • code (String)

    old code

Returns:

  • (String)

    new code



33
34
35
36
37
# File 'lib/synvert/core/rewriter/helper.rb', line 33

def strip_brackets(code)
  code.sub(/^\((.*)\)$/) { $1 }
      .sub(/^\[(.*)\]$/) { $1 }
      .sub(/^{(.*)}$/) { $1 }
end