Class: Concurrent::LazyRegister
- Inherits:
-
Synchronization::Object
- Object
- Synchronization::Object
- Concurrent::LazyRegister
- Defined in:
- lib/concurrent-ruby-edge/concurrent/lazy_register.rb
Overview
Hash-like collection that store lazy evaluated values.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Element reference.
-
#initialize ⇒ LazyRegister
constructor
A new instance of LazyRegister.
-
#register(key) { ... } ⇒ LazyRegister
(also: #add, #store)
Element assignment.
-
#registered?(key) ⇒ true, false
(also: #key?, #has_key?)
Returns true if the given key is present.
-
#unregister(key) ⇒ LazyRegister
(also: #remove, #delete)
Un-registers the object under key, realized or not.
Constructor Details
#initialize ⇒ LazyRegister
Returns a new instance of LazyRegister.
25 26 27 28 |
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 25 def initialize super self.data = {} end |
Instance Method Details
#[](key) ⇒ Object
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.
38 39 40 41 |
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 38 def [](key) delay = data[key] delay ? delay.value! : nil end |
#register(key) { ... } ⇒ LazyRegister Also known as: add, store
Element assignment. Associates the value given by value with the key given by key.
61 62 63 64 65 |
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 61 def register(key, &block) delay = Delay.new(executor: :immediate, &block) update_data { |h| h.merge(key => delay) } self end |
#registered?(key) ⇒ true, false Also known as: key?, has_key?
Returns true if the given key is present.
47 48 49 |
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 47 def registered?(key) data.key?(key) end |
#unregister(key) ⇒ LazyRegister Also known as: remove, delete
Un-registers the object under key, realized or not.
75 76 77 78 |
# File 'lib/concurrent-ruby-edge/concurrent/lazy_register.rb', line 75 def unregister(key) update_data { |h| h.dup.tap { |j| j.delete(key) } } self end |