Class: ConfigFilesApi::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/config_files_api/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) ⇒ Matcher

Returns a new instance of Matcher.



6
7
8
9
10
11
12
13
# File 'lib/config_files_api/matcher.rb', line 6

def initialize(key: nil, collection: nil, value_matcher: nil)
  @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 true
  end
end

Instance Method Details

#collection_match?(element, collection) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/config_files_api/matcher.rb', line 25

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

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

#key_match?(element, key) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
# File 'lib/config_files_api/matcher.rb', line 19

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

  element[:key] == key
end

#to_procObject



15
16
17
# File 'lib/config_files_api/matcher.rb', line 15

def to_proc
  @matcher
end

#value_match?(element, matcher) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
# File 'lib/config_files_api/matcher.rb', line 31

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