Sha256: 8405a4348d76816f36a100205835de272fff7807f62c8688be135c84504a8d6a

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

require "dry/auto_inject"

class Trailblazer::Operation
  # Thanks, @timriley! <3
  # https://gist.github.com/timriley/d314a58da9784912159006e208ba8ea9
  module AutoInject
    class InjectStrategy < Module
      ClassMethods = Class.new(Module)

      attr_reader :container
      attr_reader :dependency_map
      attr_reader :class_mod

      def initialize(container, *dependency_names)
        @container = container
        @dependency_map = Dry::AutoInject::DependencyMap.new(*dependency_names)
        @class_mod = ClassMethods.new
      end

      def included(klass)
        define_call

        klass.singleton_class.prepend @class_mod

        super
      end

      private

      def define_call
        class_mod.class_exec(container, dependency_map) do |container, dependency_map|
          define_method :call do |ctx={}, *dependencies|
            ctx_with_deps = dependency_map.to_h.each_with_object({}) { |(name, identifier), obj|
              obj[name] = ctx[name] || container[identifier]
            }.merge(ctx)

            super(ctx_with_deps, *dependencies)
          end
        end
      end
    end
  end

  def self.AutoInject(container)
    Dry::AutoInject(container, strategies: {default: AutoInject::InjectStrategy})
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trailblazer-2.1.0.beta5 lib/trailblazer/operation/auto_inject.rb
trailblazer-2.1.0.beta4 lib/trailblazer/operation/auto_inject.rb