Module: Lotus::Entity
- Defined in:
- lib/lotus/entity.rb,
lib/lotus/entity/dirty_tracking.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, DirtyTracking
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 85 86 |
# File 'lib/lotus/entity.rb', line 81 def self.included(base) base.class_eval do extend ClassMethods attributes :id end 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.
207 208 209 210 |
# File 'lib/lotus/entity.rb', line 207 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=`.
194 195 196 197 198 199 |
# File 'lib/lotus/entity.rb', line 194 def initialize(attributes = {}) attributes.each do |k, v| setter = "#{ k }=" public_send(setter, v) if respond_to?(setter) end end |
#to_h ⇒ Object
Return the hash of attributes
225 226 227 |
# File 'lib/lotus/entity.rb', line 225 def to_h Hash[self.class.attributes.map { |a| [a, public_send(a)] }] end |
#update(attributes = {}) ⇒ Object
Set attributes for entity
243 244 245 246 247 |
# File 'lib/lotus/entity.rb', line 243 def update(attributes={}) attributes.each do |attribute, value| public_send("#{attribute}=", value) end end |