Class: Hanami::Entity
- Inherits:
-
Object
- Object
- Hanami::Entity
- Defined in:
- lib/hanami/entity.rb,
lib/hanami/entity/schema.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.
If we expand the code above in **pure Ruby**, it would be:
Hanami::Model ships ‘Hanami::Entity` for developers’ convenience.
Hanami::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 Hanami::Model framework.
However, we suggest to implement this interface by including ‘Hanami::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, Types Classes: Schema
Class Method Summary collapse
- .inherited(klass) ⇒ Object private
Instance Method Summary collapse
-
#==(other) ⇒ FalseClass, TrueClass
Implement generic equality for entities.
-
#freeze ⇒ Object
Freeze the entity.
-
#hash ⇒ Integer
Implement predictable hashing for hash equality.
-
#id ⇒ Object, NilClass
Entity ID.
-
#initialize(attributes = nil) ⇒ Hanami::Entity
constructor
Instantiate a new entity.
-
#method_missing(method_name) ⇒ Object
Handle dynamic accessors.
-
#to_h ⇒ Hash
(also: #to_hash)
Serialize entity to a Hash.
Constructor Details
#initialize(attributes = nil) ⇒ Hanami::Entity
Instantiate a new entity
126 127 128 129 |
# File 'lib/hanami/entity.rb', line 126 def initialize(attributes = nil) @attributes = self.class.schema[attributes] freeze end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object
Handle dynamic accessors
If internal attributes set has the requested key, it returns the linked value, otherwise it raises a NoMethodError
146 147 148 149 |
# File 'lib/hanami/entity.rb', line 146 def method_missing(method_name, *) attribute?(method_name) or super attributes.fetch(method_name, nil) end |
Class Method Details
.inherited(klass) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
110 111 112 113 114 115 |
# File 'lib/hanami/entity.rb', line 110 def self.inherited(klass) klass.class_eval do @schema = Schema.new extend ClassMethods end end |
Instance Method Details
#==(other) ⇒ FalseClass, TrueClass
Implement generic equality for entities
Two entities are equal if they are instances of the same class and they have the same id.
161 162 163 164 |
# File 'lib/hanami/entity.rb', line 161 def ==(other) self.class == other.class && id == other.id end |
#freeze ⇒ Object
Freeze the entity
178 179 180 181 |
# File 'lib/hanami/entity.rb', line 178 def freeze attributes.freeze super end |
#hash ⇒ Integer
Implement predictable hashing for hash equality
171 172 173 |
# File 'lib/hanami/entity.rb', line 171 def hash [self.class, id].hash end |
#id ⇒ Object, NilClass
Entity ID
136 137 138 |
# File 'lib/hanami/entity.rb', line 136 def id attributes.fetch(:id, nil) end |
#to_h ⇒ Hash Also known as: to_hash
Serialize entity to a Hash
188 189 190 |
# File 'lib/hanami/entity.rb', line 188 def to_h Utils::Hash.deep_dup(attributes) end |