module Zendesk2::Collection

Public Class Methods

cistern_included(receiver)
Alias for: included
included(receiver) click to toggle source
Calls superclass method
# File lib/zendesk2/collection.rb, line 6
def included(receiver)
  cistern_included(receiver)
  receiver.extend(ClassMethods)
  super
end
Also aliased as: cistern_included

Public Instance Methods

all(params = {}) click to toggle source

Fetch a collection of resources

# File lib/zendesk2/collection.rb, line 65
def all(params = {})
  scoped_attributes = self.class.scopes.inject({}) { |a, e| a.merge(e.to_s => public_send(e)) }.merge(params)
  body = cistern.send(collection_method, scoped_attributes).body

  load(body[collection_root])
  merge_attributes(Cistern::Hash.slice(body, 'count'))
  self
end
collection_method() click to toggle source
# File lib/zendesk2/collection.rb, line 28
def collection_method
  self.class.collection_method
end
collection_root() click to toggle source
# File lib/zendesk2/collection.rb, line 32
def collection_root
  self.class.collection_root
end
create(attributes = {}) click to toggle source

Quietly attempt creation of resource. Check {#new_record?} and {#errors} for success @see {#create!} to raise an exception on failure @return [Cistern::Model, FalseClass]

# File lib/zendesk2/collection.rb, line 59
def create(attributes = {})
  model = new(attributes.merge(Zendesk2.stringify_keys(self.attributes)))
  model.save
end
create!(attributes = {}) click to toggle source

Attempt creation of resource and explode if unsuccessful @raise [Zendesk2::Error] if creation was unsuccessful @return [Cistern::Model]

# File lib/zendesk2/collection.rb, line 51
def create!(attributes = {})
  model = new(attributes.merge(Zendesk2.stringify_keys(self.attributes)))
  model.save!
end
get(*args) click to toggle source

Quiet version of {#get!} @see get! @return [Zendesk2::Model] Fetched model when successful @return [NilClass] return nothing if record cannot be found

# File lib/zendesk2/collection.rb, line 106
def get(*args)
  get!(*args)
rescue Zendesk2::Error
  nil
end
get!(identity_or_hash) click to toggle source

Fetch a single of resource @overload get!(identity)

fetch a un-namespaced specific record or a namespaced record under the current {#scopes}
@param [Integer] identity identity of the record

@overload get!(scope)

directly fetch a namespaced record
@param [Hash] scope parameters to fetch record

@example Fetch a record without contextual scoping

self.identities.all("user_id" => 2, "id" => 4) # context defined directly

@example Fetch a record with contextual scoping

self.identities("user_id" => 2).get(4) # context defined in collection
user.identities.get(4)                 # context defined by encapsulating model

@raise [Zendesk2::Error] if the record cannot be found or other request error @return [Zendesk2::Model] fetched resource corresponding to value of {Zendesk2::Collection#model}

# File lib/zendesk2/collection.rb, line 88
def get!(identity_or_hash)
  scoped_attributes = self.class.scopes.inject({}) { |a, e| a.merge(e.to_s => public_send(e)) }

  if identity_or_hash.is_a?(Hash)
    scoped_attributes.merge!(identity_or_hash)
  else
    scoped_attributes['id'] = identity_or_hash
  end

  scoped_attributes = { namespace => scoped_attributes }
  data = cistern.send(model_method, scoped_attributes).body[model_root]
  new(data) if data
end
model_method() click to toggle source
# File lib/zendesk2/collection.rb, line 36
def model_method
  self.class.model_method
end
model_root() click to toggle source
# File lib/zendesk2/collection.rb, line 40
def model_root
  self.class.model_root
end
namespace() click to toggle source
# File lib/zendesk2/collection.rb, line 44
def namespace
  self.class.namespace
end
new(attributes = {}) click to toggle source
Calls superclass method
# File lib/zendesk2/collection.rb, line 112
def new(attributes = {})
  super(self.attributes.merge(attributes))
end