Module: Epiphy::Entity

Defined in:
lib/epiphy/entity.rb,
lib/epiphy/entity/timestamp.rb

Overview

An object that is defined by its identity. See Domain Driven Design by Eric Evans.

An entity is the core of an application, where the part of the domain logic is implemented. It’s a small, cohesive object that express coherent and meaningful behaviors.

It deals with one and only one responsibility that is pertinent to the domain of the application, without caring about details such as persistence or validations.

This simplicity of design allows developers to focus on behaviors, or message passing if you will, which is the quintessence of Object Oriented Programming.

When a class includes ‘Epiphy::Entity` the `.attributes=` method is exposed. By then calling the `.attributes=` class method, the following methods are added:

* #id
* #id=
* #initialize(attributes = {})

If we expand the code above in pure Ruby, it would be:

Indeed, Epiphy::Model ships ‘Entity` only for developers’s convenience, but the rest of the framework is able to accept any object that implements the interface above.

However, we suggest to implement this interface by including ‘Epiphy::Entity`, in case that future versions of the framework will expand it.

Examples:

With Epiphy::Entity

require 'epiphy/model'

class Person
  include Epiphy::Entity
  self.attributes = :name, :age
end

Pure Ruby

class Person
  attr_accessor :id, :name, :age

  def initialize(attributes = {})
    @id, @name, @age = attributes.values_at(:id, :name, :age)
  end
end

See Also:

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods, Timestamp

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Inject the public API into the hosting class.

Examples:

With Object

require 'epiphy/model'

class User
  include Epiphy::Entity
end

With Struct

require 'epiphy/model'

User = Struct.new(:id, :name) do
  include Epiphy::Entity
end

Since:

  • 0.1.0



73
74
75
76
# File 'lib/epiphy/entity.rb', line 73

def self.included(base)
  base.extend ClassMethods
  base.send :attr_accessor, :id
end

Instance Method Details

#==(other) ⇒ Object

Overrides the equality Ruby operator

Two entities are considered equal if they are instances of the same class and if they have the same #id.

Since:

  • 0.1.0



152
153
154
155
# File 'lib/epiphy/entity.rb', line 152

def ==(other)
  self.class == other.class &&
     self.id == other.id
end

#initialize(attributes = {}) ⇒ Object

Defines a generic, inefficient initializer, in case that the attributes weren’t explicitly defined with ‘.attributes=`.

Raises:

  • NoMethodError in case the given attributes are trying to set unknown or private methods.

See Also:

  • attributes

Since:

  • 0.1.0



135
136
137
138
139
140
141
142
143
144
# File 'lib/epiphy/entity.rb', line 135

def initialize(attributes = {})
  attributes.each do |k, v|
    case k
      when Symbol
        public_send("#{ k }=", v)
      when String
        public_send("#{ k.to_sym }=", v)
    end
  end
end