Class: Panda::Resource

Inherits:
Base
  • Object
show all
Extended by:
CloudConnection, Finders::FindMany
Includes:
Associations, Builders, CloudConnection
Defined in:
lib/panda/resources/resource.rb

Direct Known Subclasses

Encoding, Profile, Video

Constant Summary

Constants included from Router

Panda::Router::DEFAULT_FORMAT, Panda::Router::VAR_PATTERN

Instance Attribute Summary

Attributes inherited from Base

#attributes, #errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CloudConnection

connection

Methods included from Finders::FindMany

all, find_by

Methods included from Associations

included

Methods included from Builders

included

Methods inherited from Base

#changed?, #delete, end_class_name, id, #id, #id=, #new?, #to_json

Methods included from Router

included, #object_url_map

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
# File 'lib/panda/resources/resource.rb', line 7

def initialize(attributes={})
  super(attributes)
  @attributes['cloud_id'] ||= Panda.cloud.id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Panda::Base

Class Method Details

.cloudObject



16
17
18
# File 'lib/panda/resources/resource.rb', line 16

def cloud
  Panda.cloud
end

.method_missing(method_symbol, *args, &block) ⇒ Object

delegate to the scope if the method exists



21
22
23
24
25
26
27
28
# File 'lib/panda/resources/resource.rb', line 21

def method_missing(method_symbol, *args, &block)
  scope = Panda::const_get("#{end_class_name}Scope").new(self)
  if scope.respond_to?(method_symbol)
     scope.send(method_symbol, *args, &block)
  else
    super
  end
end

Instance Method Details

#cloudObject



32
33
34
# File 'lib/panda/resources/resource.rb', line 32

def cloud
  Panda.clouds[cloud_id]
end

#createObject



36
37
38
39
40
# File 'lib/panda/resources/resource.rb', line 36

def create
  raise "Can't create attribute. Already have an id=#{attributes['id']}" if attributes['id']
  response = connection.post(object_url_map(self.class.many_path), attributes)
  load_response(response) ? (@changed_attributes = {}; true) : false
end

#create!Object



42
43
44
# File 'lib/panda/resources/resource.rb', line 42

def create!
  create || errors.last.raise!
end

#reloadObject



46
47
48
49
50
# File 'lib/panda/resources/resource.rb', line 46

def reload
  perform_reload("cloud_id" => cloud_id)
  reset_associations
  self
end