I have a query object in my application that filters data with Array#select method
class QueryObject
def call(filters)
data = get_data # returns array
data = by_param1(data, filter[:param1])
data = by_param2(data, filter[:param2])
end
private
def by_param1(data, filter)
data.select { |d| #filtering goes here }
end
def by_param2(data, filter)
data.select { |d| #filtering goes here }
end
end
How could I pass my filtering blocks to Enumerable to apply all filters and chain them?
I know that I can do something like that:
data.select { |d| by_param1 && by_param2 }
but it's not elegant in my case because I have a lot of filters
You can use #reduce to great effect here.
For example,
In the context of your question, it could go like this:
This assumes that
filtersis passed in as an array of objects that respond to#call. That could be a lambda, a simple ruby class, or even a symbol, as I showed in the example.