Class: Mantra::Manifest::Element

Inherits:
Object
  • Object
show all
Includes:
Helpers::ObjectWithType, Helpers::RegexpHelper
Defined in:
lib/mantra/manifest/element.rb

Direct Known Subclasses

ArrayElement, HashElement, LeafElement, RootElement

Defined Under Namespace

Classes: MergeConflictError, UnknownScopeError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::RegexpHelper

#to_regexp

Methods included from Helpers::ObjectWithType

included

Constructor Details

#initialize(content, parent) ⇒ Element

Returns a new instance of Element.



23
24
25
26
# File 'lib/mantra/manifest/element.rb', line 23

def initialize(content, parent)
  self.content = content
  self.parent  = parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



124
125
126
127
128
129
130
# File 'lib/mantra/manifest/element.rb', line 124

def method_missing(method_name, *arguments, &block)
  if self.content.respond_to?(method_name)
    self.content.send(method_name, *arguments, &block)
  else
    super
  end
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



11
12
13
# File 'lib/mantra/manifest/element.rb', line 11

def content
  @content
end

#parentObject

Returns the value of attribute parent.



11
12
13
# File 'lib/mantra/manifest/element.rb', line 11

def parent
  @parent
end

Class Method Details

.create(content, parent = nil) ⇒ Object



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

def self.create(content, parent = nil)
  return RootElement.new(content, parent) if parent.nil?
  case content
  when Element then Element.create(content.content, parent)
  when Array   then ArrayElement.new(content, parent)
  when Hash    then HashElement.new(content, parent)
  else LeafElement.new(content, parent)
  end
end

.element_with_selector(selector, value) ⇒ Object

there is a number of limitations on this method: you can not use wildcard characters, for instance jobs you can not use selectors with array filters, such as jobs



155
156
157
158
159
160
161
162
163
164
# File 'lib/mantra/manifest/element.rb', line 155

def self.element_with_selector(selector, value)
  object_to_add = {}
  parts = selector.split(".")
  last_key = parts.pop
  last_node = parts.inject(object_to_add) do |h, part|
    h[part] = {}; h[part]
  end
  last_node[last_key] = value
  Element.create(object_to_add)
end

Instance Method Details

#add_node(selector, value) ⇒ Object



148
149
150
# File 'lib/mantra/manifest/element.rb', line 148

def add_node(selector, value)
  raise "not implemented"
end

#array_selector?(selector) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/mantra/manifest/element.rb', line 174

def array_selector?(selector)
  !!selector.match(/\[(.+\=.+|\d+)?\].*/)
end

#can_merge?(element) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
# File 'lib/mantra/manifest/element.rb', line 112

def can_merge?(element)
  if element.respond_to?(:type)
    self.type == element.type
  else
    self.class == element.class
  end
end

#childrenObject



37
38
39
# File 'lib/mantra/manifest/element.rb', line 37

def children
  raise "not implemented"
end

#delete(selector) ⇒ Object



120
121
122
# File 'lib/mantra/manifest/element.rb', line 120

def delete(selector)
  raise "not implemented"
end

#escape_root(element) ⇒ Object



108
109
110
# File 'lib/mantra/manifest/element.rb', line 108

def escape_root(element)
  element.type == :root ? element.content : element
end

#fetch(scope, &block) ⇒ Object Also known as: select

this method mimics ruby Hash#fetch method



29
30
31
32
33
# File 'lib/mantra/manifest/element.rb', line 29

def fetch(scope, &block)
  current_scope = Scope.parse(scope)
  element = self.root? ? self.content : self
  current_scope.filter(element, &block)
end

#find(string_scope) ⇒ Object



49
50
51
# File 'lib/mantra/manifest/element.rb', line 49

def find(string_scope)
  self.fetch(string_scope).first
end

#get(string_scope) ⇒ Object



53
54
55
56
# File 'lib/mantra/manifest/element.rb', line 53

def get(string_scope)
  result = self.find(string_scope)
  result.nil? ? nil : result.to_ruby_object
end

#has_equal_name?(element) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_equal_name?(element)
  false
end

#has_name?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/mantra/manifest/element.rb', line 58

def has_name?
  false
end

#hash_selector?(selector) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/mantra/manifest/element.rb', line 178

def hash_selector?(selector)
  !!selector.match(/[a-z0-9]+/)
end

#match_selector?(selector) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/mantra/manifest/element.rb', line 140

def match_selector?(selector)
  selector.empty?
end

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

WARNING: merge method “obsorbs” and ruins the element from paramenters TOFIX: make duplicate of element structure that you merge



95
96
97
# File 'lib/mantra/manifest/element.rb', line 95

def merge(element, options={})
  raise "not implemented"
end

#merge_conflict_error(element) ⇒ Object



103
104
105
106
# File 'lib/mantra/manifest/element.rb', line 103

def merge_conflict_error(element)
  object = element.respond_to?(:to_ruby_object) ? element.to_ruby_object : element.inspect
  MergeConflictError.new("merge conflicts: types: #{self.class} against #{element.class}, values:  #{self.to_ruby_object} with #{object}")
end

#nameObject



45
46
47
# File 'lib/mantra/manifest/element.rb', line 45

def name
  raise "unknown name"
end

#pathObject



72
73
74
75
76
77
78
# File 'lib/mantra/manifest/element.rb', line 72

def path
  case self.parent.type
  when :hash  then "#{self.parent.path}#{self.parent.parent.root? ? "" : "."}#{self.selector}"
  when :array then "#{self.parent.path}[#{self.selector}]"
  when :leaf  then raise "leaf can't be parent"
  end
end

#path_exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/mantra/manifest/element.rb', line 144

def path_exist?(path)
  !self.select(path).empty?
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/mantra/manifest/element.rb', line 132

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

#selectorObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mantra/manifest/element.rb', line 80

def selector
  case self.parent.type
  when :array
    self.has_name? ? "name=#{self.name}" : ""
  when :hash
    self.parent.selector_for(self)
  when :root
    ""
  else
    raise "don't know how to build selector"
  end
end

#split_selector(selector, matcher_regex) ⇒ Object

Raises:



166
167
168
169
170
171
172
# File 'lib/mantra/manifest/element.rb', line 166

def split_selector(selector, matcher_regex)
  matcher = selector.match(matcher_regex)
  raise UnknownScopeError.new("Unknown selector: #{selector}") if matcher.nil?
  head_selector = matcher[1]
  tail_selector = matcher[2]
  return head_selector, tail_selector
end

#to_ruby_objectObject



99
100
101
# File 'lib/mantra/manifest/element.rb', line 99

def to_ruby_object
  raise "not implemented"
end

#traverse(&block) ⇒ Object



136
137
138
# File 'lib/mantra/manifest/element.rb', line 136

def traverse(&block)
  raise "not implemented"
end