module ContextIO::API::ResourceCollection

When ‘include`d into a class, this module provides some helper methods for various things a collections of resources will need or find useful.

Attributes

api[R]
where_constraints[R]

@!attribute [r] where_constraints

A Hash of the constraints limiting this collection of resources.

Public Class Methods

new(api, options={}) click to toggle source

@private

For internal use only. Users of this gem shouldn’t be calling this directly.

@param [API] api A handle on the Context.IO API. @param [Hash] options Optional params for the collection. @option options [Hash{Symbol => String, Numeric}] :where Where

constraints that limit the resources that belong to this collection.

@option options [Array<Hash>] :attribute_hashes An array of hashes

describing the resources in this collection.
# File lib/contextio/api/resource_collection.rb, line 26
def initialize(api, options={})
  @api = api
  @where_constraints = options[:where] || {}
  @attribute_hashes = options[:attribute_hashes]

  self.class.associations.each do |association_name|
    instance_variable_set("@#{association_name}", options[association_name.to_sym])
  end
end

Private Class Methods

included(other_mod) click to toggle source

Make sure a ResourceCollection has the declarative syntax handy.

# File lib/contextio/api/resource_collection.rb, line 132
def self.included(other_mod)
  other_mod.extend(DeclarativeClassSyntax)
end

Public Instance Methods

[](key) click to toggle source

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.

@example

provider = contextio.oauth_providers['1234']

@param [String] key The Provider Consumer Key for the

provider you want to interact with.
# File lib/contextio/api/resource_collection.rb, line 105
def [](key)
  resource_class.new(api, associations_hash.merge(resource_class.primary_key => key))
end
count()
Alias for: size
each() { |resource_class(api, merge)| ... } click to toggle source

Iterates over the resources in question.

@example

contextio.connect_tokens.each do |connect_token|
  puts connect_token.email
end
# 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?() click to toggle source

Returns true if self contains no elements.

@note Calling this method will load the collection if not already loaded.

# File lib/contextio/api/resource_collection.rb, line 66
def empty?
  size == 0
end
length()
Alias for: size
resource_url() click to toggle source

@!attribute [r] resource_url @return [String] The URL that will fetch attributes from the API.

# File lib/contextio/api/resource_collection.rb, line 38
def resource_url
  @resource_url ||= api.url_for(self)
end
size() click to toggle source

Returns the number of elements in self. May be zero.

@note Calling this method will load the collection if not already loaded.

# File lib/contextio/api/resource_collection.rb, line 57
def size
  attribute_hashes.size
end
Also aliased as: length, count
where(constraints) click to toggle source

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`.

@example

accounts = contextio.accounts
accounts = accounts.where(email: 'some@email.com')
accounts = accounts.where(status: 'OK')

accounts.each do |account|
  # API gets hit for this call
end

@param [Hash{String, Symbol => String, Integer}] constraints A Hash

mapping keys to the desired limiting values.
# 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

Private Instance Methods

associations_hash() click to toggle source

@!attribute [r] associations_hash

@return [Hash{Symbol => Resource}] A hash of association names to the
  associated resource of that type.
# File lib/contextio/api/resource_collection.rb, line 121
def associations_hash
  @associations_hash ||= self.class.associations.inject({}) do |memo, association_name|
    if (association = self.send(association_name))
      memo[association_name.to_sym] = association
    end

    memo
  end
end
attribute_hashes() click to toggle source

@!attribute [r] attribute_hashes

@return [Array<Hash>] An array of attribute hashes that describe, at
  least partially, the objects in this collection.
# File lib/contextio/api/resource_collection.rb, line 114
def attribute_hashes
  @attribute_hashes ||= api.request(:get, resource_url, where_constraints)
end