Class: Animoto::Resources::Base Abstract
- Inherits:
-
Object
- Object
- Animoto::Resources::Base
- Includes:
- Support::StandardEnvelope
- Defined in:
- lib/animoto/resources/base.rb
Overview
Set #endpoint and maybe override Support::StandardEnvelope::ClassMethods#unpack_standard_envelope to subclass.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
- .endpoint(path = nil) ⇒ Object
-
.load(body) ⇒ Resources::Base
Makes a new instance of this class from a deserialized response body.
- .new(attributes = {}) ⇒ Object
-
.original_new ⇒ Resources::Base
If an instance is instantiated with the same unique identifier (i.e. URL) as another, instead of creating a brand new object, will instead update and return the existing object.
-
.register(instance) ⇒ Object
Registers an instance in the identity map so that subsequent finds or instantiations of this resource with the same URL will return the same object.
Instance Method Summary collapse
-
#endpoint ⇒ String
Returns the endpoint for this class.
-
#initialize(attributes = {}) ⇒ Resources::Base
constructor
Creates a new resource.
-
#instantiate(attributes = {}) ⇒ self
Since resources can be created a number of different ways, this method does the actual attribute setting for a resource, acting much like a public version of #initialize.
-
#load(body = {}) ⇒ self
Update this instance with new attributes from the response body.
Methods included from Support::StandardEnvelope
Constructor Details
#initialize(attributes = {}) ⇒ Resources::Base
Creates a new resource.
95 96 97 |
# File 'lib/animoto/resources/base.rb', line 95 def initialize attributes = {} instantiate attributes end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
88 89 90 |
# File 'lib/animoto/resources/base.rb', line 88 def errors @errors end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
88 89 90 |
# File 'lib/animoto/resources/base.rb', line 88 def url @url end |
Class Method Details
.endpoint(path) ⇒ String .endpoint ⇒ String
19 20 21 22 |
# File 'lib/animoto/resources/base.rb', line 19 def self.endpoint path = nil @endpoint = path if path @endpoint end |
.load(body) ⇒ Resources::Base
Makes a new instance of this class from a deserialized response body. Note that it assumes the hash you’re passing is structured correctly and does no format checking at all, so if the hash is not in the “standard envelope”, this method will most likely raise an error.
39 40 41 |
# File 'lib/animoto/resources/base.rb', line 39 def self.load body new unpack_standard_envelope(body) end |
.new(attributes = {}) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/animoto/resources/base.rb', line 59 def new attributes = {} if attributes[:url] && instances[attributes[:url]] instances[attributes[:url]].instantiate attributes else original_new attributes end end |
.original_new ⇒ Resources::Base
If an instance is instantiated with the same unique identifier (i.e. URL) as another, instead of creating a brand new object, will instead update and return the existing object. This way there should only be one object representing any one resource.
58 |
# File 'lib/animoto/resources/base.rb', line 58 alias_method :original_new, :new |
.register(instance) ⇒ Object
Registers an instance in the identity map so that subsequent finds or instantiations of this resource with the same URL will return the same object.
73 74 75 76 |
# File 'lib/animoto/resources/base.rb', line 73 def register instance raise ArgumentError unless instance.is_a?(self) instances[instance.url] = instance end |
Instance Method Details
#endpoint ⇒ String
Returns the endpoint for this class.
27 28 29 |
# File 'lib/animoto/resources/base.rb', line 27 def endpoint self.class.endpoint end |
#instantiate(attributes = {}) ⇒ self
Since resources can be created a number of different ways, this method does the actual attribute setting for a resource, acting much like a public version of #initialize.
115 116 117 118 119 120 |
# File 'lib/animoto/resources/base.rb', line 115 def instantiate attributes = {} @errors = (attributes[:errors] || []).collect { |e| wrap_error e } @url = attributes[:url] self.class.register(self) if @url self end |
#load(body = {}) ⇒ self
Update this instance with new attributes from the response body.
104 105 106 |
# File 'lib/animoto/resources/base.rb', line 104 def load body = {} instantiate unpack_standard_envelope(body) end |