class DataFilter::FilterSet

Represents a collection of data filters that can be called on data. Provides a DSL for creating a filter set and only adding filters the filters that you need.

Attributes

filters[R]

Public Class Methods

new() click to toggle source
# File lib/data_filter/filter_set.rb, line 8
def initialize
  @filters = []
end

Public Instance Methods

add_filter(filter) click to toggle source

Add a filter to the filter set

@param filter [#call]

a callable filter. Can be a proc, lambda, or any object
that responds to #call

@return [FilterSet] the amended filter set

# File lib/data_filter/filter_set.rb, line 18
def add_filter(filter)
  @filters << filter
  self
end
batch(items) click to toggle source

Run the filter set on a collection of data items

@param items [Enumerable<Object>] collection of items that we want to

pass through all of the filters in the filter set

@return [Enumerable<Object>] the filtered results

# File lib/data_filter/filter_set.rb, line 37
def batch(items)
  items.select { |i| filter(i) }
end
filter(item) click to toggle source

Run the filter set on a single data item

@param item [Object] some item that we want to pass through all

of the filters in the filter set

@return [Object, nil] the original item or nil

# File lib/data_filter/filter_set.rb, line 28
def filter(item)
  @filters.reduce(item) { |i, filter| i if filter.call(i) }
end