Class: Playapi::Base
- Inherits:
-
Object
- Object
- Playapi::Base
- Defined in:
- lib/playapi/base.rb
Direct Known Subclasses
Class Method Summary collapse
-
.attr_reader(*attrs) ⇒ Object
Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key.
-
.fetch(attrs) ⇒ Playapi::Base
Retrieves an object from the identity map.
-
.fetch_or_new(attrs = {}) ⇒ Playapi::Base
Retrieves an object from the identity map, or stores it in the identity map if it doesn’t already exist.
-
.from_response(response = {}) ⇒ Playapi::Base
Returns a new object based on the response hash.
-
.identity_map ⇒ Object
return [Playapi::IdentityMap].
-
.store(object) ⇒ Playapi::Base
Stores an object in the identity map.
Instance Method Summary collapse
-
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation.
-
#attrs ⇒ Hash
(also: #to_hash)
Retrieve the attributes of an object.
-
#initialize(attrs = {}) ⇒ Playapi::Base
constructor
Initializes a new object.
-
#update(attrs) ⇒ Playapi::Base
Update the attributes of an object.
Constructor Details
#initialize(attrs = {}) ⇒ Playapi::Base
Initializes a new object
78 79 80 |
# File 'lib/playapi/base.rb', line 78 def initialize(attrs={}) @attrs = attrs end |
Class Method Details
.attr_reader(*attrs) ⇒ Object
Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/playapi/base.rb', line 6 def self.attr_reader(*attrs) mod = Module.new do attrs.each do |attribute| define_method attribute do @attrs[attribute.to_sym] end define_method "#{attribute}?" do !!@attrs[attribute.to_sym] end end end const_set(:Attributes, mod) include mod end |
.fetch(attrs) ⇒ Playapi::Base
Retrieves an object from the identity map.
THIS NEEDS TO BE REIMPLEMENTED
33 34 35 36 37 38 39 |
# File 'lib/playapi/base.rb', line 33 def self.fetch(attrs) return unless identity_map if object = identity_map.fetch("#{self.class.to_s.downcase}:#{attrs[:id]}") return object end return yield if block_given? end |
.fetch_or_new(attrs = {}) ⇒ Playapi::Base
Retrieves an object from the identity map, or stores it in the identity map if it doesn’t already exist.
64 65 66 67 68 69 70 71 72 |
# File 'lib/playapi/base.rb', line 64 def self.fetch_or_new(attrs={}) return unless attrs return new(attrs) unless identity_map fetch(attrs) do object = new(attrs) store(object) end end |
.from_response(response = {}) ⇒ Playapi::Base
Returns a new object based on the response hash
55 56 57 |
# File 'lib/playapi/base.rb', line 55 def self.from_response(response={}) fetch_or_new(response[:body]) end |
.identity_map ⇒ Object
return [Playapi::IdentityMap]
22 23 24 25 26 |
# File 'lib/playapi/base.rb', line 22 def self.identity_map return unless Playapi.identity_map @identity_map = Playapi.identity_map.new unless defined?(@identity_map) && @identity_map.class == Playapi.identity_map @identity_map end |
.store(object) ⇒ Playapi::Base
Stores an object in the identity map.
45 46 47 48 49 |
# File 'lib/playapi/base.rb', line 45 def self.store(object) return object unless identity_map # should this operate on type even though not ever response returns type? (campaign/entities) identity_map.store("#{self.class.to_s.downcase}:#{object.id}", object) end |
Instance Method Details
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation
85 86 87 88 89 |
# File 'lib/playapi/base.rb', line 85 def [](method) send(method.to_sym) rescue NoMethodError nil end |
#attrs ⇒ Hash Also known as: to_hash
Retrieve the attributes of an object
94 95 96 |
# File 'lib/playapi/base.rb', line 94 def attrs @attrs end |
#update(attrs) ⇒ Playapi::Base
Update the attributes of an object
103 104 105 106 |
# File 'lib/playapi/base.rb', line 103 def update(attrs) @attrs.update(attrs) self end |