Class: Mantra::Manifest::Scope::ArrayScope

Inherits:
Mantra::Manifest::Scope show all
Defined in:
lib/mantra/manifest/scope/array_scope.rb

Defined Under Namespace

Classes: UnknownFunction

Constant Summary

Constants inherited from Mantra::Manifest::Scope

ARRAY_SELECTOR_REGEXP, HASH_SELECTOR_REGEXP

Instance Attribute Summary

Attributes inherited from Mantra::Manifest::Scope

#next, #scope

Instance Method Summary collapse

Methods inherited from Mantra::Manifest::Scope

#filter, #has_same_type?, #initialize, #last?, #match?, parse, parse_selector, raise_parse_error, split_selector, #to_a

Methods included from Helpers::RegexpHelper

#to_regexp

Methods included from Helpers::ObjectWithType

included

Constructor Details

This class inherits a constructor from Mantra::Manifest::Scope

Instance Method Details

#_filter(element) ⇒ Object

returns array of children that are



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mantra/manifest/scope/array_scope.rb', line 10

def _filter(element)
  case scope
  when /^\d+$/
    index = scope.to_i
    [element.content[index]]
  when /^.+\=.+$/
    key_wildcard, value_wildcard = scope.split("=")
    key_matcher, value_matcher = to_regexp(key_wildcard), to_regexp(value_wildcard)
    element.content.select { |e| e.hash? }.select do |e|
      e.each_pair.any? do |pair|
        k, v = *pair
        k.match(key_matcher) && v.match(value_matcher)
      end
    end
  when /^\:\:.+$/
    function_name = self.scope[2..-1]
    if !allowed_function_names.include?(function_name)
      raise ScopeParseError.new("Unknown function: #{function_name}")
    end
    self.send(function_name, element)
  when ""
    element.content
  else
    raise ScopeParseError.new("Don't know how to apply scope to array " +
                              "(scope: #{self.scope.inspect}, array: #{element.to_ruby_object.inspect})")
  end
end

#allowed_function_namesObject



38
39
40
# File 'lib/mantra/manifest/scope/array_scope.rb', line 38

def allowed_function_names
  %w(last first)
end

#first(element) ⇒ Object



46
47
48
# File 'lib/mantra/manifest/scope/array_scope.rb', line 46

def first(element)
  [element.content.first]
end

#last(element) ⇒ Object



42
43
44
# File 'lib/mantra/manifest/scope/array_scope.rb', line 42

def last(element)
  [element.content.last]
end