Class: JsonApiClient::ResultSet

Inherits:
Array
  • Object
show all
Defined in:
lib/json_api_client/result_set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



4
5
6
# File 'lib/json_api_client/result_set.rb', line 4

def current_page
  @current_page
end

#errorsObject

Returns the value of attribute errors.



4
5
6
# File 'lib/json_api_client/result_set.rb', line 4

def errors
  @errors
end

#metaObject

Returns the value of attribute meta.



4
5
6
# File 'lib/json_api_client/result_set.rb', line 4

def meta
  @meta
end

#offsetObject

Returns the value of attribute offset.



4
5
6
# File 'lib/json_api_client/result_set.rb', line 4

def offset
  @offset
end

#per_pageObject

Returns the value of attribute per_page.



4
5
6
# File 'lib/json_api_client/result_set.rb', line 4

def per_page
  @per_page
end

#record_classObject

Returns the value of attribute record_class.



4
5
6
# File 'lib/json_api_client/result_set.rb', line 4

def record_class
  @record_class
end

#total_entriesObject

Returns the value of attribute total_entries.



4
5
6
# File 'lib/json_api_client/result_set.rb', line 4

def total_entries
  @total_entries
end

#total_pagesObject

Returns the value of attribute total_pages.



4
5
6
# File 'lib/json_api_client/result_set.rb', line 4

def total_pages
  @total_pages
end

Class Method Details

.build(klass, data) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/json_api_client/result_set.rb', line 14

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

Instance Method Details

#has_errors?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/json_api_client/result_set.rb', line 24

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

#next_pageObject



36
37
38
# File 'lib/json_api_client/result_set.rb', line 36

def next_page
  current_page < total_pages ? (current_page + 1) : nil
end

#out_of_bounds?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/json_api_client/result_set.rb', line 28

def out_of_bounds?
  current_page > total_pages
end

#previous_pageObject



32
33
34
# File 'lib/json_api_client/result_set.rb', line 32

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end