Class: Kameleoon::DataManager::DataArrayStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/kameleoon/data/manager/data_array_storage.rb

Overview

DataArrayStorage is a readonly accessor to a sequntial storage of specific visitor data. It is thread-safe

Instance Method Summary collapse

Constructor Details

#initialize(mutex, array) ⇒ DataArrayStorage

Returns a new instance of DataArrayStorage.



16
17
18
19
# File 'lib/kameleoon/data/manager/data_array_storage.rb', line 16

def initialize(mutex, array)
  @mutex = mutex
  @array = array
end

Instance Method Details

#enumerate(&blk) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/kameleoon/data/manager/data_array_storage.rb', line 21

def enumerate(&blk)
  return if @array.nil?

  @mutex.with_read_lock do
    @array.each { |v| blk.call(v) }
  end
end

#lastObject



29
30
31
32
33
34
35
36
37
# File 'lib/kameleoon/data/manager/data_array_storage.rb', line 29

def last
  return nil if @array.nil?

  value = nil
  @mutex.with_read_lock do
    value = @array.last
  end
  value
end

#sizeObject



39
40
41
42
43
44
45
46
47
# File 'lib/kameleoon/data/manager/data_array_storage.rb', line 39

def size
  return 0 if @array.nil?

  value = nil
  @mutex.with_read_lock do
    value = @array.size
  end
  value
end

#to_sObject



10
11
12
13
14
# File 'lib/kameleoon/data/manager/data_array_storage.rb', line 10

def to_s
  values = []
  enumerate { |v| values << v }
  "DataArrayStorage{values:#{values.join(',')}}"
end