Class: CFA::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/cfa/matcher.rb

Overview

Class used to create matcher, that allows to find specific option in augeas tree or subtree TODO: examples of usage

Instance Method Summary collapse

Constructor Details

#initialize(key: nil, collection: nil, value_matcher: nil, &block) ⇒ Matcher

Returns a new instance of Matcher.



7
8
9
10
11
12
13
14
15
# File 'lib/cfa/matcher.rb', line 7

def initialize(key: nil, collection: nil, value_matcher: nil, &block)
  @matcher = lambda do |element|
    return false unless key_match?(element, key)
    return false unless collection_match?(element, collection)
    return false unless value_match?(element, value_matcher)
    return false unless !block || block.call(element[:key], element[:value])
    return true
  end
end

Instance Method Details

#collection_match?(element, collection) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/cfa/matcher.rb', line 27

def collection_match?(element, collection)
  return true unless collection

  element[:key] == (collection + "[]")
end

#key_match?(element, key) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/cfa/matcher.rb', line 21

def key_match?(element, key)
  return true unless key

  element[:key] == key
end

#to_procObject



17
18
19
# File 'lib/cfa/matcher.rb', line 17

def to_proc
  @matcher
end

#value_match?(element, matcher) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
# File 'lib/cfa/matcher.rb', line 33

def value_match?(element, matcher)
  case matcher
  when nil then true
  when Regexp
    return false unless element[:value].is_a?(String)
    matcher =~ element[:value]
  else
    matcher == element[:value]
  end
end