Sha256: 9f1f79c2350a3e4b3fed46832f54c394023c86920a485f333778fe0620e3053c

Contents?: true

Size: 1.94 KB

Versions: 2

Compression:

Stored size: 1.94 KB

Contents

module Flare
  class Collection < ::Array
    attr_reader :total_entries, :total_pages, :current_page, :per_page
    attr_accessor :response

    def initialize(page, per_page, total)
      @current_page, @per_page, @total_entries = page, per_page, total
      
      @total_pages = (@total_entries / @per_page.to_f).ceil
    end
    
    def self.ids_from_response(response, page, per_page, options)
      collection = self.new(page, per_page, response['response']['numFound'] || 0)
      collection.response = response
      collection.replace(response['response']['docs'].map {|doc| doc['id']})
      return collection
    end
    
    def self.create_from_response(response, page, per_page, options)
      # raise response.inspect
      collection = self.new(page, per_page, response['response']['numFound'] || 0)
      collection.response = response      
      collection.replace(instantiate_objects(response, options))
      return collection
    end
    
    def max_pages=(value)
      @total_pages = value if value < @total_pages
    end
    
    def previous_page 
      current_page > 1 ? (current_page - 1) : nil
    end
    
    def next_page
      current_page < total_pages ? (current_page + 1): nil
    end
    
    def offset
      (current_page - 1) * @per_page
    end
    
    private
      def self.instantiate_objects(response, options = {})
        types = Hash.new{|h, k| h[k] = []}
        results = Hash.new{|h, k| h[k] = {}}
        response['response']['docs'].map do |doc|
          type, id = doc['id'].split(':')
          types[type] << id
        end
        
        types.each do |type, ids|
          includes = options[:include]
          type.constantize.find_all_by_id(ids, :include => includes).each do |item|
            results[type][item.id] = item
          end
        end

        response['response']['docs'].map do |doc|
          type, id = doc['id'].split(':')
          results[type][id.to_i]
        end.compact
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flare-1.7.0 lib/flare/collection.rb
flare-1.6.1 lib/flare/collection.rb