An entity is domain object that is defined by its identity.

See “Domain Driven Design” by Eric Evans.

An entity is at 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.

Entity Schema

Internally, an entity holds a schema of the attributes, made of their names and types. The role of a schema is to whitelist the data used during the initialization, and to enforce data integrity via coercions or exceptions.

We’ll see concrete examples in a second.

Automatic Schema

When using a SQL database, this is derived automatically from the table definition.

Imagine to have the books table defined as:

  1. CREATE TABLE books (
  2. id integer NOT NULL,
  3. title text,
  4. created_at timestamp without time zone,
  5. updated_at timestamp without time zone
  6. );

This is the corresponding entity Book.

  1. # lib/bookshelf/entities/book.rb
  2. class Book < Hanami::Entity
  3. end

Let’s instantiate it with proper values:

  1. book = Book.new(title: "Hanami")
  2. book.title # => "Hanami"
  3. book.created_at # => nil

The created_at attribute is nil because it wasn’t present when we have instantiated book.


It ignores unknown attributes:

  1. book = Book.new(unknown: "value")
  2. book.unknown # => NoMethodError
  3. book.foo # => NoMethodError

It raises a NoMethodError both for unknown and foo, because they aren’t part of the internal schema.


It can coerce values:

  1. book = Book.new(created_at: "Sun, 13 Nov 2016 09:41:09 GMT")
  2. book.created_at # => 2016-11-13 09:41:09 UTC
  3. book.created_at.class # => Time

An entity tries as much as possible to coerce values according to the internal schema.


It enforces data integrity via exceptions:

  1. Book.new(created_at: "foo") # => ArgumentError

If we use this feature, in combination with database constraints and validations, we can guarantee a strong level of data integrity for our projects.

You can set your own set of attributes via custom schema.