Class: Mantra::Manifest::HashElement

Inherits:
Element
  • Object
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, #fetch, #find, #get, #hash_selector?, #initialize, #match_selector?, #merge_conflict_error, #path, #path_exist?, #selector, #split_selector

Methods included from Helpers::RegexpHelper

#to_regexp

Methods included from Helpers::ObjectWithType

included

Constructor Details

This class inherits a constructor from Mantra::Manifest::Element

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/mantra/manifest/hash_element.rb', line 54

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

#childrenObject



38
39
40
# File 'lib/mantra/manifest/hash_element.rb', line 38

def children
  self.content.values
end

#content=(content) ⇒ Object

content is hash



6
7
8
9
10
11
12
# File 'lib/mantra/manifest/hash_element.rb', line 6

def content=(content) # content is hash
  @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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mantra/manifest/hash_element.rb', line 72

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

Returns:

  • (Boolean)


30
31
32
# File 'lib/mantra/manifest/hash_element.rb', line 30

def has_equal_name?(element)
  self.has_name? && element.has_name? && self.name == element.name
end

#has_name?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/mantra/manifest/hash_element.rb', line 34

def has_name?
  self.content.has_key?("name")
end

#merge(element, options = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/mantra/manifest/hash_element.rb', line 14

def merge(element, options={})
  raise merge_conflict_error(element) unless self.can_merge?(element)
  self.content.merge!(element.content) do |_, self_element, element_to_merge|
    self_element.merge(element_to_merge, options)
  end
  self
end

#nameObject



42
43
44
45
# File 'lib/mantra/manifest/hash_element.rb', line 42

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

Returns:

  • (Boolean)


62
63
64
# File 'lib/mantra/manifest/hash_element.rb', line 62

def respond_to_missing?(method_name, include_private = false)
  self.content.has_key?(method_name.to_s) || super
end

#selector_for(element) ⇒ Object



47
48
49
50
51
52
# File 'lib/mantra/manifest/hash_element.rb', line 47

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_objectObject



22
23
24
25
26
27
28
# File 'lib/mantra/manifest/hash_element.rb', line 22

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



66
67
68
69
70
# File 'lib/mantra/manifest/hash_element.rb', line 66

def traverse(&block)
  self.content.each_value do |value|
    value.traverse(&block)
  end
end