Class: Smithy::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/smithy/container.rb

Instance Method Summary collapse

Constructor Details

#initializeContainer

Returns a new instance of Container.



9
10
11
12
# File 'lib/smithy/container.rb', line 9

def initialize
  @definitions = {}
  @instances = {}
end

Instance Method Details

#instance(name) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/smithy/container.rb', line 26

def instance(name)
  return @instances[name] if @instances[name]
  component, dependency = @definitions[name]
  raise(UnsatisfiedDependencyError, name) unless component
  args = dependency.map {|service| self.instance(service) }
  @instances[name] = component.new(*args)
end

#register(name, component, *dependency) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/smithy/container.rb', line 14

def register(name, component, *dependency)
  if component.respond_to?(:new)
    if dependency.any?
      @definitions[name] = [component, dependency]
    else
      @definitions[name] = [component, infer_dependency(component)]
    end
  else
    @instances[name] = component
  end
end