Class: DataFilter::FilterSet

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/data_filter/filter_set.rb

Overview

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.

Defined Under Namespace

Modules: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL

included, #keyword_filter, #like_filter, #method_missing, #prefix_filter, #range_filter, #range_overlap_filter, #respond_to_missing?, #truthy_filter

Constructor Details

#initializeFilterSet

Returns a new instance of FilterSet.



8
9
10
# File 'lib/data_filter/filter_set.rb', line 8

def initialize
  @filters = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class DataFilter::FilterSet::DSL

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



6
7
8
# File 'lib/data_filter/filter_set.rb', line 6

def filters
  @filters
end

Instance Method Details

#add_filter(filter) ⇒ FilterSet

Add a filter to the filter set

Parameters:

  • filter (#call)

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

Returns:



18
19
20
21
# File 'lib/data_filter/filter_set.rb', line 18

def add_filter(filter)
  @filters << filter
  self
end

#batch(items) ⇒ Enumerable<Object>

Run the filter set on a collection of data items

Parameters:

  • items (Enumerable<Object>)

    collection of items that we want to pass through all of the filters in the filter set

Returns:

  • (Enumerable<Object>)

    the filtered results



37
38
39
# File 'lib/data_filter/filter_set.rb', line 37

def batch(items)
  items.select { |i| filter(i) }
end

#filter(item) ⇒ Object?

Run the filter set on a single data item

Parameters:

  • item (Object)

    some item that we want to pass through all of the filters in the filter set

Returns:

  • (Object, nil)

    the original item or nil



28
29
30
# File 'lib/data_filter/filter_set.rb', line 28

def filter(item)
  @filters.reduce(item) { |i, filter| i if filter.call(i) }
end