Sha256: 596d9b930c42e5211c6834e801f59f662f291363c163969fc6361b356838d165

Contents?: true

Size: 680 Bytes

Versions: 3

Compression:

Stored size: 680 Bytes

Contents

module Invokr
  module DependencyInjection
    def self.inject args = {}
      klass = args.fetch :klass
      resolver = args.fetch :using
      injector = Injector.new resolver, klass
      injector.inject
    end

    Injector = Struct.new :resolver, :klass do
      def inject
        invocation = Builder.build initializer, self, nil
        invocation.method = :new
        invocation.invoke! klass
      end

      def keys
        initializer.parameters.map { |_, identifier| identifier }
      end

      def fetch arg, &default
        resolver.resolve arg, &default
      end

      def initializer
        klass.instance_method :initialize
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
invokr-0.0.4 lib/invokr/dependency_injection.rb
invokr-0.0.2 lib/invokr/dependency_injection.rb
invokr-0.0.1 lib/invokr/dependency_injection.rb