Module: ContextIO::API::ResourceCollection
- Includes:
- Enumerable
- Included in:
- ContextIO::AccountCollection, BodyPartCollection, ConnectTokenCollection, ContactCollection, EmailAddressCollection, FileCollection, FolderCollection, MessageCollection, OAuthProviderCollection, SourceCollection, ThreadCollection, 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
-
#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.
-
#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
#resource_url ⇒ String (readonly)
Returns The URL that will fetch attributes from the API.
40 41 42 |
# File 'lib/contextio/api/resource_collection.rb', line 40 def resource_url @resource_url ||= api.url_for(self) end |
#where_constraints ⇒ Object (readonly)
Returns the value of attribute where_constraints.
15 16 17 |
# File 'lib/contextio/api/resource_collection.rb', line 15 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.
106 107 108 |
# File 'lib/contextio/api/resource_collection.rb', line 106 def [](key) resource_class.new(api, associations_hash.merge(resource_class.primary_key => key)) end |
#each(&block) ⇒ Object
Iterates over the resources in question.
50 51 52 53 54 |
# File 'lib/contextio/api/resource_collection.rb', line 50 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.
68 69 70 |
# File 'lib/contextio/api/resource_collection.rb', line 68 def empty? size == 0 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.
59 60 61 |
# File 'lib/contextio/api/resource_collection.rb', line 59 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 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
.
91 92 93 |
# File 'lib/contextio/api/resource_collection.rb', line 91 def where(constraints) self.class.new(api, associations_hash.merge(where: where_constraints.merge(constraints))) end |