Module: Hanami::Model

Defined in:
lib/hanami/model.rb,
lib/hanami/model/sql.rb,
lib/hanami/model/error.rb,
lib/hanami/model/types.rb,
lib/hanami/model/mapping.rb,
lib/hanami/model/plugins.rb,
lib/hanami/model/version.rb,
lib/hanami/model/migrator.rb,
lib/hanami/model/migration.rb,
lib/hanami/model/sql/types.rb,
lib/hanami/model/association.rb,
lib/hanami/model/entity_name.rb,
lib/hanami/model/sql/console.rb,
lib/hanami/model/configurator.rb,
lib/hanami/model/configuration.rb,
lib/hanami/model/relation_name.rb,
lib/hanami/model/plugins/schema.rb,
lib/hanami/model/mapped_relation.rb,
lib/hanami/model/migrator/logger.rb,
lib/hanami/model/plugins/mapping.rb,
lib/hanami/model/associations/dsl.rb,
lib/hanami/model/migrator/adapter.rb,
lib/hanami/model/sql/entity/schema.rb,
lib/hanami/model/plugins/timestamps.rb,
lib/hanami/model/sql/consoles/mysql.rb,
lib/hanami/model/migrator/connection.rb,
lib/hanami/model/sql/consoles/sqlite.rb,
lib/hanami/model/associations/has_one.rb,
lib/hanami/model/associations/has_many.rb,
lib/hanami/model/sql/consoles/abstract.rb,
lib/hanami/model/migrator/mysql_adapter.rb,
lib/hanami/model/associations/belongs_to.rb,
lib/hanami/model/migrator/sqlite_adapter.rb,
lib/hanami/model/sql/consoles/postgresql.rb,
lib/hanami/model/associations/many_to_many.rb,
lib/hanami/model/migrator/postgres_adapter.rb,
lib/hanami/model/sql/types/schema/coercions.rb

Overview

Hanami::Model migrations

Since:

  • 0.1.0

Defined Under Namespace

Modules: Associations, Plugins, Sql, Types Classes: Association, CheckConstraintViolationError, Configuration, Configurator, ConstraintViolationError, DatabaseError, EntityName, Error, ForeignKeyConstraintViolationError, InvalidCommandError, MappedRelation, Mapping, Migration, MigrationError, Migrator, MissingPrimaryKeyError, NotNullConstraintViolationError, RelationName, UniqueConstraintViolationError, UnknownAttributeError, UnknownDatabaseAdapterError, UnknownDatabaseTypeError

Constant Summary collapse

VERSION =

Defines the version

Since:

  • 0.1.0

"1.3.3"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

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



30
31
32
# File 'lib/hanami/model.rb', line 30

def config
  @config
end

.loadedObject (readonly) Also known as: loaded?

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



34
35
36
# File 'lib/hanami/model.rb', line 34

def loaded
  @loaded
end

Class Method Details

.configurationObject

Current configuration

Since:

  • 0.1.0



62
63
64
# File 'lib/hanami/model.rb', line 62

def self.configuration
  @configuration ||= Configuration.new(config)
end

.configure(&block) ⇒ Object

Configure the framework

Examples:

require 'hanami/model'

Hanami::Model.configure do
  adapter :sql, ENV['DATABASE_URL']

  migrations 'db/migrations'
  schema     'db/schema.sql'
end

Since:

  • 0.1.0



54
55
56
57
# File 'lib/hanami/model.rb', line 54

def self.configure(&block)
  @config = Configurator.build(&block)
  self
end

.containerObject

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



74
75
76
77
78
# File 'lib/hanami/model.rb', line 74

def self.container
  raise "Not loaded" unless loaded?

  @container
end

.disconnectObject

Disconnect from the database

This is useful for rebooting applications in production and to ensure that the framework prunes stale connections.

Examples:

With Full Stack Hanami Project

# config/puma.rb
# ...
on_worker_boot do
  Hanami.boot
end

With Standalone Hanami::Model

# config/puma.rb
# ...
on_worker_boot do
  Hanami::Model.disconnect
  Hanami::Model.load!
end

Since:

  • 1.0.0



107
108
109
# File 'lib/hanami/model.rb', line 107

def self.disconnect
  configuration.connection&.disconnect
end

.load!(&blk) ⇒ Object

Since:

  • 0.1.0



81
82
83
84
# File 'lib/hanami/model.rb', line 81

def self.load!(&blk)
  @container = configuration.load!(repositories, &blk)
  @loaded    = true
end

.migration(&blk) ⇒ Object

Define a migration

It must define an up/down strategy to write schema changes (up) and to rollback them (down).

We can use up and down blocks for custom strategies, or only one change block that automatically implements “down” strategy.

Examples:

Use up/down blocks

Hanami::Model.migration do
  up do
    create_table :books do
      primary_key :id
      column :book, String
    end
  end

  down do
    drop_table :books
  end
end

Use change block

Hanami::Model.migration do
  change do
    create_table :books do
      primary_key :id
      column :book, String
    end
  end

  # DOWN strategy is automatically generated
end

Parameters:

  • blk (Proc)

    a block that defines up/down or change database migration

Since:

  • 0.4.0



50
51
52
# File 'lib/hanami/model/sql.rb', line 50

def self.migration(&blk)
  Migration.new(configuration.gateways[:default], &blk)
end

.repositoriesObject

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



68
69
70
# File 'lib/hanami/model.rb', line 68

def self.repositories
  @__repositories__
end