Class: Infused::Container

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

Instance Method Summary collapse

Constructor Details

#initializeContainer

Returns a new instance of Container.



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

def initialize
  @ctors_map = {}
  @instantiated_map = {}
end

Instance Method Details

#get(id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/infused/container.rb', line 16

def get(id)
  if not @ctors_map.has_key? id
    if not DependenciesGraph.has? id
      raise ConstructorNotRegisteredError.new "id:#{id} is not registered in Container and DependenciesGraph"
    else
      return Instantiator.make(DependenciesGraph, id)
    end
  end
  
  if @ctors_map[id][:shared] == false
    @ctors_map[id][:block].call(self)
  else
    if @instantiated_map[id] == nil
      @instantiated_map[id] = @ctors_map[id][:block].call(self)
    end
    @instantiated_map[id]
  end
end

#has?(id) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/infused/container.rb', line 35

def has?(id)
  @ctors_map.has_key?(id)
end

#register(id, &block) ⇒ Object



8
9
10
# File 'lib/infused/container.rb', line 8

def register(id, &block)
  @ctors_map[id] = { block: block, shared: false }
end

#share(id, &block) ⇒ Object



12
13
14
# File 'lib/infused/container.rb', line 12

def share(id, &block)
  @ctors_map[id] = { block: block, shared: true }
end