Class: FilterMatcher::Matcher::FirstFromTopMatcher

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

Overview

matches the result from the top of the filtered collection does not match only when filtered didn’t filtered anything

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFirstFromTopMatcher

Returns a new instance of FirstFromTopMatcher.



65
66
67
# File 'lib/filter_matcher/filter_matcher.rb', line 65

def initialize
  @filters = []
end

Instance Attribute Details

#filtersObject

Returns the value of attribute filters.



63
64
65
# File 'lib/filter_matcher/filter_matcher.rb', line 63

def filters
  @filters
end

Instance Method Details

#run(collection, match) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/filter_matcher/filter_matcher.rb', line 69

def run(collection, match)
  last_filter_index = @filters.size - 1
  collection_init_size = collection.size
  @filters.each_with_index do |filter, index|
    filtered = filter.call(collection)
    collection = filtered.empty? ? collection : filtered

    if (collection.size == 1) ||
      ((last_filter_index == index) && (collection_init_size != collection.size))

      match.call(collection.first)
      break
    end
  end
end