Class: Mantra::Manifest::Scope

Inherits:
Object
  • Object
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

Direct Known Subclasses

ArrayScope, EmptyScope, HashScope

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

Methods included from Helpers::RegexpHelper

#to_regexp

Methods included from Helpers::ObjectWithType

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

#nextObject 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

#scopeObject

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mantra/manifest/scope.rb', line 18

def self.parse_selector(selector)
  case selector
  when HASH_SELECTOR_REGEXP
    head_selector, tail_selector = split_selector(selector, HASH_SELECTOR_REGEXP)
    HashScope.new(scope: head_selector, next: parse_selector(tail_selector))
  when ARRAY_SELECTOR_REGEXP
    head_selector, tail_selector = split_selector(selector, ARRAY_SELECTOR_REGEXP)
    ArrayScope.new(scope: head_selector, next: parse_selector(tail_selector))
  when ""
    EmptyScope.new()
  else
    raise_parse_error(selector)
  end
end

.raise_parse_error(selector) ⇒ Object

Raises:



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

Returns:

  • (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

Returns:

  • (Boolean)


51
52
53
# File 'lib/mantra/manifest/scope.rb', line 51

def last?
  self.next.is_a?(EmptyScope)
end

#match?(manifest_element) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/mantra/manifest/scope.rb', line 57

def match?(manifest_element)
  raise "not implemented"
end

#to_aObject



83
84
85
# File 'lib/mantra/manifest/scope.rb', line 83

def to_a
  [self, self.tail.to_a].flatten
end