Class: LazyArray
Overview
borrowed partially from StrokeDB
Instance Method Summary
collapse
Methods included from Enumerable
#each_with_object, #group_by, #index_by, #many?, #sum, #to_json
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
delegate any not-explicitly-handled methods to @array, if possible. this is handy for handling methods mixed-into Array like group_by
388
389
390
391
392
393
394
395
396
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 388
def method_missing(method, *args, &block)
if @array.respond_to?(method)
lazy_load
results = @array.send(method, *args, &block)
results.equal?(@array) ? self : results
else
super
end
end
|
Instance Method Details
#<<(entry) ⇒ Object
Also known as:
add
168
169
170
171
172
173
174
175
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 168
def <<(entry)
if loaded?
super
else
@tail << entry
end
self
end
|
#[](*args) ⇒ Object
Also known as:
slice
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 96
def [](*args)
index, length = (*args)
if length.nil?
return at(index)
end
length ||= 1
if index >= 0 && lazy_possible?(@head, index + length)
@head.slice(*args)
elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length)
@tail.slice(*args)
else
super
end
end
|
#[]=(*args) ⇒ Object
Also known as:
splice
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 130
def []=(*args)
index, length = (*args[0..-2])
length ||= 1
if index >= 0 && lazy_possible?(@head, index + length)
@head.[]=(*args)
elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length)
@tail.[]=(*args)
else
super
end
end
|
#any? ⇒ Boolean
90
91
92
93
94
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 90
def any?
(lazy_possible?(@tail) && @tail.any?) ||
(lazy_possible?(@head) && @head.any?) ||
super
end
|
#at(index) ⇒ Object
34
35
36
37
38
39
40
41
42
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 34
def at(index)
if index >= 0 && lazy_possible?(@head, index + 1)
@head.at(index)
elsif index < 0 && lazy_possible?(@tail, index.abs)
@tail.at(index)
else
super
end
end
|
261
262
263
264
265
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 261
def clear
mark_loaded
@array.clear
self
end
|
#concat(other) ⇒ Object
179
180
181
182
183
184
185
186
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 179
def concat(other)
if loaded?
super
else
@tail.concat(other.entries)
end
self
end
|
#delete_at(index) ⇒ Object
233
234
235
236
237
238
239
240
241
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 233
def delete_at(index)
if index >= 0 && lazy_possible?(@head, index + 1)
@head.delete_at(index)
elsif index < 0 && lazy_possible?(@tail, index.abs)
@tail.delete_at(index)
else
super
end
end
|
#delete_if(&block) ⇒ Object
243
244
245
246
247
248
249
250
251
252
253
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 243
def delete_if(&block)
if loaded?
super
else
@reapers ||= []
@reapers << block
@head.delete_if(&block)
@tail.delete_if(&block)
end
self
end
|
#empty? ⇒ Boolean
86
87
88
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 86
def empty?
!any?
end
|
#eql?(other) ⇒ Boolean
Also known as:
==
306
307
308
309
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 306
def eql?(other)
lazy_load
@array.eql?(other.entries)
end
|
#fetch(*args, &block) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 44
def fetch(*args, &block)
index = args.first
if index >= 0 && lazy_possible?(@head, index + 1)
@head.fetch(*args, &block)
elsif index < 0 && lazy_possible?(@tail, index.abs)
@tail.fetch(*args, &block)
else
super
end
end
|
#first(*args) ⇒ Object
8
9
10
11
12
13
14
15
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 8
def first(*args)
if lazy_possible?(@head, *args)
@head.first(*args)
else
lazy_load
@array.first(*args)
end
end
|
291
292
293
294
295
296
297
298
299
300
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 291
def freeze
if loaded?
@array.freeze
else
@head.freeze
@tail.freeze
end
@frozen = true
self
end
|
#frozen? ⇒ Boolean
302
303
304
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 302
def frozen?
@frozen == true
end
|
#include?(entry) ⇒ Boolean
80
81
82
83
84
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 80
def include?(entry)
(lazy_possible?(@tail) && @tail.include?(entry)) ||
(lazy_possible?(@head) && @head.include?(entry)) ||
super
end
|
#index(entry) ⇒ Object
76
77
78
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 76
def index(entry)
(lazy_possible?(@head) && @head.index(entry)) || super
end
|
#insert(index, *entries) ⇒ Object
206
207
208
209
210
211
212
213
214
215
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 206
def insert(index, *entries)
if index >= 0 && lazy_possible?(@head, index)
@head.insert(index, *entries)
elsif index < 0 && lazy_possible?(@tail, index.abs - 1)
@tail.insert(index, *entries)
else
super
end
self
end
|
#kind_of?(klass) ⇒ Boolean
283
284
285
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 283
def kind_of?(klass)
super || @array.kind_of?(klass)
end
|
#last(*args) ⇒ Object
26
27
28
29
30
31
32
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 26
def last(*args)
if lazy_possible?(@tail, *args)
@tail.last(*args)
else
super
end
end
|
#load_with(&block) ⇒ Object
274
275
276
277
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 274
def load_with(&block)
@load_with_proc = block
self
end
|
#loaded? ⇒ Boolean
279
280
281
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 279
def loaded?
@loaded == true
end
|
217
218
219
220
221
222
223
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 217
def pop
if lazy_possible?(@tail)
@tail.pop
else
super
end
end
|
#push(*entries) ⇒ Object
188
189
190
191
192
193
194
195
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 188
def push(*entries)
if loaded?
super
else
@tail.push(*entries)
end
self
end
|
#replace(other) ⇒ Object
255
256
257
258
259
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 255
def replace(other)
mark_loaded
@array.replace(other.entries)
self
end
|
#respond_to?(method, include_private = false) ⇒ Boolean
287
288
289
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 287
def respond_to?(method, include_private = false)
super || @array.respond_to?(method)
end
|
146
147
148
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 146
def reverse
dup.reverse!
end
|
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 150
def reverse!
if loaded?
@array = @array.reverse
else
@head, @tail = @tail.reverse, @head.reverse
proc = @load_with_proc
@load_with_proc = lambda do |v|
proc.call(v)
v.instance_variable_get(:@array).reverse!
end
end
self
end
|
225
226
227
228
229
230
231
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 225
def shift
if lazy_possible?(@head)
@head.shift
else
super
end
end
|
#slice!(*args) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 116
def slice!(*args)
index, length = (*args)
length ||= 1
if index >= 0 && lazy_possible?(@head, index + length)
@head.slice!(*args)
elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length)
@tail.slice!(*args)
else
super
end
end
|
#to_a ⇒ Object
Also known as:
to_ary
267
268
269
270
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 267
def to_a
lazy_load
@array.to_a
end
|
#unshift(*entries) ⇒ Object
197
198
199
200
201
202
203
204
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 197
def unshift(*entries)
if loaded?
super
else
@head.unshift(*entries)
end
self
end
|
#values_at(*args) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/gems/extlib-0.9.9/lib/extlib/lazy_array.rb', line 56
def values_at(*args)
accumulator = []
lazy_possible = args.all? do |arg|
index, length = (arg)
if index >= 0 && lazy_possible?(@head, index + (length || 1))
accumulator.concat(head.values_at(*arg))
elsif index < 0 && lazy_possible?(@tail, index.abs)
accumulator.concat(tail.values_at(*arg))
end
end
if lazy_possible
accumulator
else
super
end
end
|