Class: FulfilApi::Resource::Relation

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Loadable, Naming, QueryMethods
Defined in:
lib/fulfil_api/resource/relation.rb,
lib/fulfil_api/resource/relation/naming.rb,
lib/fulfil_api/resource/relation/loadable.rb,
lib/fulfil_api/resource/relation/query_methods.rb

Overview

The Relation class provides an abstraction for chaining multiple API operations.

It allows handling a set of API resources in a uniform way, similar to

ActiveRecord's query interface, enabling the user to build complex queries
in a clean and reusable manner.

Defined Under Namespace

Modules: Loadable, Naming, QueryMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from QueryMethods

#find_by, #limit, #select, #where

Methods included from Naming

#set

Methods included from Loadable

#load, #loaded?, #reload

Constructor Details

#initialize(resource_klass) ⇒ Relation

Returns a new instance of Relation.

Parameters:



21
22
23
24
25
26
27
28
# File 'lib/fulfil_api/resource/relation.rb', line 21

def initialize(resource_klass)
  @resource_klass = resource_klass

  @loaded = false
  @resources = []

  reset
end

Instance Attribute Details

#conditionsObject

Returns the value of attribute conditions.



16
17
18
# File 'lib/fulfil_api/resource/relation.rb', line 16

def conditions
  @conditions
end

#fieldsObject

Returns the value of attribute fields.



16
17
18
# File 'lib/fulfil_api/resource/relation.rb', line 16

def fields
  @fields
end

#nameObject

Returns the value of attribute name.



16
17
18
# File 'lib/fulfil_api/resource/relation.rb', line 16

def name
  @name
end

#request_limitObject

Returns the value of attribute request_limit.



16
17
18
# File 'lib/fulfil_api/resource/relation.rb', line 16

def request_limit
  @request_limit
end

Instance Method Details

#allArray<FulfilApi::Resource>

Loads and returns all resources from Fulfil’s API. This method functions as a proxy,

deferring the loading of resources until they are required, thus avoiding unnecessary
HTTP requests.

Returns:



35
36
37
38
# File 'lib/fulfil_api/resource/relation.rb', line 35

def all
  load
  @resources
end

#each {|resource| ... } ⇒ Enumerator, self

The #each method allows iteration over the resources. If no block is given,

it returns an Enumerator, enabling lazy evaluation and allowing for chaining
without immediately triggering an API request.

Yields:

  • (resource)

    Yields each resource object to the given block.

Returns:

  • (Enumerator, self)

    Returns an Enumerator if no block is given; otherwise, returns self.



46
47
48
# File 'lib/fulfil_api/resource/relation.rb', line 46

def each(&block)
  all.each(&block)
end

#resetFulfilApi::Resource::Relation

Resets any of the previously provided query conditions.

Returns:



53
54
55
56
57
58
59
# File 'lib/fulfil_api/resource/relation.rb', line 53

def reset
  @conditions = []
  @fields = %w[id]
  @limit = nil

  self
end