Module: Lotus::Entity
- Defined in:
- lib/lotus/entity.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 expresses 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 ‘Lotus::Entity` it receives the following interface:
* #id
* #id=
* #initialize(attributes = {})
‘Lotus::Entity` also provides the `.attributes=` for defining attribute accessors for the given names.
If we expand the code above in **pure Ruby**, it would be:
Lotus::Model ships ‘Lotus::Entity` for developers’s convenience.
Lotus::Model depends on a narrow and well-defined interface for an Entity - ‘#id`, `#id=`, `#initialize(attributes={})`.If your object implements that interface then that object can be used as an Entity in the Lotus::Model framework.
However, we suggest to implement this interface by including ‘Lotus::Entity`, in case that future versions of the framework will expand it.
See Dependency Inversion Principle for more on interfaces.
Defined Under Namespace
Modules: ClassMethods
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=`.
-
#to_h ⇒ Object
Return the hash of attributes.
-
#update(attributes = {}) ⇒ Object
Set attributes for entity.
Class Method Details
.included(base) ⇒ Object
Inject the public API into the hosting class.
81 82 83 84 |
# File 'lib/lotus/entity.rb', line 81 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.
190 191 192 193 |
# File 'lib/lotus/entity.rb', line 190 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=`.
178 179 180 181 182 |
# File 'lib/lotus/entity.rb', line 178 def initialize(attributes = {}) attributes.each do |k, v| public_send("#{ k }=", v) end end |
#to_h ⇒ Object
Return the hash of attributes
208 209 210 |
# File 'lib/lotus/entity.rb', line 208 def to_h Hash[self.class.attributes.map { |a| [a, public_send(a)] }] end |
#update(attributes = {}) ⇒ Object
Set attributes for entity
226 227 228 229 230 |
# File 'lib/lotus/entity.rb', line 226 def update(attributes={}) attributes.each do |attribute, value| public_send("#{attribute}=", value) end end |