Sha256: 1eab4b3b525157bfec4f70738ace5a6c3e2cd39b5e6b9afcbd367119c28be4d9

Contents?: true

Size: 674 Bytes

Versions: 2

Compression:

Stored size: 674 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
        method = Invokr.query_method klass.instance_method :initialize
        method.invoke method: :new, with: self
      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

2 entries across 2 versions & 1 rubygems

Version Path
invokr-0.0.6 lib/invokr/dependency_injection.rb
invokr-0.0.5 lib/invokr/dependency_injection.rb