Class: Wayback::Base
- Inherits:
-
Object
- Object
- Wayback::Base
- Defined in:
- lib/wayback/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) ⇒ Wayback::Base
Retrieves an object from the identity map.
-
.fetch_or_new(attrs = {}) ⇒ Wayback::Base
Retrieves an object from the identity map, or stores it in the identity map if it doesn't already exist.
-
.from_response(response = {}) ⇒ Wayback::Base
Returns a new object based on the response hash.
-
.identity_map ⇒ Object
return [Wayback::IdentityMap].
-
.store(object) ⇒ Wayback::Base
Stores an object in the identity map.
Instance Method Summary collapse
-
#[](method) ⇒ Object
Fetches an attribute of an object using hash notation.
- #attr_equal(attr, other) ⇒ Boolean protected
-
#attrs ⇒ Hash
(also: #to_hash)
Retrieve the attributes of an object.
- #attrs_equal(other) ⇒ Boolean protected
-
#initialize(attrs = {}) ⇒ Wayback::Base
constructor
Initializes a new object.
-
#update(attrs) ⇒ Wayback::Base
Update the attributes of an object.
Constructor Details
#initialize(attrs = {}) ⇒ Wayback::Base
Initializes a new object
79 80 81 |
# File 'lib/wayback/base.rb', line 79 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
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/wayback/base.rb', line 8 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) ⇒ Wayback::Base
Retrieves an object from the identity map.
34 35 36 37 38 39 40 41 |
# File 'lib/wayback/base.rb', line 34 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 Wayback::Error::IdentityMapKeyError, "key not found" end |
.fetch_or_new(attrs = {}) ⇒ Wayback::Base
Retrieves an object from the identity map, or stores it in the identity map if it doesn't already exist.
65 66 67 68 69 70 71 72 73 |
# File 'lib/wayback/base.rb', line 65 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 = {}) ⇒ Wayback::Base
Returns a new object based on the response hash
56 57 58 |
# File 'lib/wayback/base.rb', line 56 def self.from_response(response={}) fetch_or_new(response[:body]) end |
.identity_map ⇒ Object
return [Wayback::IdentityMap]
24 25 26 27 28 |
# File 'lib/wayback/base.rb', line 24 def self.identity_map return unless Wayback.identity_map @identity_map = Wayback.identity_map.new unless defined?(@identity_map) && @identity_map.class == Wayback.identity_map @identity_map end |
.store(object) ⇒ Wayback::Base
Stores an object in the identity map.
47 48 49 50 |
# File 'lib/wayback/base.rb', line 47 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
86 87 88 89 90 |
# File 'lib/wayback/base.rb', line 86 def [](method) send(method.to_sym) rescue NoMethodError nil end |
#attr_equal(attr, other) ⇒ Boolean (protected)
116 117 118 |
# File 'lib/wayback/base.rb', line 116 def attr_equal(attr, other) self.class == other.class && !other.send(attr).nil? && send(attr) == other.send(attr) end |
#attrs ⇒ Hash Also known as: to_hash
Retrieve the attributes of an object
95 96 97 98 99 |
# File 'lib/wayback/base.rb', line 95 def attrs @attrs.inject({}) do |attrs, (key, value)| attrs.merge!(key => respond_to?(key) ? send(key) : value) end end |
#attrs_equal(other) ⇒ Boolean (protected)
122 123 124 |
# File 'lib/wayback/base.rb', line 122 def attrs_equal(other) self.class == other.class && !other.attrs.empty? && attrs == other.attrs end |
#update(attrs) ⇒ Wayback::Base
Update the attributes of an object
106 107 108 109 |
# File 'lib/wayback/base.rb', line 106 def update(attrs) @attrs.update(attrs) self end |