class Echowrap::Base

Public Class Methods

attr_reader(*attrs) click to toggle source

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/echowrap/base.rb, line 6
def self.attr_reader(*attrs)
  #@attr_readers ||= []
  #@attr_readers.concat attrs

  mod = Module.new do
    attrs.each do |attribute|
      define_method attribute do
        @attrs[attribute.to_sym] if @attrs
      end
      define_method "#{attribute}?" do
        !!@attrs[attribute.to_sym]
      end
    end
  end
  const_set(:Attributes, mod)
  include mod
end
new(attrs={}) click to toggle source

Initializes a new object

@param attrs [Hash] @return [Echowrap::Base]

# File lib/echowrap/base.rb, line 34
def initialize(attrs={})
  @attrs = attrs
end

Public Instance Methods

==(other) click to toggle source

@param other [Echowrap::Base] @return [Boolean]

Calls superclass method
# File lib/echowrap/base.rb, line 26
def ==(other)
  super || attr_equal(:id, other) || attrs_equal(other)
end
attrs() click to toggle source

Retrieve the attributes of an object

@return [Hash]

# File lib/echowrap/base.rb, line 41
def attrs
  @attrs
end
Also aliased as: to_hash
to_hash()
Alias for: attrs
update(attrs) click to toggle source

Update the attributes of an object

@param attrs [Hash] @return [Echowrap::Base]

# File lib/echowrap/base.rb, line 50
def update(attrs)
  @attrs.update(attrs)
  self
end

Protected Instance Methods

attr_equal(attr, other) click to toggle source

@param attr [Symbol] @param other [Echowrap::Base] @return [Boolean]

# File lib/echowrap/base.rb, line 68
def attr_equal(attr, other)
  self.class == other.class && !other.send(attr).nil? && send(attr) == other.send(attr)
end
attrs_equal(other) click to toggle source

@param other [Echowrap::Base] @return [Boolean]

# File lib/echowrap/base.rb, line 74
def attrs_equal(other)
  self.class == other.class && !other.attrs.empty? && attrs == other.attrs
end
map_collection(klass, key) click to toggle source

@param klass [Class] @param key [Symbol] @return [Array]

# File lib/echowrap/base.rb, line 81
def map_collection(klass, key)
  Array(@attrs[key.to_sym]).map do |entity|
    klass.new(entity)
  end
end