Class: Dumpling::Container

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

Direct Known Subclasses

TestContainer

Instance Method Summary collapse

Constructor Details

#initializeContainer

Returns a new instance of Container.



3
4
5
6
# File 'lib/dumpling/container.rb', line 3

def initialize
  @services = Registry.new
  @abstract_services = Registry.new
end

Instance Method Details

#abstract(id, &block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/dumpling/container.rb', line 16

def abstract(id, &block)
  raise(Errors::Container::Duplicate, id) if @abstract_services.has?(id)

  specification = create_abstract_specification(&block)
  @abstract_services.set(id, specification)
  id
end

#configure(&block) ⇒ Object



33
34
35
36
# File 'lib/dumpling/container.rb', line 33

def configure(&block)
  instance_eval(&block)
  self
end

#get(id) ⇒ Object Also known as: []



24
25
26
27
28
29
# File 'lib/dumpling/container.rb', line 24

def get(id)
  raise(Errors::Container::Missing, id) unless @services.has?(id)

  specification = @services.get(id)
  build_service(specification)
end

#initialize_dup(original) ⇒ Object



38
39
40
41
42
# File 'lib/dumpling/container.rb', line 38

def initialize_dup(original)
  @services = original.services.dup
  @abstract_services = original.abstract_services.dup
  super
end

#inspectObject



44
45
46
47
# File 'lib/dumpling/container.rb', line 44

def inspect
  services = @services.keys.sort.map { |id| inspect_service(id) }
  services.empty? ? to_s : "#{self}\n#{services.join("\n").strip}"
end

#set(id, &block) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/dumpling/container.rb', line 8

def set(id, &block)
  raise(Errors::Container::Duplicate, id) if @services.has?(id)

  specification = create_specification(&block)
  @services.set(id, specification)
  id
end