Class: Factbase::LazyTaped::LazyTapedArray

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

Overview

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



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

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

Instance Method Details

#<<(item) ⇒ Object



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

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

#[](idx) ⇒ Object



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

def [](idx)
  current_array[idx]
end

#any?(pattern = nil) ⇒ Boolean

Returns:



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

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

#eachObject



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

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

#to_aObject



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

def to_a
  current_array.to_a
end

#uniq!Object



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

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