Class: SimpleRecord::ResultsArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/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

Returns a new instance of ResultsArray.



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

def initialize(clz=nil, params=[], items=[], next_token=nil)
    @clz = clz
    #puts 'class=' + clz.inspect
    @params = params
    @items = items
    @currentset_items = items
    @next_token = next_token
    @i = 0
end

Instance Attribute Details

#clzObject (readonly)

Returns the value of attribute clz.



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

def clz
  @clz
end

#iObject (readonly)

Returns the value of attribute i.



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

def i
  @i
end

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

#next_tokenObject (readonly)

Returns the value of attribute next_token.



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

def next_token
  @next_token
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Instance Method Details

#<<(val) ⇒ Object



21
22
23
# File 'lib/results_array.rb', line 21

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

#[](*i) ⇒ Object



25
26
27
28
# File 'lib/results_array.rb', line 25

def [](*i)
    # todo: load items up to i if size > i
    @items[*i]
end

#delete(item) ⇒ Object



98
99
100
# File 'lib/results_array.rb', line 98

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

#delete_at(index) ⇒ Object



102
103
104
# File 'lib/results_array.rb', line 102

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

#each(&blk) ⇒ Object



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
# File 'lib/results_array.rb', line 61

def each(&blk)
    limit = nil
    if params.size > 1
        options = params[1]
        limit = options[:limit]
    else
        options = {}
        params[1] = options
    end

    @currentset_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
        #puts "i from results_array = " + @i.to_s

        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
        each(&blk)
    end
end

#empty?Boolean

Returns:



38
39
40
# File 'lib/results_array.rb', line 38

def empty?
    @items.empty?
end

#firstObject



30
31
32
# File 'lib/results_array.rb', line 30

def first
    @items[0]
end

#include?(obj) ⇒ Boolean

Returns:



42
43
44
# File 'lib/results_array.rb', line 42

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

#lastObject



34
35
36
# File 'lib/results_array.rb', line 34

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

#lengthObject



57
58
59
# File 'lib/results_array.rb', line 57

def length
    return size
end

#sizeObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/results_array.rb', line 46

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