Class: Objectify::Injector

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/objectify/injector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Injector

Returns a new instance of Injector.



9
10
11
# File 'lib/objectify/injector.rb', line 9

def initialize(config)
  @config = config
end

Instance Attribute Details

#config=(value) ⇒ Object (writeonly)

Sets the attribute config

Parameters:

  • value

    the value to set the attribute config to.



7
8
9
# File 'lib/objectify/injector.rb', line 7

def config=(value)
  @config = value
end

Instance Method Details

#call(object, method) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/objectify/injector.rb', line 13

def call(object, method)
  payload = {:object => object, :method => method}
  instrument("inject.objectify", payload) do |payload|
    method_obj           = method_object(object, method)
    injectables          = method_obj.parameters.map do |reqd, name|
      @config.get(name) if reqd == :req
    end.compact
    arguments            = injectables.map do |type, value|
      if type == :unknown
        type, value = unknown_value_to_injectable(value)
      end

      if type == :resolver
        resolver_klass = [value, :resolver].join("_").classify.constantize
        call(call(resolver_klass, :new), :call)
      elsif type == :implementation
        implementation_klass = value.to_s.classify.constantize
        call(implementation_klass, :new)
      elsif type == :value
        value
      else
        raise ArgumentError, "Unknown injectable type: #{type}."
      end
    end

    payload[:parameters]  = method_obj.parameters
    payload[:injectables] = injectables
    payload[:arguments]   = arguments

    object.send(method, *arguments)
  end
end