Sha256: a34c2a1ebe9c7c7ad3e7bb7a3359f325cf8598590023cee8151abc4c52d11e00

Contents?: true

Size: 975 Bytes

Versions: 1

Compression:

Stored size: 975 Bytes

Contents

module Dependor
  class AutoInjector

    def initialize(injector, search_modules)
      @injector = injector
      @instantiator = Instantiator.new(self)
      @class_name_resolver = ClassNameResolver.new(search_modules)
    end

    def get(name)
      return @injector.send(name)
    rescue ArgumentError
      return @injector.method(name).to_proc
    rescue NameError
      get_by_class_name(name)
    end

    def get_by_class_name(name)
      ensure_class_exists!(name)
      klass = @class_name_resolver.for_name(name)
      instantiator = @instantiator
      @injector.define_singleton_method(name) do
        instantiator.instantiate(klass)
      end
      @injector.send(name)
    end

    def class_exists?(name)
      !!@class_name_resolver.for_name(name)
    end

    private

    def ensure_class_exists!(name)
      unless class_exists?(name)
        raise UnknownObject.new("Injector does not know how to create object: #{name}")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dependor-1.0.0 lib/dependor/auto_injector.rb