Class: MyJohnDeereApi::Request::Collection::Base

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Helpers::UriHelpers
Defined in:
lib/my_john_deere_api/request/collection/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, associations = {}) ⇒ Base

client is the original client instance which contains the necessary config info.



12
13
14
15
16
# File 'lib/my_john_deere_api/request/collection/base.rb', line 12

def initialize(client, associations = {})
  @client = client
  @associations = associations
  @items = []
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



6
7
8
# File 'lib/my_john_deere_api/request/collection/base.rb', line 6

def associations
  @associations
end

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/my_john_deere_api/request/collection/base.rb', line 6

def client
  @client
end

Instance Method Details

#allObject

Return all objects in the collection at once



32
33
34
35
# File 'lib/my_john_deere_api/request/collection/base.rb', line 32

def all
  return @all if defined?(@all)
  @all = map { |i| i }
end

#countObject

Total count of records, even before pagination



40
41
42
# File 'lib/my_john_deere_api/request/collection/base.rb', line 40

def count
  @count ||= first_page['total']
end

#each(&block) ⇒ Object

Iterate lazily through all records in the collection, fetching additional pages as needed.



22
23
24
25
26
27
# File 'lib/my_john_deere_api/request/collection/base.rb', line 22

def each(&block)
  count.times do |index|
    fetch if @items.size <= index
    block.call(@items[index])
  end
end