Class: MyJohnDeereApi::Request::Collection::Base
- Inherits:
-
Object
- Object
- MyJohnDeereApi::Request::Collection::Base
- Includes:
- Enumerable, Helpers::UriHelpers
- Defined in:
- lib/my_john_deere_api/request/collection/base.rb
Direct Known Subclasses
AssetLocations, Assets, ContributionDefinitions, ContributionProducts, Fields, Flags, Organizations
Instance Attribute Summary collapse
-
#associations ⇒ Object
readonly
Returns the value of attribute associations.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#all ⇒ Object
Return all objects in the collection at once.
-
#count ⇒ Object
Total count of records, even before pagination.
-
#each(&block) ⇒ Object
Iterate lazily through all records in the collection, fetching additional pages as needed.
-
#initialize(client, associations = {}) ⇒ Base
constructor
client is the original client instance which contains the necessary config info.
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
#associations ⇒ Object (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 |
#client ⇒ Object (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
#all ⇒ Object
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 |
#count ⇒ Object
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 |