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/version.rb,
lib/hanami/model/mapping.rb,
lib/hanami/model/plugins.rb,
lib/hanami/model/migrator.rb,
lib/hanami/model/migration.rb,
lib/hanami/model/sql/types.rb,
lib/hanami/model/entity_name.rb,
lib/hanami/model/association.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/plugins/mapping.rb,
lib/hanami/model/migrator/adapter.rb,
lib/hanami/model/associations/dsl.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_many.rb,
lib/hanami/model/sql/consoles/abstract.rb,
lib/hanami/model/migrator/mysql_adapter.rb,
lib/hanami/model/migrator/sqlite_adapter.rb,
lib/hanami/model/associations/belongs_to.rb,
lib/hanami/model/sql/consoles/postgresql.rb,
lib/hanami/model/migrator/postgres_adapter.rb,
lib/hanami/model/sql/types/schema/coercions.rb

Overview

Hanami::Model migrations

Defined Under Namespace

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

Constant Summary collapse

VERSION =

Defines the version

Since:

  • 0.1.0

'0.7.0'.freeze

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


25
26
27
# File 'lib/hanami/model.rb', line 25

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


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

def loaded
  @loaded
end

Class Method Details

.configurationObject

Current configuration

Since:

  • 0.1.0


57
58
59
# File 'lib/hanami/model.rb', line 57

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


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

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


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

def self.container
  raise 'Not loaded' unless loaded?
  @container
end

.load!(&blk) ⇒ Object

Since:

  • 0.1.0


75
76
77
78
79
80
81
82
83
84
# File 'lib/hanami/model.rb', line 75

def self.load!(&blk) # rubocop:disable Metrics/AbcSize
  configuration.setup.auto_registration(config.directory.to_s) unless config.directory.nil?
  configuration.instance_eval(&blk)                            if     block_given?
  repositories.each(&:load!)

  @container = ROM.container(configuration)
  configuration.define_entities_mappings(@container, repositories)

  @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


48
49
50
# File 'lib/hanami/model/sql.rb', line 48

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


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

def self.repositories
  @__repositories__
end