Sha256: ffe70c88b4ad1973eca25e2a8923c2195e6dd06ae7cbd40f07d07877b51347d5

Contents?: true

Size: 1.01 KB

Versions: 10

Compression:

Stored size: 1.01 KB

Contents

module JsonApiClient
  class ResultSet < Array

    attr_accessor :total_pages,
                  :total_entries,
                  :offset,
                  :per_page,
                  :current_page,
                  :errors,
                  :record_class,
                  :meta
    alias_attribute :limit_value, :per_page

    def self.build(klass, data)
      # Objects representing an individual resource are
      # not necessarily wrapped in an Array; enforce wrapping
      result_data = [data.fetch(klass.table_name, [])].flatten
      new(result_data.map {|attributes| klass.new(attributes) }).tap do |result_set|
        result_set.record_class = klass
        yield(result_set) if block_given?
      end
    end

    def has_errors?
      errors && errors.length > 0
    end

    def out_of_bounds?
      current_page > 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
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
json_api_client-0.9.6 lib/json_api_client/result_set.rb
json_api_client-0.9.5 lib/json_api_client/result_set.rb
json_api_client-0.9.4 lib/json_api_client/result_set.rb
json_api_client-0.9.3 lib/json_api_client/result_set.rb
json_api_client-0.9.2 lib/json_api_client/result_set.rb
json_api_client-0.9.0 lib/json_api_client/result_set.rb
json_api_client-0.8.1 lib/json_api_client/result_set.rb
json_api_client-0.8.0 lib/json_api_client/result_set.rb
json_api_client-0.7.1 lib/json_api_client/result_set.rb
json_api_client-0.7.0 lib/json_api_client/result_set.rb