Class: NoyoFulfillment::ApiResourceCollection

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/noyo_fulfillment/models/api_resource_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#==, #attributes, class_name, #excluded_attributes, #inspect, #synced_attributes, #to_h, #update_attrs

Constructor Details

#initialize(records, page_number = 0) ⇒ ApiResourceCollection

Returns a new instance of ApiResourceCollection.



13
14
15
16
17
# File 'lib/noyo_fulfillment/models/api_resource_collection.rb', line 13

def initialize(records, page_number = 0)
  @records = records
  @page_number = page_number
  super(records: records, page_number: page_number)
end

Instance Attribute Details

#page_numberObject

Returns the value of attribute page_number.



7
8
9
# File 'lib/noyo_fulfillment/models/api_resource_collection.rb', line 7

def page_number
  @page_number
end

#recordsObject

Returns the value of attribute records.



7
8
9
# File 'lib/noyo_fulfillment/models/api_resource_collection.rb', line 7

def records
  @records
end

Instance Method Details

#[](key) ⇒ Object



27
28
29
# File 'lib/noyo_fulfillment/models/api_resource_collection.rb', line 27

def [](key)
  records[key]
end

#firstObject



31
32
33
# File 'lib/noyo_fulfillment/models/api_resource_collection.rb', line 31

def first
  records.first
end

#next_pageObject



19
20
21
22
23
24
25
# File 'lib/noyo_fulfillment/models/api_resource_collection.rb', line 19

def next_page
  raise "Invalid number of records: #{records.size}" if !records.size.positive?

  klass = records.first.class
  next_page_number = page_number + 1
  klass.all(next_page_number)
end

#sizeObject Also known as: count, length



35
36
37
# File 'lib/noyo_fulfillment/models/api_resource_collection.rb', line 35

def size
  records.size
end