Class: Toller::Retriever
- Inherits:
-
Object
- Object
- Toller::Retriever
- Defined in:
- lib/toller/retriever.rb
Overview
Retriever
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#filter_params ⇒ Object
readonly
Returns the value of attribute filter_params.
-
#retrievals ⇒ Object
readonly
Returns the value of attribute retrievals.
-
#sort_params ⇒ Object
readonly
Returns the value of attribute sort_params.
Class Method Summary collapse
Instance Method Summary collapse
- #filter ⇒ Object
-
#initialize(collection, filter_params, sort_params, retrievals) ⇒ Retriever
constructor
A new instance of Retriever.
Constructor Details
#initialize(collection, filter_params, sort_params, retrievals) ⇒ Retriever
Returns a new instance of Retriever.
14 15 16 17 18 19 |
# File 'lib/toller/retriever.rb', line 14 def initialize(collection, filter_params, sort_params, retrievals) @collection = collection @filter_params = filter_params @sort_params = sort_params @retrievals = retrievals end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
8 9 10 |
# File 'lib/toller/retriever.rb', line 8 def collection @collection end |
#filter_params ⇒ Object (readonly)
Returns the value of attribute filter_params.
8 9 10 |
# File 'lib/toller/retriever.rb', line 8 def filter_params @filter_params end |
#retrievals ⇒ Object (readonly)
Returns the value of attribute retrievals.
8 9 10 |
# File 'lib/toller/retriever.rb', line 8 def retrievals @retrievals end |
#sort_params ⇒ Object (readonly)
Returns the value of attribute sort_params.
8 9 10 |
# File 'lib/toller/retriever.rb', line 8 def sort_params @sort_params end |
Class Method Details
.filter(collection, filter_params, sort_params, retrievals) ⇒ Object
10 11 12 |
# File 'lib/toller/retriever.rb', line 10 def self.filter(collection, filter_params, sort_params, retrievals) new(collection, filter_params, sort_params, retrievals).filter end |
Instance Method Details
#filter ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/toller/retriever.rb', line 21 def filter active_retrievals.reduce(collection) do |items, retrieval| param_value = if retrieval.is_a?(Filter) filter_params.fetch(retrieval.parameter, nil) else sort_params.include?("-#{retrieval.parameter}") ? :desc : :asc end retrieval.apply!(items, param_value) end end |