Module: Zendesk2::Collection

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cistern_includedObject



4
# File 'lib/zendesk2/collection.rb', line 4

alias cistern_included included

.included(receiver) ⇒ Object



6
7
8
9
10
# File 'lib/zendesk2/collection.rb', line 6

def included(receiver)
  cistern_included(receiver)
  receiver.extend(ClassMethods)
  super
end

Instance Method Details

#all(params = {}) ⇒ Object

Fetch a collection of resources



65
66
67
68
69
70
71
72
# 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_methodObject



28
29
30
# File 'lib/zendesk2/collection.rb', line 28

def collection_method
  self.class.collection_method
end

#collection_rootObject



32
33
34
# File 'lib/zendesk2/collection.rb', line 32

def collection_root
  self.class.collection_root
end

#create(attributes = {}) ⇒ Cistern::Model, FalseClass

Quietly attempt creation of resource. Check #new_record? and #errors for success

Returns:

  • (Cistern::Model, FalseClass)

See Also:

  • to raise an exception on failure


59
60
61
62
# File 'lib/zendesk2/collection.rb', line 59

def create(attributes = {})
  model = new(attributes.merge(Zendesk2.stringify_keys(self.attributes)))
  model.save
end

#create!(attributes = {}) ⇒ Cistern::Model

Attempt creation of resource and explode if unsuccessful

Returns:

  • (Cistern::Model)

Raises:



51
52
53
54
# File 'lib/zendesk2/collection.rb', line 51

def create!(attributes = {})
  model = new(attributes.merge(Zendesk2.stringify_keys(self.attributes)))
  model.save!
end

#get(*args) ⇒ Zendesk2::Model, NilClass

Quiet version of #get!

Returns:

  • (Zendesk2::Model)

    Fetched model when successful

  • (NilClass)

    return nothing if record cannot be found

See Also:



106
107
108
109
110
# File 'lib/zendesk2/collection.rb', line 106

def get(*args)
  get!(*args)
rescue Zendesk2::Error
  nil
end

#get!(identity) ⇒ Zendesk2::Model #get!(scope) ⇒ Zendesk2::Model

Fetch a single of resource

Examples:

Fetch a record without contextual scoping

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

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

Overloads:

  • #get!(identity) ⇒ Zendesk2::Model

    fetch a un-namespaced specific record or a namespaced record under the current #scopes

    Parameters:

    • identity (Integer)

      identity of the record

  • #get!(scope) ⇒ Zendesk2::Model

    directly fetch a namespaced record

    Parameters:

    • scope (Hash)

      parameters to fetch record

Returns:

Raises:

  • (Zendesk2::Error)

    if the record cannot be found or other request error



88
89
90
91
92
93
94
95
96
97
98
99
100
# 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_methodObject



36
37
38
# File 'lib/zendesk2/collection.rb', line 36

def model_method
  self.class.model_method
end

#model_rootObject



40
41
42
# File 'lib/zendesk2/collection.rb', line 40

def model_root
  self.class.model_root
end

#namespaceObject



44
45
46
# File 'lib/zendesk2/collection.rb', line 44

def namespace
  self.class.namespace
end

#new(attributes = {}) ⇒ Object



112
113
114
# File 'lib/zendesk2/collection.rb', line 112

def new(attributes = {})
  super(self.attributes.merge(attributes))
end