Class: Objectify::Injector
- Inherits:
-
Object
- Object
- Objectify::Injector
- Includes:
- Instrumentation
- Defined in:
- lib/objectify/injector.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
writeonly
Sets the attribute config.
Instance Method Summary collapse
- #call(object, method) ⇒ Object
-
#initialize(config) ⇒ Injector
constructor
A new instance of Injector.
Constructor Details
#initialize(config) ⇒ Injector
Returns a new instance of Injector.
9 10 11 12 |
# File 'lib/objectify/injector.rb', line 9 def initialize(config) @config = config @decoration_context = {} end |
Instance Attribute Details
#config=(value) ⇒ Object (writeonly)
Sets the attribute config
7 8 9 |
# File 'lib/objectify/injector.rb', line 7 def config=(value) @config = value end |
Instance Method Details
#call(object, method) ⇒ Object
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/objectify/injector.rb', line 14 def call(object, method) payload = {:object => object, :method => method} instrument("inject.objectify", payload) do |payload| namespace = object.respond_to?(:name) && object.name ? object.name.underscore.split("/")[0...-1].join("/") : nil method_obj = method_object(object, method) injectables = method_obj.parameters.map do |reqd, name| @decoration_context[[namespace, name].join("/").to_sym] || @decoration_context[name] || @config.get(name) if reqd == :req end.compact arguments = injectables.map do |type, value| if type == :unknown type, value = unknown_value_to_injectable(namespace, value) end if type == :resolver resolver_klass = [value, :resolver].join("_").camelize.constantize call(call(resolver_klass, :new), :call) elsif type == :implementation implementation_klass = value.to_s.camelize.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 result = object.send(method, *arguments) if method == :new base_name = object.name.underscore.to_sym add_decoration_context(base_name, result) result = @config.decorators(base_name).inject(result) do |memo, decorator| call(decorator.to_s.camelize.constantize, :new).tap do |decorated| add_decoration_context(base_name, decorated) end end clear_decoration_context end result end end |