Class: Hypo::Lifetime::Singleton

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

Instance Method Summary collapse

Constructor Details

#initializeSingleton

Returns a new instance of Singleton.



4
5
6
7
# File 'lib/hypo/lifetime/singleton.rb', line 4

def initialize
  @instances = Hash.new
  @mutex = Mutex.new
end

Instance Method Details

#instance(component, attrs = nil) ⇒ Object



9
10
11
# File 'lib/hypo/lifetime/singleton.rb', line 9

def instance(component, attrs = nil)
  @instances[component.name]
end

#preload(component) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/hypo/lifetime/singleton.rb', line 13

def preload(component)
  @mutex.synchronize do
    unless @instances.key? component.name
      instance = component.respond_to?(:type) ? component.type.new(*component.dependencies) : component.object
      @instances[component.name] = instance
    end
  end
end