Class: Factbase::LazyTaped::LazyTapedHash

Inherits:
Object
  • Object
show all
Defined in:
lib/factbase/lazy_taped_hash.rb

Overview

Decorator of Hash that triggers copy-on-write.

Instance Method Summary collapse

Constructor Details

#initialize(origin, lazy_taped, added) ⇒ LazyTapedHash

Creates a new LazyTapedHash decorator.

Parameters:

  • origin (Hash)

    The original hash being wrapped (not yet copied)

  • lazy_taped (Factbase::LazyTaped)

    The parent LazyTaped instance that manages copy-on-write

  • added (Array)

    Array to track object IDs of maps that have been modified



16
17
18
19
20
21
# File 'lib/factbase/lazy_taped_hash.rb', line 16

def initialize(origin, lazy_taped, added)
  @origin = origin
  @lazy_taped = lazy_taped
  @added = added
  @copied_map = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object (private)



67
68
69
# File 'lib/factbase/lazy_taped_hash.rb', line 67

def method_missing(method, *, &)
  current_map.send(method, *, &)
end

Instance Method Details

#[](key) ⇒ Object



31
32
33
34
35
# File 'lib/factbase/lazy_taped_hash.rb', line 31

def [](key)
  v = current_map[key]
  v = LazyTapedArray.new(v, key, self, @added) if v.is_a?(Array)
  v
end

#[]=(key, value) ⇒ Object



37
38
39
40
41
# File 'lib/factbase/lazy_taped_hash.rb', line 37

def []=(key, value)
  ensure_copied_map
  @copied_map[key] = value
  @added.append(@copied_map.object_id)
end

#copied?Boolean

Returns:



57
58
59
# File 'lib/factbase/lazy_taped_hash.rb', line 57

def copied?
  !@copied_map.nil?
end

#ensure_copied_mapObject



43
44
45
46
# File 'lib/factbase/lazy_taped_hash.rb', line 43

def ensure_copied_map
  return if @copied_map
  @copied_map = @lazy_taped.get_copied_map(@origin)
end

#get_copied_array(key) ⇒ Object



48
49
50
51
# File 'lib/factbase/lazy_taped_hash.rb', line 48

def get_copied_array(key)
  ensure_copied_map
  @copied_map[key]
end

#keysObject



23
24
25
# File 'lib/factbase/lazy_taped_hash.rb', line 23

def keys
  current_map.keys
end

#mapObject



27
28
29
# File 'lib/factbase/lazy_taped_hash.rb', line 27

def map(&)
  current_map.map(&)
end

#tracking_idObject



53
54
55
# File 'lib/factbase/lazy_taped_hash.rb', line 53

def tracking_id
  @copied_map ? @copied_map.object_id : @origin.object_id
end