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

Instance Method Details

#loadtrue, 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.

Returns:

  • (true, false)

    True if the resources were loaded successfully.



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.

Returns:

  • (true, false)

    True if the resources are already loaded.



40
41
42
# File 'lib/fulfil_api/resource/relation/loadable.rb', line 40

def loaded?
  @loaded
end

#reloadtrue, false

Reloads the resources from Fulfil’s API by resetting the @loaded flag.

Returns:

  • (true, false)

    True if the resources were successfully reloaded.



47
48
49
50
# File 'lib/fulfil_api/resource/relation/loadable.rb', line 47

def reload
  @loaded = false
  load
end