Class: Sift::Filtrator
- Inherits:
-
Object
- Object
- Sift::Filtrator
- Defined in:
- lib/sift/filtrator.rb
Overview
Filtrator takes a collection, params and a set of filters and applies them to create a new active record collection with those filters applied.
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#sort ⇒ Object
readonly
Returns the value of attribute sort.
Class Method Summary collapse
Instance Method Summary collapse
- #filter ⇒ Object
-
#initialize(collection, params, _sort, filters = []) ⇒ Filtrator
constructor
A new instance of Filtrator.
Constructor Details
#initialize(collection, params, _sort, filters = []) ⇒ Filtrator
Returns a new instance of Filtrator.
12 13 14 15 16 17 |
# File 'lib/sift/filtrator.rb', line 12 def initialize(collection, params, _sort, filters = []) @collection = collection @params = params @filters = filters @sort = params.fetch(:sort, "").split(",") if filters.any? { |filter| filter.is_a?(Sort) } end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
6 7 8 |
# File 'lib/sift/filtrator.rb', line 6 def collection @collection end |
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
6 7 8 |
# File 'lib/sift/filtrator.rb', line 6 def filters @filters end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
6 7 8 |
# File 'lib/sift/filtrator.rb', line 6 def params @params end |
#sort ⇒ Object (readonly)
Returns the value of attribute sort.
6 7 8 |
# File 'lib/sift/filtrator.rb', line 6 def sort @sort end |
Class Method Details
.filter(collection, params, filters, sort = []) ⇒ Object
8 9 10 |
# File 'lib/sift/filtrator.rb', line 8 def self.filter(collection, params, filters, sort = []) new(collection, params, sort, filters).filter end |
Instance Method Details
#filter ⇒ Object
19 20 21 22 23 |
# File 'lib/sift/filtrator.rb', line 19 def filter active_filters.reduce(collection) do |col, filter| apply(col, filter) end end |