Class: FulfilApi::Relation
- Inherits:
-
Object
- Object
- FulfilApi::Relation
- Includes:
- Enumerable, Batchable, Countable, Loadable, Naming, QueryMethods
- Defined in:
- lib/fulfil_api/relation.rb,
lib/fulfil_api/relation/naming.rb,
lib/fulfil_api/relation/loadable.rb,
lib/fulfil_api/relation/batchable.rb,
lib/fulfil_api/relation/countable.rb,
lib/fulfil_api/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: Batchable, Countable, Loadable, Naming, QueryMethods
Instance Attribute Summary collapse
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#model_name ⇒ Object
Returns the value of attribute model_name.
-
#request_limit ⇒ Object
Returns the value of attribute request_limit.
-
#request_offset ⇒ Object
Returns the value of attribute request_offset.
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::Relation
Resets any of the previously provided query conditions.
Methods included from QueryMethods
#find_by, #find_by!, #limit, #offset, #select, #where
Methods included from Naming
Methods included from Loadable
Methods included from Countable
Methods included from Batchable
Constructor Details
#initialize(resource_klass) ⇒ Relation
Returns a new instance of Relation.
26 27 28 29 30 31 32 33 |
# File 'lib/fulfil_api/relation.rb', line 26 def initialize(resource_klass) @resource_klass = resource_klass @loaded = false @resources = [] reset end |
Instance Attribute Details
#conditions ⇒ Object
Returns the value of attribute conditions.
21 22 23 |
# File 'lib/fulfil_api/relation.rb', line 21 def conditions @conditions end |
#fields ⇒ Object
Returns the value of attribute fields.
21 22 23 |
# File 'lib/fulfil_api/relation.rb', line 21 def fields @fields end |
#model_name ⇒ Object
Returns the value of attribute model_name.
21 22 23 |
# File 'lib/fulfil_api/relation.rb', line 21 def model_name @model_name end |
#request_limit ⇒ Object
Returns the value of attribute request_limit.
21 22 23 |
# File 'lib/fulfil_api/relation.rb', line 21 def request_limit @request_limit end |
#request_offset ⇒ Object
Returns the value of attribute request_offset.
21 22 23 |
# File 'lib/fulfil_api/relation.rb', line 21 def request_offset @request_offset 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.
40 41 42 43 |
# File 'lib/fulfil_api/relation.rb', line 40 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.
51 52 53 |
# File 'lib/fulfil_api/relation.rb', line 51 def each(&block) all.each(&block) end |
#reset ⇒ FulfilApi::Relation
Resets any of the previously provided query conditions.
58 59 60 61 62 63 64 65 |
# File 'lib/fulfil_api/relation.rb', line 58 def reset @conditions = [] @fields = %w[id] @request_limit = nil @request_offset = nil self end |