Module: FulfilApi::Resource::Relation::Loadable
- Included in:
- FulfilApi::Resource::Relation
- Defined in:
- lib/fulfil_api/resource/relation/loadable.rb
Overview
The Loadable extends the relation by
adding methods to load, reload and identify loaded resources from Fulfil's
API endpoints.
By default, all HTTP requests to Fulfil are delayed until they’re directly
or indirectly requested by the user of the gem. This way, we ensure that
we only request data when we need to.
Instance Method Summary collapse
-
#load ⇒ true, false
Loads resources from Fulfil’s API based on the current filters, fields, and limits if they haven’t been loaded yet.
-
#loaded? ⇒ true, false
Checks whether the resources have been loaded to avoid repeated API calls when using enumerable methods.
-
#reload ⇒ true, false
Reloads the resources from Fulfil’s API by resetting the @loaded flag.
Instance Method Details
#load ⇒ true, false
Loads resources from Fulfil’s API based on the current filters, fields, and limits
if they haven't been loaded yet.
Requires that FulfilApi::Resource::Relation#name is set; raises an exception if it’s not.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fulfil_api/resource/relation/loadable.rb', line 20 def load return true if loaded? if name.nil? raise FulfilApi::Resource::Relation::ModelNameMissing, "The model name is missing. Use #set to define it." end response = FulfilApi.client.put( "/model/#{name}/search_read", body: { filters: conditions, fields: fields, limit: request_limit }.compact_blank ) @resources = response.map { |resource| @resource_klass.new(resource) } @loaded = true end |
#loaded? ⇒ true, false
Checks whether the resources have been loaded to avoid repeated API calls when
using enumerable methods.
40 41 42 |
# File 'lib/fulfil_api/resource/relation/loadable.rb', line 40 def loaded? @loaded end |
#reload ⇒ true, false
Reloads the resources from Fulfil’s API by resetting the @loaded flag.
47 48 49 50 |
# File 'lib/fulfil_api/resource/relation/loadable.rb', line 47 def reload @loaded = false load end |