Sha256: 3fa88e6f1f08d040f7a2e25fdf1e6ec6d18d258f527930f15fa4cf78b7f9e043

Contents?: true

Size: 1.38 KB

Versions: 12

Compression:

Stored size: 1.38 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))
      return collection
    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)
        response['response']['docs'].map do |doc|
          type, id = doc['id'].split(':')
          type.constantize.find(id)
        end
      end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
flare-1.4.4 lib/flare/collection.rb
flare-1.4.3 lib/flare/collection.rb
flare-1.4.2 lib/flare/collection.rb
flare-1.4.1 lib/flare/collection.rb
flare-1.4.0 lib/flare/collection.rb
flare-1.3.0 lib/flare/collection.rb
flare-1.2.0 lib/flare/collection.rb
flare-1.1.0 lib/flare/collection.rb
flare-1.0.2 lib/flare/collection.rb
flare-1.0.1 lib/flare/collection.rb
flare-1.0.0 lib/flare/collection.rb
flare-0.0.1 lib/flare/collection.rb