Module: ContextIO::API::ResourceCollection
- Includes:
- Enumerable
- Included in:
- Lite::EmailAccountCollection, Lite::FolderCollection, Lite::MessageCollection, Lite::UserCollection, Lite::WebhookCollection
- Defined in:
- lib/contextio/api/resource_collection.rb
Overview
When ‘include`d into a class, this module provides some helper methods for various things a collections of resources will need or find useful.
Defined Under Namespace
Modules: DeclarativeClassSyntax
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Handle for the ‘API` instance.
-
#resource_url ⇒ String
readonly
The URL that will fetch attributes from the API.
-
#where_constraints ⇒ Object
readonly
Returns the value of attribute where_constraints.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns a resource with the given key.
-
#each(&block) ⇒ Object
Iterates over the resources in question.
-
#empty? ⇒ Boolean
Returns true if self contains no elements.
-
#initialize(api, options = {}) ⇒ Object
For internal use only.
-
#size ⇒ Object
(also: #length, #count)
Returns the number of elements in self.
-
#where(constraints) ⇒ Object
Specify one or more constraints for limiting resources in this collection.
Instance Attribute Details
#api ⇒ Object (readonly)
Handle for the ‘API` instance. For internal use only.
9 10 11 |
# File 'lib/contextio/api/resource_collection.rb', line 9 def api @api end |
#resource_url ⇒ String (readonly)
Returns The URL that will fetch attributes from the API.
38 39 40 |
# File 'lib/contextio/api/resource_collection.rb', line 38 def resource_url @resource_url ||= api.url_for(self) end |
#where_constraints ⇒ Object (readonly)
Returns the value of attribute where_constraints.
13 14 15 |
# File 'lib/contextio/api/resource_collection.rb', line 13 def where_constraints @where_constraints end |
Instance Method Details
#[](key) ⇒ Object
Returns a resource with the given key.
This is a lazy method, making no requests. When you try to access attributes on the object, or otherwise interact with it, it will actually make requests.
105 106 107 |
# File 'lib/contextio/api/resource_collection.rb', line 105 def [](key) resource_class.new(api, associations_hash.merge(resource_class.primary_key => key)) end |
#each(&block) ⇒ Object
Iterates over the resources in question.
48 49 50 51 52 |
# File 'lib/contextio/api/resource_collection.rb', line 48 def each(&block) attribute_hashes.each do |attribute_hash| yield resource_class.new(api, attribute_hash.merge(associations_hash)) end end |
#empty? ⇒ Boolean
Calling this method will load the collection if not already loaded.
Returns true if self contains no elements.
66 67 68 |
# File 'lib/contextio/api/resource_collection.rb', line 66 def empty? size == 0 end |
#initialize(api, options = {}) ⇒ Object
For internal use only. Users of this gem shouldn’t be calling this directly.
26 27 28 29 30 31 32 33 34 |
# File 'lib/contextio/api/resource_collection.rb', line 26 def initialize(api, ={}) @api = api @where_constraints = [:where] || {} @attribute_hashes = [:attribute_hashes] self.class.associations.each do |association_name| instance_variable_set("@#{association_name}", [association_name.to_sym]) end end |
#size ⇒ Object Also known as: length, count
Calling this method will load the collection if not already loaded.
Returns the number of elements in self. May be zero.
57 58 59 |
# File 'lib/contextio/api/resource_collection.rb', line 57 def size attribute_hashes.size end |
#where(constraints) ⇒ Object
Specify one or more constraints for limiting resources in this collection. See individual classes in the [Context.IO docs](context.io/docs/2.0/) for the list of valid constraints. Not all collections have valid where constraints at all.
This can be chained at need and doesn’t actually cause the API to get hit until some iterator is called like ‘#each`.
89 90 91 92 |
# File 'lib/contextio/api/resource_collection.rb', line 89 def where(constraints) constraints.each{|i,c| constraints[i] = (c ? 1 : 0) if c == !!c } self.class.new(api, associations_hash.merge(where: where_constraints.merge(constraints))) end |