Hello!
I had an idea on how to add semantics to the importer.
When I want to import something on my code-base I have to do something like this :
include AppName::Import[servers: 'domain.organization.server_repository']
This allows too easily to access objects from another domain.
I just have to change the organization
by something else to access the server_repository
of my domain.
I had the idea to namespace the import with a proxy :
# frozen_string_literal: true
class InjectorNamespaced
# @param [Dry::AutoInject::Builder] builder
# @param [String] default_namespace
def initialize(builder, default_namespace)
@builder = builder
@default_namespace = default_namespace
end
def [](*dependency_names)
dependency_names
.map { |dependency| handle_dependency(dependency) }
.then { |names| @builder[*names] }
end
# rubocop:disable Style/OptionalBooleanParameter
def respond_to?(name, include_private = false)
@builder.respond_to?(name, include_private)
end
# rubocop:enable Style/OptionalBooleanParameter
private
def handle_dependency(dependency)
case dependency
when String
@default_namespace + dependency
when Hash
dependency.transform_values { |name| @default_namespace + name }
else
raise :invalid_type
end
end
end
Thank this, I can specify on with domain my object belongs.
I define an importer object
# frozen_string_literal: true
require_relative 'lib/injector_namespaced'
module Importers
Application = InjectorNamespaced.new(AppName::Import, 'application.')
Infrastructure = InjectorNamespaced.new(AppName::Import, 'infrastructure.')
module Domain
Organization = InjectorNamespaced.new(AppName::Import, 'domain.organization.')
end
end
And to simplify usage I alias them
module Domain
module Organization
Importer = Importers::Domain::Organization
end
end
Get back to the initial line, I now can do
include Domain::Organization::Importer[servers: 'server_repository']
This enforces the fact that this importer can only load an object from the domain organization.
As my class is inside the module Domain::Organization
I can omit this information
include Importer[servers: 'server_repository']