Class: EmpireAvenue::Base
- Inherits:
-
Object
- Object
- EmpireAvenue::Base
- Defined in:
- lib/empireavenue/base.rb
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) ⇒ EmpireAvenue::Base
Retrieves an object from the identity map.
-
.fetch_or_new(attrs = {}) ⇒ EmpireAvenue::Base
Retrieves an object from the identity map, or stores it in the identity map if it doesn’t already exist.
-
.from_response(response = {}) ⇒ EmpireAvenue::Base
Returns a new object based on the response hash.
-
.identity_map ⇒ Object
return [EmpireAvenue::IdentityMap].
-
.store(object) ⇒ EmpireAvenue::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 = {}) ⇒ EmpireAvenue::Base
constructor
Initializes a new object.
-
#update(attrs) ⇒ EmpireAvenue::Base
Update the attributes of an object.
Constructor Details
#initialize(attrs = {}) ⇒ EmpireAvenue::Base
Initializes a new object
80 81 82 |
# File 'lib/empireavenue/base.rb', line 80 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
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/empireavenue/base.rb', line 9 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) ⇒ EmpireAvenue::Base
Retrieves an object from the identity map.
35 36 37 38 39 40 41 42 |
# File 'lib/empireavenue/base.rb', line 35 def self.fetch(attrs) return unless identity_map if object = identity_map.fetch(Marshal.dump(attrs)) return object end return yield if block_given? raise EmpireAvenue::Error::IdentityMapKeyError, "key not found" end |
.fetch_or_new(attrs = {}) ⇒ EmpireAvenue::Base
Retrieves an object from the identity map, or stores it in the identity map if it doesn’t already exist.
66 67 68 69 70 71 72 73 74 |
# File 'lib/empireavenue/base.rb', line 66 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 = {}) ⇒ EmpireAvenue::Base
Returns a new object based on the response hash
57 58 59 |
# File 'lib/empireavenue/base.rb', line 57 def self.from_response(response={}) fetch_or_new(response[:body]) end |
.identity_map ⇒ Object
return [EmpireAvenue::IdentityMap]
25 26 27 28 29 |
# File 'lib/empireavenue/base.rb', line 25 def self.identity_map return unless EmpireAvenue.identity_map @identity_map = EmpireAvenue.identity_map.new unless defined?(@identity_map) && @identity_map.class == EmpireAvenue.identity_map @identity_map end |
.store(object) ⇒ EmpireAvenue::Base
Stores an object in the identity map.
48 49 50 51 |
# File 'lib/empireavenue/base.rb', line 48 def self.store(object) return object unless identity_map identity_map.store(Marshal.dump(object.attrs), object) end |
Instance Method Details
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation
87 88 89 90 91 |
# File 'lib/empireavenue/base.rb', line 87 def [](method) send(method.to_sym) rescue NoMethodError nil end |
#attrs ⇒ Hash Also known as: to_hash
Retrieve the attributes of an object
96 97 98 |
# File 'lib/empireavenue/base.rb', line 96 def attrs @attrs end |
#update(attrs) ⇒ EmpireAvenue::Base
Update the attributes of an object
105 106 107 108 |
# File 'lib/empireavenue/base.rb', line 105 def update(attrs) @attrs.update(attrs) self end |