Module: Appfuel::Handler::InjectDsl
- Included in:
- Base
- Defined in:
- lib/appfuel/handler/inject_dsl.rb
Overview
Allow handlers to inject domains, commands, repositories or anything that was initialized in the app container. Injections are done in two steps, first, when the class is loaded in memory the declarations are converted into fully qualified container keys and second, when the handler is run they plucked from the app container into a container dedicated to that one execution.
Constant Summary collapse
- TYPES =
[:domain, :cmd, :repo, :container]
Instance Method Summary collapse
-
#inject(type, key, opts = {}) ⇒ Object
Dsl to declare a dependency injection.
-
#injections ⇒ Hash
Holds a dictionary where the key is the container key and the value is an an optional alias for the name of the injection to be saved as.
- #resolve_dependencies(container = Dry::Container.new) ⇒ Object
Instance Method Details
#inject(type, key, opts = {}) ⇒ Object
Dsl to declare a dependency injection. You can inject one of four types, which are:
domain: these are domains or value object
cmd: commands to be run
repo: repositories to query the persistence layer
container: any initialized container item
since names an collide you can rename your injection with the :as option.
inject :domain, 'member.user'
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/appfuel/handler/inject_dsl.rb', line 47 def inject(type, key, opts = {}) unless inject_type?(type) fail "inject type must be #{TYPES.join(",")} #{type} given" end cat = case type when :repo then 'repositories' when :cmd then 'commands' when :domain then 'domains' else "container" end no_context = false if opts.key?(:no_context) no_context = !!opts.delete(:no_context) end namespaced_key = qualify_container_key(key, cat, no_context: no_context) injections[namespaced_key] = opts[:as] nil end |
#injections ⇒ Hash
Holds a dictionary where the key is the container key and the value is an an optional alias for the name of the injection to be saved as. Injections a separated into two categories because domains are part of the type system (Dry::Types) and therefore kept in its own container “Types”, meaning are fetched differently.
19 20 21 |
# File 'lib/appfuel/handler/inject_dsl.rb', line 19 def injections @injections ||= {} end |
#resolve_dependencies(container = Dry::Container.new) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/appfuel/handler/inject_dsl.rb', line 70 def resolve_dependencies(container = Dry::Container.new) app_container = Appfuel.app_container injections.each do |key, alias_name| unless app_container.key?(key) fail "Could not inject (#{key}): not registered in app container" end basename = key.split('.').last item = app_container[key] container_key = alias_name || basename container.register(container_key, item) end end |