Class: Factbase::LazyTaped::LazyTapedArray

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

Overview

TODO:

#424:30min Add dedicated unit tests for LazyTapedArray class. Currently this class is tested indirectly through LazyTaped tests. We should add explicit tests for all public methods including each, [], to_a, any?, <<, and uniq! to ensure proper copy-on-write behavior.

Decorator of Array that triggers copy-on-write.

Instance Method Summary collapse

Constructor Details

#initialize(origin, key, taped_hash, added) ⇒ LazyTapedArray

Creates a new lazy array wrapper.

Parameters:

  • origin (Array)

    The original array to wrap

  • key (String)

    The key in the parent hash where this array is stored

  • taped_hash (LazyTapedHash)

    The parent hash wrapper that owns this array

  • added (Array)

    Accumulator for tracking object IDs of modified facts



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

def initialize(origin, key, taped_hash, added)
  @origin = origin
  @key = key
  @taped_hash = taped_hash
  @added = added
end

Instance Method Details

#<<(item) ⇒ Object



44
45
46
47
48
# File 'lib/factbase/lazy_taped_array.rb', line 44

def <<(item)
  @taped_hash.ensure_copied_map
  @added.append(@taped_hash.tracking_id)
  @taped_hash.get_copied_array(@key) << item
end

#[](idx) ⇒ Object



32
33
34
# File 'lib/factbase/lazy_taped_array.rb', line 32

def [](idx)
  current_array[idx]
end

#any?(pattern = nil) ⇒ Boolean

Returns:



40
41
42
# File 'lib/factbase/lazy_taped_array.rb', line 40

def any?(pattern = nil, &)
  pattern ? current_array.any?(pattern) : current_array.any?(&)
end

#eachObject



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

def each(&)
  return to_enum(__method__) unless block_given?
  current_array.each(&)
end

#to_aObject



36
37
38
# File 'lib/factbase/lazy_taped_array.rb', line 36

def to_a
  current_array.to_a
end

#uniq!Object



50
51
52
53
54
# File 'lib/factbase/lazy_taped_array.rb', line 50

def uniq!
  @taped_hash.ensure_copied_map
  @added.append(@taped_hash.tracking_id)
  @taped_hash.get_copied_array(@key).uniq!
end