class EmpireAvenue::Base
Public Class Methods
Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
@param attrs [Array, Set, Symbol]
# 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
Retrieves an object from the identity map.
@param attrs [Hash] @return [EmpireAvenue::Base]
# 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
Retrieves an object from the identity map, or stores it in the identity map if it doesn’t already exist.
@param attrs [Hash] @return [EmpireAvenue::Base]
# 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
Returns a new object based on the response hash
@param response [Hash] @return [EmpireAvenue::Base]
# File lib/empireavenue/base.rb, line 57 def self.from_response(response={}) fetch_or_new(response[:body]) end
return [EmpireAvenue::IdentityMap]
# 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
Initializes a new object
@param attrs [Hash] @return [EmpireAvenue::Base]
# File lib/empireavenue/base.rb, line 80 def initialize(attrs={}) @attrs = attrs end
Stores an object in the identity map.
@param object [Object] @return [EmpireAvenue::Base]
# 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
Public Instance Methods
Fetches an attribute of an object using hash notation
@param method [String, Symbol] Message to send to the object
# File lib/empireavenue/base.rb, line 87 def [](method) send(method.to_sym) rescue NoMethodError nil end
Retrieve the attributes of an object
@return [Hash]
# File lib/empireavenue/base.rb, line 96 def attrs @attrs end
Update the attributes of an object
@param attrs [Hash] @return [EmpireAvenue::Base]
# File lib/empireavenue/base.rb, line 105 def update(attrs) @attrs.update(attrs) self end
Protected Instance Methods
@param attr [Symbol] @param other [EmpireAvenue::Base] @return [Boolean]
# File lib/empireavenue/base.rb, line 115 def attr_equal(attr, other) self.class == other.class && !other.send(attr).nil? && send(attr) == other.send(attr) end
@param other [EmpireAvenue::Base] @return [Boolean]
# File lib/empireavenue/base.rb, line 121 def attrs_equal(other) self.class == other.class && !other.attrs.empty? && attrs == other.attrs end