Class: Hanami::Model::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/model/configuration.rb

Overview

Configuration for the framework, models and adapters.

Hanami::Model has its own global configuration that can be manipulated via ‘Hanami::Model.configure`.

Since:

  • 0.2.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configurator) ⇒ Configuration

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 Configuration.

Since:

  • 0.2.0



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hanami/model/configuration.rb', line 32

def initialize(configurator)
  @backend = configurator.backend
  @url = configurator.url
  @migrations = configurator._migrations
  @schema = configurator._schema
  @gateway_config = configurator._gateway
  @logger = configurator._logger
  @migrations_logger = configurator.migrations_logger
  @mappings = {}
  @entities = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &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.

Since:

  • 1.0.0



168
169
170
171
172
173
174
# File 'lib/hanami/model/configuration.rb', line 168

def method_missing(method_name, *args, &blk)
  if rom.respond_to?(method_name)
    rom.__send__(method_name, *args, &blk)
  else
    super
  end
end

Instance Attribute Details

#entitiesObject (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



20
21
22
# File 'lib/hanami/model/configuration.rb', line 20

def entities
  @entities
end

#loggerObject

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:

  • 1.0.0



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

def logger
  @logger
end

#mappingsObject (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



16
17
18
# File 'lib/hanami/model/configuration.rb', line 16

def mappings
  @mappings
end

#migrations_loggerObject (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:

  • 1.0.0



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

def migrations_logger
  @migrations_logger
end

#urlObject (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.

NOTE: This must be changed when we want to support several adapters at the time

Since:

  • 0.7.0



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

def url
  @url
end

Instance Method Details

#configure_gatewayObject

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:

  • 1.0.0



122
123
124
# File 'lib/hanami/model/configuration.rb', line 122

def configure_gateway
  @gateway_config&.call(gateway)
end

#connectionObject

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.

NOTE: This must be changed when we want to support several adapters at the time

Raises:

Since:

  • 0.7.0



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

def connection
  gateway.connection
end

#define_entities_mappings(container, repositories) ⇒ 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.

Since:

  • 0.7.0



109
110
111
112
113
114
115
116
117
118
# File 'lib/hanami/model/configuration.rb', line 109

def define_entities_mappings(container, repositories)
  return unless defined?(Sql::Entity::Schema)

  repositories.each do |r|
    relation = r.relation
    entity   = r.entity

    entity.schema = Sql::Entity::Schema.new(entities, container.relations[relation], mappings.fetch(relation))
  end
end

#define_mappings(root, &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.

Since:

  • 0.7.0



96
97
98
# File 'lib/hanami/model/configuration.rb', line 96

def define_mappings(root, &blk)
  @mappings[root] = Mapping.new(&blk)
end

#gatewayObject

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.

NOTE: This must be changed when we want to support several adapters at the time

Raises:

Since:

  • 0.7.0



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

def gateway
  gateways[:default]
end

#load!(repositories, &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:

  • 1.0.0



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/hanami/model/configuration.rb', line 152

def load!(repositories, &blk)
  rom.setup.auto_registration(config.directory.to_s) unless config.directory.nil?
  rom.instance_eval(&blk)                            if     block_given?
  configure_gateway
  repositories.each(&:load!)
  self.logger = logger

  container = ROM.container(rom)
  define_entities_mappings(container, repositories)
  container
rescue => exception
  raise Hanami::Model::Error.for(exception)
end

#migrationsObject

Migrations directory

Since:

  • 0.4.0



83
84
85
# File 'lib/hanami/model/configuration.rb', line 83

def migrations
  (@migrations.nil? ? root : root.join(@migrations)).realpath
end

#register_entity(plural, singular, klass) ⇒ 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.

Since:

  • 0.7.0



102
103
104
105
# File 'lib/hanami/model/configuration.rb', line 102

def register_entity(plural, singular, klass)
  @entities[plural]   = klass
  @entities[singular] = klass
end

#respond_to_missing?(method_name, include_all) ⇒ Boolean

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:

  • (Boolean)

Since:

  • 1.1.0



178
179
180
# File 'lib/hanami/model/configuration.rb', line 178

def respond_to_missing?(method_name, include_all)
  rom.respond_to?(method_name, include_all)
end

#romObject

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:

  • 1.0.0



139
140
141
142
143
144
145
# File 'lib/hanami/model/configuration.rb', line 139

def rom
  @rom ||= ROM::Configuration.new(@backend, @url, infer_relations: false)
rescue => exception
  raise UnknownDatabaseAdapterError.new(@url) if exception.message =~ /adapters/

  raise exception
end

#rootObject

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.

Root directory

Since:

  • 0.4.0



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

def root
  Hanami.respond_to?(:root) ? Hanami.root : Pathname.pwd
end

#schemaObject

Path for schema dump file

Since:

  • 0.4.0



90
91
92
# File 'lib/hanami/model/configuration.rb', line 90

def schema
  @schema.nil? ? root : root.join(@schema)
end