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
-
#accessor ⇒ Object
client accessor.
-
#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
#accessor ⇒ Object
client accessor
21 22 23 24 |
# File 'lib/my_john_deere_api/request/collection/base.rb', line 21 def accessor return @accessor if defined?(@accessor) @accessor = client&.accessor end |
#all ⇒ Object
Return all objects in the collection at once
40 41 42 43 |
# File 'lib/my_john_deere_api/request/collection/base.rb', line 40 def all return @all if defined?(@all) @all = map { |i| i } end |
#count ⇒ Object
Total count of records, even before pagination
48 49 50 |
# File 'lib/my_john_deere_api/request/collection/base.rb', line 48 def count @count ||= first_page['total'] end |
#each(&block) ⇒ Object
Iterate lazily through all records in the collection, fetching additional pages as needed.
30 31 32 33 34 35 |
# File 'lib/my_john_deere_api/request/collection/base.rb', line 30 def each(&block) count.times do |index| fetch if @items.size <= index block.call(@items[index]) end end |