Class: Hanami::Entity::Schema::Definition::Dsl 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.

Schema DSL

Since:

  • 0.7.0

Constant Summary collapse

TYPES =

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

Since:

  • 1.1.0

%i[schema strict weak permissive strict_with_defaults symbolized].freeze
DEFAULT_TYPE =

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

Since:

  • 1.1.0

TYPES.first

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ Dsl

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.

Returns a new instance of Dsl.

Since:

  • 0.7.0


116
117
118
119
# File 'lib/hanami/entity/schema.rb', line 116

def initialize(&blk)
  @attributes = {}
  instance_eval(&blk)
end

Class Method Details

.build(type, &blk) ⇒ 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.

Raises:

Since:

  • 0.7.0


106
107
108
109
110
111
112
# File 'lib/hanami/entity/schema.rb', line 106

def self.build(type, &blk)
  type ||= DEFAULT_TYPE
  raise Hanami::Model::Error.new("Unknown schema type: `#{type.inspect}'") unless TYPES.include?(type)

  attributes = new(&blk).to_h
  [attributes, Hanami::Model::Types::Coercible::Hash.__send__(type, attributes)]
end

Instance Method Details

#attribute(name, type) ⇒ 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.

Define an attribute

Examples:

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

Parameters:

  • name (Symbol)

    the attribute name

  • type (Dry::Types::Definition)

    the attribute type

Since:

  • 0.7.0


147
148
149
# File 'lib/hanami/entity/schema.rb', line 147

def attribute(name, type)
  @attributes[name] = type
end

#to_hObject

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.

Since:

  • 0.7.0


153
154
155
# File 'lib/hanami/entity/schema.rb', line 153

def to_h
  @attributes
end