Class: StrokeDB::LazyMappingArray
- Inherits:
-
Array
show all
- Defined in:
- lib/strokedb/util/lazy_mapping_array.rb
Overview
Lazy loads items from array applying procs on each read and write.
Example:
@ary = 10
applies “unmap proc” to new item value.
@ary.at(i)
applies “map proc” to value just have been read.
StrokeDB uses this class to “follow” links to other documents found in slots in a lazy manner.
player:
model: [@#8b195509-f9c4-4fea-90c9-425b38bdda3e.ea5eda78-d410-44be-8b14-f4e33f6fa047]
generation: 4
when model collection item is fetched, reference followed and turned into document instance with mapping proc of lazy mapping array.
Constant Summary
Constants inherited
from Array
Array::SDATPTAGS
Instance Method Summary
collapse
Methods inherited from Array
#stroke_diff, #stroke_merge, #stroke_patch, #to_raw
Constructor Details
Returns a new instance of LazyMappingArray.
24
25
26
27
28
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 24
def initialize(*args)
@map_proc = proc {|v| v}
@unmap_proc = proc {|v| v}
super(*args)
end
|
Instance Method Details
126
127
128
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 126
def -(a)
_substract(a.map {|v| @unmap_proc.call(v) })
end
|
139
140
141
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 139
def ==(arr)
to_a == arr
end
|
#[](*args) ⇒ Object
Also known as:
slice
41
42
43
44
45
46
47
48
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 41
def [](*args)
r = _square_brackets(*args)
if (args.first.is_a?(Range) || args.size == 2) && r.is_a?(Array)
LazyMappingArray.new(r).map_with(&@map_proc).unmap_with(&@unmap_proc)
else
@map_proc.call(r)
end
end
|
#[]=(*args) ⇒ Object
52
53
54
55
56
57
58
59
60
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 52
def []=(*args)
value = args.pop
if (args.first.is_a?(Range) || args.size == 2) && value.is_a?(Array)
args << value.map{|e| @unmap_proc.call(e) }
_square_brackets_set(*args)
else
_square_brackets_set(args[0], @unmap_proc.call(value))
end
end
|
62
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 62
alias :_at :at
|
75
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 75
alias :_each :each
|
115
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 115
alias :_find :find
|
#_include? ⇒ Object
130
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 130
alias :_include? :include?
|
120
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 120
alias :_index :index
|
82
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 82
alias :_map :map
|
105
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 105
alias :_pop :pop
|
94
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 94
alias :_push :push
|
110
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 110
alias :_shift :shift
|
#_square_brackets ⇒ Object
40
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 40
alias :_square_brackets :[]
|
#_square_brackets_set ⇒ Object
51
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 51
alias :_square_brackets_set :[]=
|
#_substract ⇒ Object
125
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 125
alias :_substract :-
|
100
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 100
alias :_unshift :unshift
|
89
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 89
alias :_zip :zip
|
#at(index) ⇒ Object
63
64
65
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 63
def at(index)
@map_proc.call(_at(index))
end
|
143
144
145
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 143
def class
Array
end
|
76
77
78
79
80
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 76
def each
_each do |val|
yield @map_proc.call(val)
end
end
|
116
117
118
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 116
def find
_find {|value| yield(@map_proc.call(value))}
end
|
67
68
69
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 67
def first
at(0)
end
|
#include?(v) ⇒ Boolean
131
132
133
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 131
def include?(v)
_include?(@unmap_proc.call(v))
end
|
121
122
123
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 121
def index(v)
_index(@unmap_proc.call(v))
end
|
71
72
73
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 71
def last
at(size-1)
end
|
83
84
85
86
87
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 83
def map
_map do |val|
yield @map_proc.call(val)
end
end
|
#map_with(&block) ⇒ Object
30
31
32
33
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 30
def map_with(&block)
@map_proc = block
self
end
|
106
107
108
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 106
def pop
@map_proc.call(_pop)
end
|
#push(value) ⇒ Object
Also known as:
<<
95
96
97
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 95
def push(value)
_push(@unmap_proc.call(value))
end
|
111
112
113
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 111
def shift
@map_proc.call(_shift)
end
|
135
136
137
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 135
def to_a
Array.new(map{|v| v})
end
|
#unmap_with(&block) ⇒ Object
35
36
37
38
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 35
def unmap_with(&block)
@unmap_proc = block
self
end
|
#unshift(value) ⇒ Object
101
102
103
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 101
def unshift(value)
_unshift(@unmap_proc.call(value))
end
|
#zip(*args) ⇒ Object
90
91
92
|
# File 'lib/strokedb/util/lazy_mapping_array.rb', line 90
def zip(*args)
map{|v|v}.zip(*args)
end
|