Module: RubyCodeAPI::Manipulation::Update

Includes:
Hash, Position, Token, Value
Included in:
RubyCodeAPI::Manipulation
Defined in:
lib/manipulation/update/api.rb,
lib/manipulation/update/hash.rb,
lib/manipulation/update/token.rb,
lib/manipulation/update/value.rb,
lib/manipulation/update/position.rb

Defined Under Namespace

Modules: Hash, Position, Token, Value Classes: UpdateError

Instance Method Summary collapse

Methods included from Value

#replace_value

Methods included from Hash

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

Methods included from Token

#matching_string_arg?, #replace_arg_token

Methods included from Position

#position_arg?, #replace_pos_argument, #replace_position_arg

Instance Method Details

#replace(options) ⇒ Object

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

Raises:



30
31
32
33
34
35
36
37
38
# File 'lib/manipulation/update/api.rb', line 30

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

#replace_arg(options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/manipulation/update/api.rb', line 40

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



52
53
54
55
56
57
58
59
# File 'lib/manipulation/update/api.rb', line 52

def replace_argument(options)                                    
  case self.arg
  when Ruby::String                                    
    replace_arg_token(options[:with]) if matching_string_arg?(options[:arg])  
  else
    raise UpdateError, "Invalid replace_argument options: #{options}"          
  end
end

#update(options, &block) ⇒ Object

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



19
20
21
22
23
24
25
26
27
# File 'lib/manipulation/update/api.rb', line 19

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