Class: Mantra::Manifest::HashElement
- Inherits:
-
Element
- Object
- Element
- Mantra::Manifest::HashElement
show all
- Defined in:
- lib/mantra/manifest/hash_element.rb
Instance Attribute Summary
Attributes inherited from Element
#content, #parent
Instance Method Summary
collapse
Methods inherited from Element
#add_node, #array_selector?, #can_merge?, create, #delete, element_with_selector, #escape_root, #fetch, #find, #get, #hash_selector?, #initialize, #match_selector?, #merge_conflict_error, #path, #path_exist?, #selector, #split_selector
#to_regexp
included
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/mantra/manifest/hash_element.rb', line 57
def method_missing(method_name, *arguments, &block)
if self.content.has_key?(method_name.to_s) && arguments.empty?
self.content[method_name.to_s]
else
super
end
end
|
Instance Method Details
#children ⇒ Object
41
42
43
|
# File 'lib/mantra/manifest/hash_element.rb', line 41
def children
self.content.values
end
|
#content=(content) ⇒ Object
6
7
8
9
10
11
12
|
# File 'lib/mantra/manifest/hash_element.rb', line 6
def content=(content)
@content = content.to_a.inject({}) do |result, pair|
key, value = *pair
result[key] = Element.create(value, self)
result
end
end
|
#find_children_by_scope(scope) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/mantra/manifest/hash_element.rb', line 75
def find_children_by_scope(scope)
return [] unless scope.hash?
self.content.each_pair.map do |pair|
key, value = *pair
if scope.match?(key, value)
if scope.has_next?
element.find_children_by_scope(scope.next)
else
element
end
end
end.flatten.compact
end
|
#has_equal_name?(element) ⇒ Boolean
33
34
35
|
# File 'lib/mantra/manifest/hash_element.rb', line 33
def has_equal_name?(element)
self.has_name? && element.has_name? && self.name == element.name
end
|
#has_name? ⇒ Boolean
37
38
39
|
# File 'lib/mantra/manifest/hash_element.rb', line 37
def has_name?
self.content.has_key?("name")
end
|
#merge(element, options = {}) ⇒ Object
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/mantra/manifest/hash_element.rb', line 14
def merge(element, options={})
element = escape_root(element)
raise merge_conflict_error(element) unless self.can_merge?(element)
element.content.values.each { |v| v.parent = self }
self.content.merge!(element.content) do |_, self_element, element_to_merge|
self_element.merge(element_to_merge, options)
end
self
end
|
#name ⇒ Object
45
46
47
48
|
# File 'lib/mantra/manifest/hash_element.rb', line 45
def name
raise "name should be a value, not node" if !self.content["name"].leaf?
self.content["name"].content || raise("no name for #{self.inspect}")
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
65
66
67
|
# File 'lib/mantra/manifest/hash_element.rb', line 65
def respond_to_missing?(method_name, include_private = false)
self.content.has_key?(method_name.to_s) || super
end
|
#selector_for(element) ⇒ Object
50
51
52
53
54
55
|
# File 'lib/mantra/manifest/hash_element.rb', line 50
def selector_for(element)
self.content.each_pair do |key, value|
return key if value == element
end
raise "can't find key for object: #{element.to_ruby_object.inspect}"
end
|
#to_ruby_object ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/mantra/manifest/hash_element.rb', line 25
def to_ruby_object
self.content.to_a.inject({}) do |result, pair|
key, element = *pair
result[key] = element.to_ruby_object
result
end
end
|
#traverse(&block) ⇒ Object
69
70
71
72
73
|
# File 'lib/mantra/manifest/hash_element.rb', line 69
def traverse(&block)
self.content.each_value do |value|
value.traverse(&block)
end
end
|