Class: Hanami::Entity::Schema Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/entity/schema.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Entity schema is a definition of a set of typed attributes.

Examples:

SQL Automatic Setup

require 'hanami/model'

 class Account < Hanami::Entity
 end

  = Account.new(name: "Acme Inc.")
 .name # => "Hanami"

  = Account.new(foo: "bar")
 .foo # => NoMethodError

Non-SQL Manual Setup

require 'hanami/model'

class Account < Hanami::Entity
  attributes do
    attribute :id,         Types::Int
    attribute :name,       Types::String
    attribute :codes,      Types::Array(Types::Int)
    attribute :users,      Types::Array(User)
    attribute :email,      Types::String.constrained(format: /@/)
    attribute :created_at, Types::DateTime
  end
end

 = Account.new(name: "Acme Inc.")
.name # => "Acme Inc."

 = Account.new(foo: "bar")
.foo # => NoMethodError

Schemaless Entity

require 'hanami/model'

class Account < Hanami::Entity
end

 = Account.new(name: "Acme Inc.")
.name # => "Acme Inc."

 = Account.new(foo: "bar")
.foo # => "bar"

Since:

  • 0.7.0

Direct Known Subclasses

Model::Sql::Entity::Schema

Defined Under Namespace

Classes: Definition, Schemaless

Instance Method Summary collapse

Constructor Details

#initialize(type = nil, &blk) ⇒ Hanami::Entity::Schema

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.

Build a new instance of Schema with the attributes defined by the given block

Parameters:

  • blk (Proc)

    the optional block that defines the attributes

Since:

  • 0.7.0


222
223
224
225
226
227
228
# File 'lib/hanami/entity/schema.rb', line 222

def initialize(type = nil, &blk)
  @schema = if block_given?
              Definition.new(type, &blk)
            else
              Schemaless.new
            end
end

Instance Method Details

#attribute?(name) ⇒ TrueClass, FalseClass

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.

Check if the attribute is known

Parameters:

  • name (Symbol)

    the attribute name

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.7.0


256
257
258
# File 'lib/hanami/entity/schema.rb', line 256

def attribute?(name)
  schema.attribute?(name)
end

#call(attributes) ⇒ Object Also known as: []

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.

Process attributes

Parameters:

  • attributes (#to_hash)

    the attributes hash

Raises:

  • (TypeError)

    if the process fails

Since:

  • 0.7.0


238
239
240
241
242
# File 'lib/hanami/entity/schema.rb', line 238

def call(attributes)
  Utils::Hash.deep_symbolize(
    schema.call(attributes)
  )
end