Sha256: 379fd60a6af91a4afebd3ad5b825b684893563a34d265cbb5eece88b0d686357

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

module Invokr
  module DependencyInjection
    extend self

    def inject obj, using
      meth = case obj
             when Proc then :inject_proc
             when Class then :inject_klass
             else raise ArgumentError, "can't inject #{obj.inspect}"
             end
      resolver = build_resolver using
      send meth, obj, resolver
    end

    private

    def build_resolver using
      if using.is_a? Hash
        HashResolver.new using
      else
        using
      end
    end

    def inject_klass klass, resolver
      injector = KlassInjector.new resolver, klass
      injector.inject
    end

    def inject_proc proc, resolver
      injector = ProcInjector.new resolver, proc
      injector.inject
    end

    Injector = Struct.new :resolver, :obj do
      def keys
        method.parameters.map { |_, identifier| identifier }
      end

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

      def has_key? arg
        resolver.could_resolve? arg
      end
    end

    class KlassInjector < Injector
      def inject
        _method = Invokr.query_method method
        _method.invoke :method => :new, :with => self
      end

      def method
        obj.instance_method :initialize
      end
    end

    class ProcInjector < Injector
      def inject
        Invokr.invoke :proc => obj, :with => self
      end

      def method
        obj
      end
    end

    class HashResolver
      def initialize hsh
        @hsh = hsh
      end

      def inject klass
        DependencyInjection.inject(
          :klass => klass,
          :using => self,
        )
      end

      def resolve val, &block
        @hsh.fetch val, &block
      end

      def could_resolve? val
        @hsh.has_key? val
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
invokr-0.9.2 lib/invokr/dependency_injection.rb