/
results_array.rb
105 lines (86 loc) · 2.49 KB
/
results_array.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module SimpleRecord
#
# We need to make this behave as if the full set were loaded into the array.
class ResultsArray
include Enumerable
attr_reader :next_token, :clz, :params, :items, :i
def initialize(clz=nil, params=[], items=[], next_token=nil)
@clz = clz
#puts 'class=' + clz.inspect
@params = params
@items = items
@next_token = next_token
@i = 0
end
def << (val)
@items << val
end
def [](*i)
# todo: load items up to i if size > i
@items[*i]
end
def first
@items[0]
end
def last
@items[@items.length-1]
end
def empty?
@items.empty?
end
def include?(obj)
@items.include?(obj)
end
def size
# puts 'SIZE count=' + @count.inspect
return @count if @count
params_for_count = params.dup
params_for_count[0] = :count
#puts 'params_for_count=' + params_for_count.inspect
@count = clz.find(*params_for_count)
# puts '@count=' + @count.to_s
@count
end
def length
return size
end
def each(&blk)
limit = nil
if params.size > 1
options = params[1]
limit = options[:limit]
else
options = {}
params[1] = options
end
@items.each do |v|
#puts @i.to_s
yield v
@i += 1
if !limit.nil? && @i >= limit
return
end
end
# no more items, but is there a next token?
return if clz.nil?
unless next_token.nil?
#puts 'finding more items...'
#puts 'params in block=' + params.inspect
options[:next_token] = next_token
res = clz.find(*params)
items = res.items # get the real items array from the ResultsArray
items.each do |item|
@items << item
end
@next_token = res.next_token
each(&blk)
end
end
def delete(item)
@items.delete(item)
end
def delete_at(index)
@items.delete_at(index)
end
end
end