Class: Mantra::Manifest::ArrayElement

Inherits:
Element
  • Object
show all
Defined in:
lib/mantra/manifest/array_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, #has_equal_name?, #has_name?, #hash_selector?, #initialize, #match_selector?, #merge_conflict_error, #method_missing, #name, #path, #path_exist?, #respond_to_missing?, #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 in the class Mantra::Manifest::Element

Instance Method Details

#childrenObject



56
57
58
# File 'lib/mantra/manifest/array_element.rb', line 56

def children
  self.content
end

#content=(content) ⇒ Object



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

def content=(content)
  @content = content.map do |item|
    Element.create(item, self)
  end
end

#each(path, &block) ⇒ Object



35
36
37
38
# File 'lib/mantra/manifest/array_element.rb', line 35

def each(path, &block)
  elements = select(path)
  elements.each(&block)
end

#find_children_by_scope(scope) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mantra/manifest/array_element.rb', line 22

def find_children_by_scope(scope)
  return [] unless scope.array?
  self.content.map do |element|
    if scope.match?(element)
      if scope.has_next?
        element.find_children_by_scope(scope.next)
      else
        element
      end
    end
  end.flatten.compact
end

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



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

def merge(element, options={})
  raise merge_conflict_error(element) unless self.can_merge?(element)
  elements_to_add = element.content.dup
  merge_by_name(elements_to_add, options)
  merge_by_value(elements_to_add, options)
  self.content.concat(elements_to_add)
  self
end

#merge_by_name(elements, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mantra/manifest/array_element.rb', line 60

def merge_by_name(elements, options={})
  self.content.each do |self_element|
    element_with_the_same_name = elements.find do |element_to_add|
      self_element.has_equal_name?(element_to_add)
    end
    unless element_with_the_same_name.nil?
      self_element.merge(element_with_the_same_name, options)
      elements.delete(element_with_the_same_name)
    end
  end
end

#merge_by_value(elements, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mantra/manifest/array_element.rb', line 44

def merge_by_value(elements, options={})
  self.content.each do |self_element|
    element_with_the_same_value = elements.find do |element_to_add|
      self_element.content == element_to_add.content
    end
    unless element_with_the_same_value.nil?
      self_element.merge(element_with_the_same_value, options)
      elements.delete(element_with_the_same_value)
    end
  end
end

#selector_for(element) ⇒ Object



40
41
42
# File 'lib/mantra/manifest/array_element.rb', line 40

def selector_for(element)
  self.content.index(element).to_s
end

#to_ruby_objectObject



72
73
74
# File 'lib/mantra/manifest/array_element.rb', line 72

def to_ruby_object
  self.content.map { |element| element.to_ruby_object }
end

#traverse(&block) ⇒ Object



76
77
78
79
80
# File 'lib/mantra/manifest/array_element.rb', line 76

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