Class: FilterMatcher::Matcher::SingleMatcher
- Inherits:
-
Object
- Object
- FilterMatcher::Matcher::SingleMatcher
- Defined in:
- lib/filter_matcher/filter_matcher.rb
Overview
matches only when filters can narrow collection to one element
Instance Attribute Summary collapse
-
#filters ⇒ Object
Returns the value of attribute filters.
Instance Method Summary collapse
-
#initialize ⇒ SingleMatcher
constructor
A new instance of SingleMatcher.
- #run(collection, match) ⇒ Object
Constructor Details
#initialize ⇒ SingleMatcher
Returns a new instance of SingleMatcher.
43 44 45 |
# File 'lib/filter_matcher/filter_matcher.rb', line 43 def initialize @filters = [] end |
Instance Attribute Details
#filters ⇒ Object
Returns the value of attribute filters.
41 42 43 |
# File 'lib/filter_matcher/filter_matcher.rb', line 41 def filters @filters end |
Instance Method Details
#run(collection, match) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/filter_matcher/filter_matcher.rb', line 47 def run(collection, match) @filters.each do |filter| filtered = filter.call(collection) collection = filtered.empty? ? collection : filtered if collection.size == 1 match.call(collection.first) break end end end |