Class: Toller::Retriever

Inherits:
Object
  • Object
show all
Defined in:
lib/toller/retriever.rb

Overview

Retriever

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#collectionObject (readonly)

Returns the value of attribute collection.



8
9
10
# File 'lib/toller/retriever.rb', line 8

def collection
  @collection
end

#filter_paramsObject (readonly)

Returns the value of attribute filter_params.



8
9
10
# File 'lib/toller/retriever.rb', line 8

def filter_params
  @filter_params
end

#retrievalsObject (readonly)

Returns the value of attribute retrievals.



8
9
10
# File 'lib/toller/retriever.rb', line 8

def retrievals
  @retrievals
end

#sort_paramsObject (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

#filterObject



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