Module: Prism::HashNodeExt
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Respond key value and source for hash node
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/prism_ext.rb', line 34
def method_missing(method_name, *args, &block)
if method_name.to_s.end_with?('_element')
key = method_name.to_s[0..-9]
return elements.find { |element| element.type == :assoc_node && element.key.to_value.to_s == key }
elsif method_name.to_s.end_with?('_value')
key = method_name.to_s[0..-7]
return elements.find { |element| element.type == :assoc_node && element.key.to_value.to_s == key }&.value
elsif method_name.to_s.end_with?('_source')
key = method_name.to_s[0..-8]
return elements.find { |element| element.type == :assoc_node && element.key.to_value.to_s == key }&.value&.to_source || ''
end
super
end
|
Instance Method Details
#keys ⇒ Object
25
26
27
|
# File 'lib/prism_ext.rb', line 25
def keys
elements.select { |element| element.type == :assoc_node }.map(&:key)
end
|
#respond_to_missing?(method_name, *args) ⇒ Boolean
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/prism_ext.rb', line 49
def respond_to_missing?(method_name, *args)
if method_name.to_s.end_with?('_element')
key = method_name.to_s[0..-9]
return !!elements.find { |element| element.type == :assoc_node && element.key.to_value.to_s == key }
elsif method_name.to_s.end_with?('_value')
key = method_name.to_s[0..-7]
return !!elements.find { |element| element.type == :assoc_node && element.key.to_value.to_s == key }
elsif method_name.to_s.end_with?('_source')
key = method_name.to_s[0..-8]
return !!elements.find { |element| element.type == :assoc_node && element.key.to_value.to_s == key }
end
super
end
|
#values ⇒ Object
29
30
31
|
# File 'lib/prism_ext.rb', line 29
def values
elements.select { |element| element.type == :assoc_node }.map(&:value)
end
|