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.
Defined Under Namespace
Modules: ClassMethods, Timestamp
Class Method Summary collapse
-
.included(base) ⇒ Object
Inject the public API into the hosting class.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Overrides the equality Ruby operator.
-
#initialize(attributes = {}) ⇒ Object
Defines a generic, inefficient initializer, in case that the attributes weren’t explicitly defined with ‘.attributes=`.
Class Method Details
.included(base) ⇒ Object
Inject the public API into the hosting class.
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.
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=`.
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 |