class Multiton::InstanceBox
InstanceBox
is a thread safe container for storing and retrieving multiton instances.
Attributes
hash[RW]
A hash that contains the created instances indexed by their multiton key.
sync[RW]
An instance of Sync to provide thread safety (via a shared-exclusive lock) when reading and writing multiton instances to hash
.
Public Class Methods
new → new_instance
click to toggle source
Returns a new InstanceBox
instance.
# File lib/multiton/instance_box.rb, line 13 def initialize self.hash = {} self.sync = Sync.new self end
Public Instance Methods
get(key) → instance
click to toggle source
Returns the instance associated with key
. If key
does not exist it returns nil
.
# File lib/multiton/instance_box.rb, line 24 def get(key) sync.synchronize(:SH) { hash[key] } end
key(instance) → key
click to toggle source
Returns the multiton key associated with instance
. If instance
does not exist it returns nil
.
# File lib/multiton/instance_box.rb, line 33 def key(instance) sync.synchronize(:SH) { Utils.hash_key(hash, instance) } end
store(key, instance) → instance
click to toggle source
Stores instance
indexed by key
.
Returns instance
.
# File lib/multiton/instance_box.rb, line 44 def store(key, instance) sync.synchronize(:EX) { hash[key] ||= instance } end