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, #escape_root, #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



59
60
61
# File 'lib/mantra/manifest/array_element.rb', line 59

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



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

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

#find_children_by_scope(scope) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mantra/manifest/array_element.rb', line 25

def find_children_by_scope(scope)
  return Element.create([]) 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
21
22
23
# File 'lib/mantra/manifest/array_element.rb', line 13

def merge(element, options={})
  element = escape_root(element)
  raise merge_conflict_error(element) unless self.can_merge?(element)
  elements_to_add = element.content.dup
  elements_to_add.each { |e| e.parent = self }
  merge_by_name(elements_to_add, options)
  merge_by_value(elements_to_add, options)
  # keep the element structure consistent
  self.content.concat(elements_to_add)
  self
end

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



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mantra/manifest/array_element.rb', line 63

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



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mantra/manifest/array_element.rb', line 47

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



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

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

#to_ruby_objectObject



75
76
77
# File 'lib/mantra/manifest/array_element.rb', line 75

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

#traverse(&block) ⇒ Object



79
80
81
82
83
# File 'lib/mantra/manifest/array_element.rb', line 79

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