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


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

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


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

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


18
19
20
# File 'lib/hanami/model/configuration.rb', line 18

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


22
23
24
# File 'lib/hanami/model/configuration.rb', line 22

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


14
15
16
# File 'lib/hanami/model/configuration.rb', line 14

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


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

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


46
47
48
# File 'lib/hanami/model/configuration.rb', line 46

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


120
121
122
# File 'lib/hanami/model/configuration.rb', line 120

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


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

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


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

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


94
95
96
# File 'lib/hanami/model/configuration.rb', line 94

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


66
67
68
# File 'lib/hanami/model/configuration.rb', line 66

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


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

def load!(repositories, &blk) # rubocop:disable Metrics/AbcSize
  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 => e
  raise Hanami::Model::Error.for(e)
end

#migrationsObject

Migrations directory

Since:

  • 0.4.0


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

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


100
101
102
103
# File 'lib/hanami/model/configuration.rb', line 100

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


176
177
178
# File 'lib/hanami/model/configuration.rb', line 176

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


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

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

  raise e
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


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

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

#schemaObject

Path for schema dump file

Since:

  • 0.4.0


88
89
90
# File 'lib/hanami/model/configuration.rb', line 88

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