Sha256: 90ae6684e0670a722c675fc7606b17c6577caa6eb17d00c9a9208f8fa71e10b5

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require 'dry/auto_inject/dependency_map'

module Dry
  module AutoInject
    class Strategies
      class Constructor < Module
        ClassMethods = Class.new(Module)
        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
        end

        # @api private
        def included(klass)
          define_readers

          define_new
          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
          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

5 entries across 5 versions & 1 rubygems

Version Path
dry-auto_inject-0.7.0 lib/dry/auto_inject/strategies/constructor.rb
dry-auto_inject-0.6.1 lib/dry/auto_inject/strategies/constructor.rb
dry-auto_inject-0.6.0 lib/dry/auto_inject/strategies/constructor.rb
dry-auto_inject-0.5.0 lib/dry/auto_inject/strategies/constructor.rb
dry-auto_inject-0.4.6 lib/dry/auto_inject/strategies/constructor.rb