Class: Rumx::Beans::Hash

Inherits:
Object
  • Object
show all
Includes:
Rumx::Bean
Defined in:
lib/rumx/beans/hash.rb

Direct Known Subclasses

TimerAndErrorHash, TimerHash

Instance Method Summary collapse

Methods included from Rumx::Bean

add_root, #bean_add_child, #bean_children, #bean_each, #bean_each_child, #bean_each_child_recursive, #bean_each_embedded_child, #bean_each_operation, #bean_each_operation_recursive, #bean_embedded, #bean_find, #bean_get_and_set_attributes, #bean_get_attributes, #bean_has_attributes?, #bean_has_operations?, #bean_monitor, #bean_remove_child, #bean_set_and_get_attributes, #bean_set_attributes, #bean_synchronize, find, find_operation, included, remove_root, root

Constructor Details

#initialize(&block) ⇒ Hash

Returns a new instance of Hash.



6
7
8
9
10
11
12
# File 'lib/rumx/beans/hash.rb', line 6

def initialize(&block)
  raise 'Must be given initialize instance to create the bean' unless block_given?
  @block = block
  # Hacks for potential race conditions
  bean_children
  bean_synchronize {}
end

Instance Method Details

#[](key) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rumx/beans/hash.rb', line 14

def [](key)
  child = bean_children[key]
  return child if child
  bean_synchronize do
    # Try it again to prevent race
    child = bean_children[key]
    return child if child
    child = @block.call(key)
    bean_add_child(key, child)
  end
  return child
end

#delete(key) ⇒ Object



27
28
29
30
31
# File 'lib/rumx/beans/hash.rb', line 27

def delete(key)
  bean_synchronize do
    bean_remove_child(key)
  end
end