Class: DiFtw::Singleton
- Inherits:
-
Object
- Object
- DiFtw::Singleton
- Defined in:
- lib/diftw/singleton.rb
Overview
Class representing an injected dependency that’s only created once.
Instance Method Summary collapse
-
#initialize(y) ⇒ Singleton
constructor
A new dependency.
-
#resolve ⇒ Object
Return the value for the dependency.
Constructor Details
#initialize(y) ⇒ Singleton
A new dependency.
11 12 13 14 15 |
# File 'lib/diftw/singleton.rb', line 11 def initialize(y) @y = y @val = nil @mutex = Mutex.new end |
Instance Method Details
#resolve ⇒ Object
Return the value for the dependency. If this is the first access, the injected value will be cached and re-used for later injections. Yes, it’s thread-safe.
21 22 23 |
# File 'lib/diftw/singleton.rb', line 21 def resolve @val || @mutex.synchronize { @val ||= @y.call } end |