Class: Hanami::Entity

Inherits:
Object
  • Object
show all
Defined in:
gems/gems/hanami-model-1.3.0/lib/hanami/entity.rb,
gems/gems/hanami-model-1.3.0/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.

Examples:

With Hanami::Entity

require 'hanami/model'

class Person < Hanami::Entity
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: Types

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ Hanami::Entity

Instantiate a new entity

Parameters:

  • attributes (Hash, #to_h, NilClass) (defaults to: nil)

    data to initialize the entity

Raises:

  • (TypeError)

    if the given attributes are invalid

Since:

  • 0.1.0

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

Since:

  • 0.7.0

def method_missing(method_name, *)
  attribute?(method_name) or super
  attributes.fetch(method_name, nil)
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.

Parameters:

  • other (Object)

    the object of comparison

Returns:

  • (FalseClass, TrueClass)

    the result of the check

Since:

  • 0.1.0

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

#freezeObject

Freeze the entity

Since:

  • 0.7.0

def freeze
  attributes.freeze
  super
end

#hashInteger

Implement predictable hashing for hash equality

Returns:

  • (Integer)

    the object hash

Since:

  • 0.7.0

def hash
  [self.class, id].hash
end

#idObject, NilClass

Entity ID

Returns:

  • (Object, NilClass)

    the ID, if present

Since:

  • 0.7.0

def id
  attributes.fetch(:id, nil)
end

#to_hHash Also known as: to_hash

Serialize entity to a Hash

Returns:

  • (Hash)

    the result of serialization

Since:

  • 0.1.0

def to_h
  Utils::Hash.deep_dup(attributes)
end