Class: CFA::Matcher
- Inherits:
-
Object
- Object
- CFA::Matcher
- Defined in:
- lib/cfa/matcher.rb
Overview
Note:
The coupling to AugeasTree, AugeasElement is not a goal. Once we have more parsers it will go away.
The Matcher is used as a predicate on AugeasElement.
Being a predicate, it is passed to methods such as Enumerable#select or Array#index, returning a Boolean meaning whether a match was found.
Acting on AugeasElement means it expects a Hash ‘e` containing `e` and `e`.
It is used with the ‘&` syntax which makes the matcher act like a block/lambda/Proc (via #to_proc).
Instance Method Summary collapse
-
#initialize(key: nil, collection: nil, value_matcher: nil) {|blk_key, blk_value| ... } ⇒ Matcher
constructor
The constructor arguments are constraints to match on an element.
- #to_proc ⇒ Proc{AugeasElement=>Boolean}
Constructor Details
#initialize(key: nil, collection: nil, value_matcher: nil) {|blk_key, blk_value| ... } ⇒ Matcher
The constructor arguments are constraints to match on an element. All constraints are optional. All supplied constraints must match, so it is a conjunction.
40 41 42 43 44 45 46 47 48 |
# File 'lib/cfa/matcher.rb', line 40 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 || yield(element[:key], element[:value]) return true end end |
Instance Method Details
#to_proc ⇒ Proc{AugeasElement=>Boolean}
51 52 53 |
# File 'lib/cfa/matcher.rb', line 51 def to_proc @matcher end |