Class: Mantra::Manifest::Scope
- Inherits:
-
Object
- Object
- Mantra::Manifest::Scope
show all
- Includes:
- Helpers::ObjectWithType, Helpers::RegexpHelper
- Defined in:
- lib/mantra/manifest/scope.rb,
lib/mantra/manifest/scope/hash_scope.rb,
lib/mantra/manifest/scope/array_scope.rb,
lib/mantra/manifest/scope/empty_scope.rb
Defined Under Namespace
Classes: ArrayScope, EmptyScope, HashScope, ScopeParseError
Constant Summary
collapse
- HASH_SELECTOR_REGEXP =
/^([a-zA-Z0-9\_\-\=\*]+)\.?(.*)$/
- ARRAY_SELECTOR_REGEXP =
/^\[([a-zA-Z0-9\:\_\-\=\*]*)\]\.?(.*)$/
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#to_regexp
included
Constructor Details
#initialize(options = {}) ⇒ Scope
Returns a new instance of Scope.
47
48
49
|
# File 'lib/mantra/manifest/scope.rb', line 47
def initialize(options={})
self.scope, self.next = options[:scope], options[:next]
end
|
Instance Attribute Details
#next ⇒ Object
Also known as:
tail
Returns the value of attribute next.
45
46
47
|
# File 'lib/mantra/manifest/scope.rb', line 45
def next
@next
end
|
#scope ⇒ Object
Returns the value of attribute scope.
45
46
47
|
# File 'lib/mantra/manifest/scope.rb', line 45
def scope
@scope
end
|
Class Method Details
.parse(scope_or_string) ⇒ Object
10
11
12
13
|
# File 'lib/mantra/manifest/scope.rb', line 10
def self.parse(scope_or_string)
return scope_or_string if scope_or_string.is_a?(Scope)
parse_selector(scope_or_string)
end
|
.parse_selector(selector) ⇒ Object
.raise_parse_error(selector) ⇒ Object
41
42
43
|
# File 'lib/mantra/manifest/scope.rb', line 41
def self.raise_parse_error(selector)
raise ScopeParseError.new("Can't parse selector: #{selector}")
end
|
.split_selector(selector, matcher_regex) ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/mantra/manifest/scope.rb', line 33
def self.split_selector(selector, matcher_regex)
matcher = selector.match(matcher_regex)
raise_parse_error(selector) if matcher.nil?
head_selector = matcher[1]
tail_selector = matcher[2]
return head_selector, tail_selector
end
|
Instance Method Details
#_filter(element) ⇒ Object
79
80
81
|
# File 'lib/mantra/manifest/scope.rb', line 79
def _filter(element)
raise "not implemented"
end
|
#filter(element, &block) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/mantra/manifest/scope.rb', line 61
def filter(element, &block)
return [] if !has_same_type?(element)
matched_elements = _filter(element)
if self.last?
block.call(matched_elements) if block_given?
matched_elements
else
matched_elements.map do |e|
self.next.filter(e)
end.compact.flatten
end
end
|
#has_same_type?(element) ⇒ Boolean
74
75
76
77
|
# File 'lib/mantra/manifest/scope.rb', line 74
def has_same_type?(element)
raise element.inspect if element.is_a?(Array)
self.type == element.type
end
|
#last? ⇒ Boolean
51
52
53
|
# File 'lib/mantra/manifest/scope.rb', line 51
def last?
self.next.is_a?(EmptyScope)
end
|
#match?(manifest_element) ⇒ Boolean
57
58
59
|
# File 'lib/mantra/manifest/scope.rb', line 57
def match?(manifest_element)
raise "not implemented"
end
|
#to_a ⇒ Object
83
84
85
|
# File 'lib/mantra/manifest/scope.rb', line 83
def to_a
[self, self.tail.to_a].flatten
end
|