Class: Factbase::LazyTaped::LazyTapedHash

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

Overview

TODO:

#424:30min Add dedicated unit tests for LazyTapedHash class. Currently this class is tested indirectly through LazyTaped tests. We should add explicit tests for all public methods including keys, map, bracket access, bracket assignment, and the copy-on-write behavior.

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



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

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)



71
72
73
# File 'lib/factbase/lazy_taped_hash.rb', line 71

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

Instance Method Details

#[](key) ⇒ Object



35
36
37
38
39
# File 'lib/factbase/lazy_taped_hash.rb', line 35

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

#[]=(key, value) ⇒ Object



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

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

#copied?Boolean

Returns:



61
62
63
# File 'lib/factbase/lazy_taped_hash.rb', line 61

def copied?
  !@copied_map.nil?
end

#ensure_copied_mapObject



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

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

#get_copied_array(key) ⇒ Object



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

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

#keysObject



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

def keys
  current_map.keys
end

#mapObject



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

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

#tracking_idObject



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

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