Class: FulfilApi::Resource::Relation
- Inherits:
-
Object
- Object
- FulfilApi::Resource::Relation
- 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
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#name ⇒ Object
Returns the value of attribute name.
-
#request_limit ⇒ Object
Returns the value of attribute request_limit.
Instance Method Summary collapse
-
#all ⇒ Array<FulfilApi::Resource>
Loads and returns all resources from Fulfil’s API.
-
#each {|resource| ... } ⇒ Enumerator, self
The #each method allows iteration over the resources.
-
#initialize(resource_klass) ⇒ Relation
constructor
A new instance of Relation.
-
#reset ⇒ FulfilApi::Resource::Relation
Resets any of the previously provided query conditions.
Methods included from QueryMethods
#find_by, #limit, #select, #where
Methods included from Naming
Methods included from Loadable
Constructor Details
#initialize(resource_klass) ⇒ Relation
Returns a new instance of Relation.
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
#conditions ⇒ Object
Returns the value of attribute conditions.
16 17 18 |
# File 'lib/fulfil_api/resource/relation.rb', line 16 def conditions @conditions end |
#fields ⇒ Object
Returns the value of attribute fields.
16 17 18 |
# File 'lib/fulfil_api/resource/relation.rb', line 16 def fields @fields end |
#name ⇒ Object
Returns the value of attribute name.
16 17 18 |
# File 'lib/fulfil_api/resource/relation.rb', line 16 def name @name end |
#request_limit ⇒ Object
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
#all ⇒ Array<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.
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.
46 47 48 |
# File 'lib/fulfil_api/resource/relation.rb', line 46 def each(&block) all.each(&block) end |
#reset ⇒ FulfilApi::Resource::Relation
Resets any of the previously provided query conditions.
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 |