module Picky::Convenience

Use this class to extend the hash that the client returns.

Public Instance Methods

allocations() click to toggle source

Returns the allocations.

# File lib/picky-client/convenience.rb, line 27
def allocations
  @allocations ||= self[:allocations]
end
clear_ids() click to toggle source

Removes all ids of each allocation.

# File lib/picky-client/convenience.rb, line 120
def clear_ids
  allocations.each { |allocation| allocation[4].clear }
end
empty?() click to toggle source

Are there any allocations?

# File lib/picky-client/convenience.rb, line 9
def empty?
  allocations.empty?
end
entries(limit = 20) { |ar_or_rendered| ... } click to toggle source

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.

# 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) click to toggle source

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.

# 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 = {}) click to toggle source

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 {}.

# File lib/picky-client/convenience.rb, line 57
def populate_with model_class, options = {}, &block
  the_ids       = ids options.delete(:up_to)
  finder_method = options.delete(:finder_method) || :find_all_by_id
  
  # Call finder method.
  #
  objects = model_class.send finder_method, the_ids, options

  # 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() click to toggle source

Returns the total of results.

# File lib/picky-client/convenience.rb, line 33
def total
  @total ||= self[:total]
end