Module: RubyAPI::Mutator

Includes:
Replacer
Included in:
Ruby::Node
Defined in:
lib/mutate/api.rb,
lib/mutate/replacer.rb

Defined Under Namespace

Modules: Replacer

Instance Method Summary collapse

Methods included from Replacer

#replace, #replace_argument

Methods included from ValueReplacer

#replace_value

Methods included from HashReplacer

#hash_arg?, #matching_hash_arg?, #matching_symbol_arg?, #replace_hash_arg, #set_ldelim

Methods included from TokenReplacer

#matching_string_arg?, #replace_arg_token

Methods included from PositionReplacer

#position_arg?, #replace_pos_argument, #replace_position_arg

Instance Method Details

#append_code(code) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/mutate/api.rb', line 15

def append_code(code)
  return append_code_simple(code) if !elemental?
  obj = object
  indentation = obj.last_indent  
  code =  "\n#{code}\n".indent(' ', indentation)
  ruby_code = Ripper::RubyBuilder.build(code)
  inject_code = ruby_code.elements[0]
  obj.get_elements << inject_code
  inject_code
end

#append_code_simple(code) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/mutate/api.rb', line 26

def append_code_simple(code)
  indentation = position.col
  code =  "\n#{code}\n".indent(' ', indentation)
  ruby_code = Ripper::RubyBuilder.build(code)
  inject_code = ruby_code.elements[0]
  index = parent.find_index(self)
  parent.get_elements.insert(index+1, inject_code)
  inject_code
end

#append_comment(text) ⇒ Object



55
56
57
# File 'lib/mutate/api.rb', line 55

def append_comment(text)
  append_code("# #{text}")
end

#elemental?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/mutate/api.rb', line 59

def elemental?
  respond_to?(:body) || respond_to?(:elements)
end

#find_index(obj) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/mutate/api.rb', line 36

def find_index(obj)
  get_elements.each_with_index do |elem, i|
    if elem == obj
      return i
    end
  end        
end

#get_elementsObject



63
64
65
66
67
68
69
70
# File 'lib/mutate/api.rb', line 63

def get_elements
  case self
  when Ruby::Class
    body.elements
  else
    elements
  end
end

#prepend_code(code) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/mutate/api.rb', line 44

def prepend_code(code)
  obj = object
  indentation = obj.first_indent  
  code =  "\n#{code}\n".indent(' ', indentation)
  ruby_code = Ripper::RubyBuilder.build(code)
  inject_code = ruby_code.elements[0]
  obj.get_elements.insert(0, inject_code)
  obj
end