Class: Poniard::Injector

Inherits:
Object
  • Object
show all
Defined in:
lib/poniard/injector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sources = []) ⇒ Injector

Returns a new instance of Injector.



7
8
9
10
11
12
13
14
15
# File 'lib/poniard/injector.rb', line 7

def initialize(sources = [])
  @sources = sources.map {|source|
    if source.is_a? Hash
      OpenStruct.new(source)
    else
      source
    end
  }+ [self]
end

Instance Attribute Details

#sourcesObject (readonly)

Returns the value of attribute sources.



5
6
7
# File 'lib/poniard/injector.rb', line 5

def sources
  @sources
end

Instance Method Details

#dispatch(method, overrides = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/poniard/injector.rb', line 17

def dispatch(method, overrides = {})
  args = method.parameters.map {|_, name|
    source = sources_for(overrides).detect {|source|
      source.respond_to?(name)
    }

    if source
      dispatch(source.method(name), overrides)
    else
      UnknownInjectable.new(name)
    end
  }
  method.(*args)
end

#injectorObject



32
33
34
# File 'lib/poniard/injector.rb', line 32

def injector
  self
end