lib/hanami/model/configuration.rb in hanami-model-0.7.0 vs lib/hanami/model/configuration.rb in hanami-model-1.0.0.beta1
- old
+ new
@@ -6,36 +6,46 @@
#
# Hanami::Model has its own global configuration that can be manipulated
# via `Hanami::Model.configure`.
#
# @since 0.2.0
- class Configuration < ROM::Configuration
+ class Configuration
# @since 0.7.0
# @api private
attr_reader :mappings
# @since 0.7.0
# @api private
attr_reader :entities
+ # @since 1.0.0.beta1
+ # @api private
+ attr_reader :logger
+
+ # @since 1.0.0.beta1
+ # @api private
+ attr_reader :migrations_logger
+
# @since 0.2.0
# @api private
def initialize(configurator)
- super(configurator.backend, configurator.url)
- @migrations = configurator._migrations
- @schema = configurator._schema
- @mappings = {}
- @entities = {}
+ @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
# NOTE: This must be changed when we want to support several adapters at the time
#
# @since 0.7.0
# @api private
- def url
- connection.url
- end
+ attr_reader :url
# NOTE: This must be changed when we want to support several adapters at the time
#
# @since 0.7.0
# @api private
@@ -46,11 +56,11 @@
# NOTE: This must be changed when we want to support several adapters at the time
#
# @since 0.7.0
# @api private
def gateway
- environment.gateways[:default]
+ gateways[:default]
end
# Root directory
#
# @since 0.4.0
@@ -94,9 +104,54 @@
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
+
+ # @since 1.0.0.beta1
+ # @api private
+ def configure_gateway
+ @gateway_config.call(gateway) unless @gateway_config.nil?
+ end
+
+ # @since 1.0.0.beta1
+ # @api private
+ def logger=(value)
+ return if value.nil?
+ gateway.use_logger(@logger = value)
+ end
+
+ # @since 1.0.0.beta1
+ # @api private
+ def rom
+ @rom ||= ROM::Configuration.new(@backend, @url, infer_relations: false)
+ end
+
+ # @since 1.0.0.beta1
+ # @api private
+ 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
+
+ # @since 1.0.0.beta1
+ # @api private
+ def method_missing(method_name, *args, &blk)
+ if rom.respond_to?(method_name)
+ rom.__send__(method_name, *args, &blk)
+ else
+ super
end
end
end
end
end