Module: RubyCodeAPI::Manipulation::Update

Includes:
HashReplacer, PositionReplacer, TokenReplacer, ValueReplacer
Included in:
RubyCodeAPI::Manipulation
Defined in:
lib/manipulation/update/api.rb

Instance Method Summary collapse

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

#replace(options) ⇒ Object

:arg => ‘ripper’, :replace_arg => ‘rapper’

Raises:

  • (Error)


112
113
114
115
116
117
# File 'lib/manipulation/update/api.rb', line 112

def replace(options)                               
  return replace_value(options) if options[:value]
  return replace_position_arg(options) if position_arg?(options[:arg])
  return replace_arg(options) if options[:arg]
  raise Error, "Invalid options: #{options}"
end

#replace_arg(options) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/manipulation/update/api.rb', line 119

def replace_arg(options)
  self.arguments.elements.each_with_index do |elem, i|
    case elem
    when Ruby::Arg
      if elem.hash_arg?(options[:arg])
        return elem.replace_hash_arg(options) 
      end
      elem.replace_argument(options)
    end
  end
end

#replace_argument(options) ⇒ Object



131
132
133
134
135
136
# File 'lib/manipulation/update/api.rb', line 131

def replace_argument(options)                                    
  case self.arg
  when Ruby::String                                    
    replace_arg_token(options[:with]) if matching_string_arg?(options[:arg])  
  end
end

#update(options, &block) ⇒ Object

update :select => …, :with => … update :select => …, :with_code => ‘code’



101
102
103
104
105
106
107
108
109
# File 'lib/manipulation/update/api.rb', line 101

def update(options, &block)                
  s = replace_value(options) if options[:value]
  s = replace options[:select].merge(options) if options[:select]        
  if block_given?
    block.arity < 1 ? s.instance_eval(&block) : block.call(s)      
  else
    s
  end        
end