Class: Echowrap::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/echowrap/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Echowrap::Base

Initializes a new object

Parameters:

  • attrs (Hash) (defaults to: {})


34
35
36
# File 'lib/echowrap/base.rb', line 34

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

Parameters:

  • attrs (Array, Set, Symbol)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


26
27
28
# File 'lib/echowrap/base.rb', line 26

def ==(other)
  super || attr_equal(:id, other) || attrs_equal(other)
end

#attrsHash Also known as: to_hash

Retrieve the attributes of an object

Returns:

  • (Hash)


41
42
43
# File 'lib/echowrap/base.rb', line 41

def attrs
  @attrs
end

#update(attrs) ⇒ Echowrap::Base

Update the attributes of an object

Parameters:

  • attrs (Hash)

Returns:



50
51
52
53
# File 'lib/echowrap/base.rb', line 50

def update(attrs)
  @attrs.update(attrs)
  self
end