Class: Hypo::Container

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

Instance Method Summary collapse

Constructor Details

#initializeContainer



7
8
9
# File 'lib/hypo/container.rb', line 7

def initialize
  @components = {}
end

Instance Method Details

#register(item, name = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/hypo/container.rb', line 11

def register(item, name = nil)
  type = item.is_a?(Class) ? Component : Instance
  component = type.new(item, self, name)

  if @components.key?(component.name)
    raise ContainerError, "Component of type \"#{component.type.to_s}\" has already been registered"
  end

  @components[component.name] = component
end

#resolve(name) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/hypo/container.rb', line 22

def resolve(name)
  unless @components.key?(name)
    raise ContainerError, "Component with name \"#{name}\" is not registered"
  end

  @components[name].instance
end