Class: Perpetuity::Retrieval
- Inherits:
-
Object
- Object
- Perpetuity::Retrieval
- Includes:
- Enumerable
- Defined in:
- lib/perpetuity/retrieval.rb
Instance Attribute Summary collapse
-
#quantity_per_page ⇒ Object
Returns the value of attribute quantity_per_page.
-
#result_limit ⇒ Object
Returns the value of attribute result_limit.
-
#result_page ⇒ Object
Returns the value of attribute result_page.
-
#sort_attribute ⇒ Object
Returns the value of attribute sort_attribute.
-
#sort_direction ⇒ Object
Returns the value of attribute sort_direction.
Instance Method Summary collapse
- #[](index) ⇒ Object
- #count ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(mapper, criteria) ⇒ Retrieval
constructor
A new instance of Retrieval.
- #limit(lim) ⇒ Object
- #options ⇒ Object
- #page(page) ⇒ Object
- #per_page(per) ⇒ Object
- #reverse ⇒ Object
- #sort(attribute = :name) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(mapper, criteria) ⇒ Retrieval
Returns a new instance of Retrieval.
8 9 10 11 12 13 |
# File 'lib/perpetuity/retrieval.rb', line 8 def initialize mapper, criteria @mapper = mapper @class = mapper.mapped_class @criteria = criteria @data_source = mapper.data_source end |
Instance Attribute Details
#quantity_per_page ⇒ Object
Returns the value of attribute quantity_per_page.
6 7 8 |
# File 'lib/perpetuity/retrieval.rb', line 6 def quantity_per_page @quantity_per_page end |
#result_limit ⇒ Object
Returns the value of attribute result_limit.
6 7 8 |
# File 'lib/perpetuity/retrieval.rb', line 6 def result_limit @result_limit end |
#result_page ⇒ Object
Returns the value of attribute result_page.
6 7 8 |
# File 'lib/perpetuity/retrieval.rb', line 6 def result_page @result_page end |
#sort_attribute ⇒ Object
Returns the value of attribute sort_attribute.
6 7 8 |
# File 'lib/perpetuity/retrieval.rb', line 6 def sort_attribute @sort_attribute end |
#sort_direction ⇒ Object
Returns the value of attribute sort_direction.
6 7 8 |
# File 'lib/perpetuity/retrieval.rb', line 6 def sort_direction @sort_direction end |
Instance Method Details
#[](index) ⇒ Object
64 65 66 |
# File 'lib/perpetuity/retrieval.rb', line 64 def [] index to_a[index] end |
#count ⇒ Object
51 52 53 |
# File 'lib/perpetuity/retrieval.rb', line 51 def count @data_source.count(@class, @criteria) end |
#each(&block) ⇒ Object
43 44 45 |
# File 'lib/perpetuity/retrieval.rb', line 43 def each &block to_a.each(&block) end |
#empty? ⇒ Boolean
68 69 70 |
# File 'lib/perpetuity/retrieval.rb', line 68 def empty? to_a.empty? end |
#limit(lim) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/perpetuity/retrieval.rb', line 72 def limit lim retrieval = clone retrieval.result_limit = lim retrieval end |
#options ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/perpetuity/retrieval.rb', line 55 def { attribute: sort_attribute, direction: sort_direction, limit: result_limit || quantity_per_page, page: result_page } end |
#page(page) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/perpetuity/retrieval.rb', line 30 def page page retrieval = clone retrieval.result_page = page retrieval.quantity_per_page = 20 retrieval end |
#per_page(per) ⇒ Object
37 38 39 40 41 |
# File 'lib/perpetuity/retrieval.rb', line 37 def per_page per retrieval = clone retrieval.quantity_per_page = per retrieval end |
#reverse ⇒ Object
23 24 25 26 27 28 |
# File 'lib/perpetuity/retrieval.rb', line 23 def reverse retrieval = clone retrieval.sort_direction = retrieval.sort_direction == :descending ? :ascending : :descending retrieval end |
#sort(attribute = :name) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/perpetuity/retrieval.rb', line 15 def sort attribute=:name retrieval = clone retrieval.sort_attribute = attribute retrieval.sort_direction = :ascending retrieval end |
#to_a ⇒ Object
47 48 49 |
# File 'lib/perpetuity/retrieval.rb', line 47 def to_a @results ||= @data_source.unserialize(@data_source.retrieve(@class, @criteria, ), @mapper) end |