Module: Picky::Convenience
- Defined in:
- lib/picky-client/convenience.rb
Overview
Use this class to extend the hash that the client returns.
Instance Method Summary collapse
-
#allocations ⇒ Object
Returns the allocations.
-
#amend_ids_with(entries) ⇒ Object
The ids need to come in the order which the ids were returned by the ids method.
-
#clear_ids ⇒ Object
Removes all ids of each allocation.
-
#empty? ⇒ Boolean
Are there any allocations?.
-
#entries(limit = 20) ⇒ Object
Returns either * the rendered entries, if you have used #populate_with with a block OR * the model instances, if you have used #populate_with without a block.
-
#ids(limit = nil) ⇒ Object
Returns the topmost n results.
-
#populate_with(model_class, options = {}, &block) ⇒ Object
Populates the ids with (rendered) model instances.
-
#total ⇒ Object
Returns the total of results.
Instance Method Details
#allocations ⇒ Object
Returns the allocations.
27 28 29 |
# File 'lib/picky-client/convenience.rb', line 27 def allocations @allocations ||= self[:allocations] end |
#amend_ids_with(entries) ⇒ Object
The ids need to come in the order which the ids were returned by the ids method.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/picky-client/convenience.rb', line 107 def amend_ids_with entries # :nodoc: i = 0 allocations.each do |allocation| allocation[5] = allocation[4].map do |_| e = entries[i] i += 1 e end end end |
#clear_ids ⇒ Object
Removes all ids of each allocation.
120 121 122 |
# File 'lib/picky-client/convenience.rb', line 120 def clear_ids allocations.each { |allocation| allocation[4].clear } end |
#empty? ⇒ Boolean
Are there any allocations?
9 10 11 |
# File 'lib/picky-client/convenience.rb', line 9 def empty? allocations.empty? end |
#entries(limit = 20) ⇒ Object
Returns either
-
the rendered entries, if you have used #populate_with with a block
OR
-
the model instances, if you have used #populate_with without a block
Or, if you haven’t called #populate_with yet, you will get an empty array.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/picky-client/convenience.rb', line 94 def entries limit = 20 if block_given? i = 0 allocations.each { |allocation| allocation[5].collect! { |ar_or_rendered| break if i >= limit; i = i + 1; yield ar_or_rendered } } else entries = [] allocations.each { |allocation| allocation[5].each { |ar_or_rendered| break if entries.size >= limit; entries << ar_or_rendered } } entries end end |
#ids(limit = nil) ⇒ Object
Returns the topmost n results. (Note that not all ids are returned with the results. By default only maximally 20.)
Parameters
-
limit: The amount of ids to return. Default is all of them.
19 20 21 22 23 |
# File 'lib/picky-client/convenience.rb', line 19 def ids limit = nil ids = [] allocations.each { |allocation| allocation[4].each { |id| break if limit && ids.size > limit; ids << id } } ids end |
#populate_with(model_class, options = {}, &block) ⇒ Object
Populates the ids with (rendered) model instances.
It does this by calling #find_by_id on the given model class.
Give it eg. an ActiveRecord class and options for the find_by_id and it will yield each found result for you to render.
If you don’t pass it a block, it will just use the AR results.
Note: Usually, after this the ids are not needed anymore.
Use #clear_ids to remove them.
Parameters
-
model_class: The model to use for the results. Will call #find on the given class.
Options
-
up_to: Amount of results to populate. All of them by default.
-
finder_method: Specify which AR finder method you want to load the model with. Default is #find_all_by_id.
-
The rest of the options are directly passed through to the ModelClass.find_by_id(ids, options) method. Default is {}.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/picky-client/convenience.rb', line 57 def populate_with model_class, = {}, &block the_ids = ids .delete(:up_to) finder_method = .delete(:finder_method) || :find_all_by_id # Call finder method. # objects = model_class.send finder_method, the_ids, # Put together a mapping. # mapped_entries = objects.inject({}) do |mapped, entry| mapped[entry.id] = entry if entry mapped end # Preserves the order. # objects = the_ids.map { |id| mapped_entries[id] } # Replace objects with rendered versions if a block is given. # objects.collect! &block if block # Enhance the allocations with the objects or rendered objects. # amend_ids_with objects objects end |
#total ⇒ Object
Returns the total of results.
33 34 35 |
# File 'lib/picky-client/convenience.rb', line 33 def total @total ||= self[:total] end |