Class: Copland::ServiceModel::SingletonServiceModel
- Inherits:
-
AbstractServiceModel
- Object
- AbstractServiceModel
- Copland::ServiceModel::SingletonServiceModel
- Defined in:
- lib/copland/models/singleton.rb
Overview
This service model enforces a singleton constraint on the service point it protects. That is to say, whenever the #instance method of this model is called, it will always return the same instance of the service point.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from AbstractServiceModel
Instance Method Summary collapse
-
#initialize(service_point) ⇒ SingletonServiceModel
constructor
Create a new SingletonServiceModel on the given service point.
-
#instance(&init) ⇒ Object
Return the singleton instance of the service point.
Methods inherited from AbstractServiceModel
Constructor Details
#initialize(service_point) ⇒ SingletonServiceModel
Create a new SingletonServiceModel on the given service point.
49 50 51 52 |
# File 'lib/copland/models/singleton.rb', line 49 def initialize( service_point ) super @mutex = QueryableMutex.new end |
Instance Method Details
#instance(&init) ⇒ Object
Return the singleton instance of the service point. This is thread safe.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/copland/models/singleton.rb', line 56 def instance( &init ) return @instance if @instance if @mutex.self_locked? raise CoplandException, "circular dependency on #{service_point.full_name} detected" end @mutex.synchronize do return @instance if @instance new_instance( &init ) return @instance end end |