class Concurrent::LazyRegister
Hash-like collection that store lazys evaluated values.
@example
register = Concurrent::LazyRegister.new #=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @Data=#<Concurrent::AtomicReference:0x007fd7ecd5e1e0>> register[:key] #=> nil register.add(:key) { Concurrent::Actor.spawn!(Actor::AdHoc, :ping) { -> message { message } } } #=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @Data=#<Concurrent::AtomicReference:0x007fd7ecd5e1e0>> register[:key] #=> #<Concurrent::Actor::Reference /ping (Concurrent::Actor::AdHoc)>
@!macro edge_warning
Public Class Methods
new()
click to toggle source
Calls superclass method
# File lib/concurrent/lazy_register.rb, line 23 def initialize super self.data = {} end
Public Instance Methods
[](key)
click to toggle source
Element reference. Retrieves the value object corresponding to the key object. Returns nil if the key is not found. Raises an exception if the stored item raised an exception when the block was evaluated.
@param [Object] key @return [Object] value stored for the key or nil if the key is not found
@raise Exception when the initialization block fails
# File lib/concurrent/lazy_register.rb, line 36 def [](key) delay = data[key] delay ? delay.value! : nil end
register(key, &block)
click to toggle source
Element assignment. Associates the value given by value with the key given by key.
@param [Object] key @yield the object to store under the key
@return [LazyRegister] self
# File lib/concurrent/lazy_register.rb, line 59 def register(key, &block) delay = Delay.new(executor: :immediate, &block) update_data { |h| h.merge(key => delay) } self end
registered?(key)
click to toggle source
Returns true if the given key is present.
@param [Object] key @return [true, false] if the key is registered
# File lib/concurrent/lazy_register.rb, line 45 def registered?(key) data.key?(key) end