Sha256: fccbb653a8ba76fbbe1991a711a7348f7a81c689288b0dfb7a3510d5b5b075d2

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

require 'dry/auto_inject/dependency_map'

module Dry
  module AutoInject
    class Strategies
      class Constructor < Module
        ClassMethods = Class.new(Module) do
          def initialize(container)
            super()
            define_method(:container) { container }
          end
        end

        InstanceMethods = Class.new(Module)

        attr_reader :container
        attr_reader :dependency_map
        attr_reader :instance_mod
        attr_reader :class_mod

        def initialize(container, *dependency_names)
          @container = container
          @dependency_map = DependencyMap.new(*dependency_names)
          @instance_mod = InstanceMethods.new
          @class_mod = ClassMethods.new(container)
        end

        # @api private
        def included(klass)
          define_readers

          define_new(klass)
          define_initialize(klass)

          klass.send(:include, instance_mod)
          klass.extend(class_mod)

          super
        end

        private

        def define_readers
          instance_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
            attr_reader #{dependency_map.names.map { |name| ":#{name}" }.join(', ')}
          RUBY
          self
        end

        def define_new(klass)
          raise NotImplementedError, "must be implemented by a subclass"
        end

        def define_initialize(klass)
          raise NotImplementedError, "must be implemented by a subclass"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-auto_inject-0.4.1 lib/dry/auto_inject/strategies/constructor.rb
dry-auto_inject-0.4.0 lib/dry/auto_inject/strategies/constructor.rb
dry-auto_inject-0.3.0 lib/dry/auto_inject/strategies/constructor.rb