Class: SimpleRecord::ResultsArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/simple_record/results_array.rb

Overview

We need to make this behave as if the full set were loaded into the array.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clz = nil, params = [], items = [], next_token = nil) ⇒ ResultsArray



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/simple_record/results_array.rb', line 11

def initialize(clz=nil, params=[], items=[], next_token=nil)
    @clz = clz
    #puts 'class=' + clz.inspect
    @params = params
    if @params.size <= 1
        options = {}
        @params[1] = options
    end
    @items = items
    @currentset_items = items
    @next_token = next_token
end

Instance Attribute Details

#clzObject (readonly)

Returns the value of attribute clz.



8
9
10
# File 'lib/simple_record/results_array.rb', line 8

def clz
  @clz
end

#iObject (readonly)

Returns the value of attribute i.



8
9
10
# File 'lib/simple_record/results_array.rb', line 8

def i
  @i
end

#itemsObject (readonly)

Returns the value of attribute items.



8
9
10
# File 'lib/simple_record/results_array.rb', line 8

def items
  @items
end

#next_tokenObject (readonly)

Returns the value of attribute next_token.



8
9
10
# File 'lib/simple_record/results_array.rb', line 8

def next_token
  @next_token
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/simple_record/results_array.rb', line 8

def params
  @params
end

Instance Method Details

#<<(val) ⇒ Object



24
25
26
# File 'lib/simple_record/results_array.rb', line 24

def << (val)
    @items << val
end

#[](*i) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/simple_record/results_array.rb', line 28

def [](*i)
#            puts 'i.inspect=' + i.inspect
#            puts i.size.to_s
#            i.each do |x|
#                puts 'x=' + x.inspect + " -- " + x.class.name
#            end
    if i.size == 1
        # either fixnum or range
        x = i[0]
        if x.is_a?(Fixnum)
            load_to(x)
        else
            # range
            end_val = x.exclude_end? ? x.end-1 : x.end
            load_to(end_val)
        end
    elsif i.size == 2
        # two fixnums
        end_val = i[0] + i[1]
        load_to(end_val)
    end
    @items[*i]
end

#delete(item) ⇒ Object



132
133
134
# File 'lib/simple_record/results_array.rb', line 132

def delete(item)
    @items.delete(item)
end

#delete_at(index) ⇒ Object



136
137
138
# File 'lib/simple_record/results_array.rb', line 136

def delete_at(index)
    @items.delete_at(index)
end

#each(&blk) ⇒ Object



92
93
94
# File 'lib/simple_record/results_array.rb', line 92

def each(&blk)
    each2(0, &blk)
end

#each2(i, &blk) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/simple_record/results_array.rb', line 96

def each2(i, &blk)
    options = @params[1]
    limit = options[:limit]

    @items[i..@items.size].each do |v|
#                puts "i=" + i.to_s
        yield v
        i += 1
        if !limit.nil? && i >= limit
            return
        end
    end
    return if @clz.nil?

    # no more items, but is there a next token?
    unless @next_token.nil?
        #puts 'finding more items...'
        #puts 'params in block=' + params.inspect
        #puts "i from results_array = " + @i.to_s

        load_next_token_set
        each2(i, &blk)
    end
end

#empty?Boolean



68
69
70
# File 'lib/simple_record/results_array.rb', line 68

def empty?
    @items.empty?
end

#firstObject



60
61
62
# File 'lib/simple_record/results_array.rb', line 60

def first
    @items[0]
end

#include?(obj) ⇒ Boolean



72
73
74
# File 'lib/simple_record/results_array.rb', line 72

def include?(obj)
    @items.include?(obj)
end

#lastObject



64
65
66
# File 'lib/simple_record/results_array.rb', line 64

def last
    @items[@items.length-1]
end

#lengthObject



88
89
90
# File 'lib/simple_record/results_array.rb', line 88

def length
    return size
end

#load_next_token_setObject



121
122
123
124
125
126
127
128
129
130
# File 'lib/simple_record/results_array.rb', line 121

def load_next_token_set
    options = @params[1]
    options[:next_token] = @next_token
    res = @clz.find(*@params)
    @currentset_items = res.items # get the real items array from the ResultsArray
    @currentset_items.each do |item|
        @items << item
    end
    @next_token = res.next_token
end

#load_to(i) ⇒ Object

Will load items from SimpleDB up to i.



53
54
55
56
57
58
# File 'lib/simple_record/results_array.rb', line 53

def load_to(i)
    return if @items.size >= i
    while @items.size < i && !@next_token.nil?
        load_next_token_set
    end
end

#sizeObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/simple_record/results_array.rb', line 76

def size
    # puts 'SIZE count=' + @count.inspect
    # todo: if no next token, should use the array.size so we don't call count
    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