Module: RubyCodeAPI::Manipulation::Update::Hash

Included in:
RubyCodeAPI::Manipulation::Update
Defined in:
lib/manipulation/update/hash.rb

Instance Method Summary collapse

Instance Method Details

#hash_arg?(arg) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
# File 'lib/manipulation/update/hash.rb', line 5

def hash_arg?(arg) 
  case arg
  when Hash 
    matching_hash_arg?(arg)
  when Symbol
    matching_symbol_arg?(arg)      
  end
end

#matching_hash_arg?(arg) ⇒ Boolean

Returns:

  • (Boolean)


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

def matching_hash_arg?(arg)
  if self.arg.respond_to? :elements
    if self.arg.elements[0].class == Ruby::Assoc
      key = self.arg.elements[0].key
      value = self.arg.elements[0].value
      arg_key = arg.first[0]
      arg_value = arg.first[1]
      return key.identifier.token.to_sym == arg_key && value.elements[0].token == arg_value
    end
  end
  false
end

#matching_symbol_arg?(arg) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/manipulation/update/hash.rb', line 43

def matching_symbol_arg?(arg)
  if self.arg.respond_to? :elements
    if self.arg.elements[0].class == Ruby::Assoc
      return self.arg.elements[0].key.identifier.token.to_sym == arg
    end
  else
    # remove ':' token from symbol      
    self.arg.ldelim.token = ''
    return self.arg.identifier.token.to_sym == arg       
  end
  false
end

#replace_hash_arg(options) ⇒ Object



14
15
16
17
18
19
# File 'lib/manipulation/update/hash.rb', line 14

def replace_hash_arg(options) 
  src = options[:replace_code]        
  code = Ripper::RubyBuilder.build(src)
  code.set_ldelim(self.arg) 
  self.arg = code
end

#set_ldelim(arg) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/manipulation/update/hash.rb', line 21

def set_ldelim(arg)
  if arg.respond_to? :elements
    self.ldelim = arg.elements[0].key.ldelim
    self.ldelim.token = ''
  else                  
    self.ldelim = arg.ldelim
  end
end