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"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



18
19
20
# File 'lib/objectmancy/objectable.rb', line 18

def self.included(base)
  base.extend(CommonClassMethods)
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Comparator for two objects

Parameters:

  • other (Object)

    to be compared to

Returns:

  • (TrueClass, FalseClass)

    Boolean indicating if the two objects are equal.



45
46
47
48
49
50
# File 'lib/objectmancy/objectable.rb', line 45

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

Parameters:

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

    Hash of attributes to create the object with



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

Parameters:

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

    Attributes to update



36
37
38
# File 'lib/objectmancy/objectable.rb', line 36

def mass_update(attrs = {})
  _attributes_update!(attrs)
end