Module: Objectmancy::Objectable

Defined in:
lib/objectmancy/objectable.rb

Overview

Mixin for allowing your objects to take a Hash and turn them into an object.

Examples:

Including Objectable

class Kitten
  include Objectmancy::Objectable

  attribute :name
end

tabby = Kitten.new(name: 'Eddy')
tabby.name # => "Eddy"

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Comparator for two objects


49
50
51
52
53
54
# File 'lib/objectmancy/objectable.rb', line 49

def ==(other)
  self.class == other.class &&
    self.class.registered_attributes.keys.all? do |attr|
      send(attr) == other.send(attr)
    end
end

#initialize(attrs = {}) ⇒ Object

Creates your object. You should use the after_initialize


25
26
27
28
29
30
31
# File 'lib/objectmancy/objectable.rb', line 25

def initialize(attrs = {})
  before_initialize

  _attributes_update!(attrs)

  after_initialize
end

#mass_update(attrs = {}) ⇒ Object

Updates the attributes of the object


36
37
38
39
40
41
42
# File 'lib/objectmancy/objectable.rb', line 36

def mass_update(attrs = {})
  tap do
    before_mass_update
    _attributes_update!(attrs)
    after_mass_update
  end
end