Sha256: ce6df4135f551340c39857b22c40d970beb3d47524c20893d39b916b93d7ff12

Contents?: true

Size: 720 Bytes

Versions: 6

Compression:

Stored size: 720 Bytes

Contents

module JsonApiClient
  class ResultSet < Array

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

    def self.build(klass, data)
      result_data = data.fetch(klass.table_name, [])
      new(result_data.map {|attributes| klass.new(attributes) }).tap do |result_set|
        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

6 entries across 6 versions & 1 rubygems

Version Path
json_api_client-0.3.1 lib/json_api_client/result_set.rb
json_api_client-0.3.0 lib/json_api_client/result_set.rb
json_api_client-0.2.4 lib/json_api_client/result_set.rb
json_api_client-0.2.3 lib/json_api_client/result_set.rb
json_api_client-0.2.2 lib/json_api_client/result_set.rb
json_api_client-0.2.1 lib/json_api_client/result_set.rb