Class: SimpleRecord::ResultsArray
- Inherits:
-
Object
- Object
- SimpleRecord::ResultsArray
- 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
-
#box_usage ⇒ Object
readonly
Returns the value of attribute box_usage.
-
#clz ⇒ Object
readonly
Returns the value of attribute clz.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#next_token ⇒ Object
readonly
Returns the value of attribute next_token.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#request_id ⇒ Object
readonly
Returns the value of attribute request_id.
Instance Method Summary collapse
- #<<(val) ⇒ Object
- #[](*i) ⇒ Object
-
#as_json(options = nil) ⇒ Object
A couple json serialization methods copied from active_support.
- #current_page ⇒ Object
- #delete(item) ⇒ Object
- #delete_at(index) ⇒ Object
- #each(&blk) ⇒ Object
- #each2(i, &blk) ⇒ Object
- #empty? ⇒ Boolean
-
#encode_json(encoder) ⇒ Object
:nodoc:.
- #first ⇒ Object
- #include?(obj) ⇒ Boolean
-
#initialize(clz = nil, params = [], results = nil, next_token = nil) ⇒ ResultsArray
constructor
A new instance of ResultsArray.
- #last ⇒ Object
- #length ⇒ Object
- #load_next_token_set ⇒ Object
-
#load_to(i) ⇒ Object
Will load items from SimpleDB up to i.
-
#next_page ⇒ Object
current_page + 1 or nil if there is no next page.
-
#offset ⇒ Object
Current offset of the paginated collection.
-
#out_of_bounds? ⇒ Boolean
Helper method that is true when someone tries to fetch a page with a larger number than the last page.
-
#previous_page ⇒ Object
current_page - 1 or nil if there is no previous page.
- #query_options ⇒ Object
- #size ⇒ Object
- #total_entries ⇒ Object
-
#total_pages ⇒ Object
for will_paginate support.
Constructor Details
#initialize(clz = nil, params = [], results = nil, next_token = nil) ⇒ ResultsArray
Returns a new instance of ResultsArray.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/simple_record/results_array.rb', line 11 def initialize(clz=nil, params=[], results=nil, next_token=nil) @clz = clz #puts 'class=' + clz.inspect @params = params if @params.size <= 1 = {} @params[1] = end @items = results[:items] @currentset_items = results[:items] @next_token = next_token # puts 'bu=' + results[:box_usage] @box_usage = results[:box_usage].to_f @request_id = results[:request_id] @options = @params[1] if @options[:page] load_to(@options[:per_page] * @options[:page]) @start_at = @options[:per_page] * (@options[:page] - 1) end @index = 0 # puts 'RESULTS_ARRAY=' + self.inspect end |
Instance Attribute Details
#box_usage ⇒ Object (readonly)
Returns the value of attribute box_usage.
8 9 10 |
# File 'lib/simple_record/results_array.rb', line 8 def box_usage @box_usage end |
#clz ⇒ Object (readonly)
Returns the value of attribute clz.
8 9 10 |
# File 'lib/simple_record/results_array.rb', line 8 def clz @clz end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
8 9 10 |
# File 'lib/simple_record/results_array.rb', line 8 def index @index end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
8 9 10 |
# File 'lib/simple_record/results_array.rb', line 8 def items @items end |
#next_token ⇒ Object (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 |
#params ⇒ Object (readonly)
Returns the value of attribute params.
8 9 10 |
# File 'lib/simple_record/results_array.rb', line 8 def params @params end |
#request_id ⇒ Object (readonly)
Returns the value of attribute request_id.
8 9 10 |
# File 'lib/simple_record/results_array.rb', line 8 def request_id @request_id end |
Instance Method Details
#<<(val) ⇒ Object
34 35 36 |
# File 'lib/simple_record/results_array.rb', line 34 def << (val) @items << val end |
#[](*i) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/simple_record/results_array.rb', line 38 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 |
#as_json(options = nil) ⇒ Object
A couple json serialization methods copied from active_support
219 220 221 222 223 |
# File 'lib/simple_record/results_array.rb', line 219 def as_json( = nil) #:nodoc: # use encoder as a proxy to call as_json on all elements, to protect from circular references encoder = && [:encoder] || ActiveSupport::JSON::Encoding::Encoder.new() map { |v| encoder.as_json(v) } end |
#current_page ⇒ Object
160 161 162 |
# File 'lib/simple_record/results_array.rb', line 160 def current_page return [:page] || 1 end |
#delete(item) ⇒ Object
209 210 211 |
# File 'lib/simple_record/results_array.rb', line 209 def delete(item) @items.delete(item) end |
#delete_at(index) ⇒ Object
213 214 215 |
# File 'lib/simple_record/results_array.rb', line 213 def delete_at(index) @items.delete_at(index) end |
#each(&blk) ⇒ Object
113 114 115 |
# File 'lib/simple_record/results_array.rb', line 113 def each(&blk) each2((@start_at || 0), &blk) end |
#each2(i, &blk) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/simple_record/results_array.rb', line 117 def each2(i, &blk) = @params[1] # puts 'options=' + options.inspect limit = [:limit] # puts 'limit=' + limit.inspect if i > @items.size i = @items.size end range = i..@items.size # puts 'range=' + range.inspect @items[range].each do |v| # puts "i=" + i.to_s yield v i += 1 @index += 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
78 79 80 |
# File 'lib/simple_record/results_array.rb', line 78 def empty? @items.empty? end |
#encode_json(encoder) ⇒ Object
:nodoc:
225 226 227 228 |
# File 'lib/simple_record/results_array.rb', line 225 def encode_json(encoder) #:nodoc: # we assume here that the encoder has already run as_json on self and the elements, so we run encode_json directly "[#{map { |v| v.encode_json(encoder) } * ','}]" end |
#first ⇒ Object
70 71 72 |
# File 'lib/simple_record/results_array.rb', line 70 def first @items[0] end |
#include?(obj) ⇒ Boolean
82 83 84 |
# File 'lib/simple_record/results_array.rb', line 82 def include?(obj) @items.include?(obj) end |
#last ⇒ Object
74 75 76 |
# File 'lib/simple_record/results_array.rb', line 74 def last @items[@items.length-1] end |
#length ⇒ Object
109 110 111 |
# File 'lib/simple_record/results_array.rb', line 109 def length return size end |
#load_next_token_set ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/simple_record/results_array.rb', line 197 def load_next_token_set = @params[1] [:next_token] = @next_token [:called_by] = :results_array 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.
63 64 65 66 67 68 |
# File 'lib/simple_record/results_array.rb', line 63 def load_to(i) return if @items.size >= i while @items.size < i && !@next_token.nil? load_next_token_set end end |
#next_page ⇒ Object
current_page + 1 or nil if there is no next page
193 194 195 |
# File 'lib/simple_record/results_array.rb', line 193 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#offset ⇒ Object
Current offset of the paginated collection. If we’re on the first page, it is always 0. If we’re on the 2nd page and there are 30 entries per page, the offset is 30. This property is useful if you want to render ordinals side by side with records in the view: simply start with offset + 1.
183 184 185 |
# File 'lib/simple_record/results_array.rb', line 183 def offset (current_page - 1) * per_page end |
#out_of_bounds? ⇒ Boolean
Helper method that is true when someone tries to fetch a page with a larger number than the last page. Can be used in combination with flashes and redirecting.
175 176 177 |
# File 'lib/simple_record/results_array.rb', line 175 def out_of_bounds? current_page > total_pages end |
#previous_page ⇒ Object
current_page - 1 or nil if there is no previous page
188 189 190 |
# File 'lib/simple_record/results_array.rb', line 188 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#query_options ⇒ Object
164 165 166 |
# File 'lib/simple_record/results_array.rb', line 164 def return @options end |
#size ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/simple_record/results_array.rb', line 86 def size # if @options[:per_page] # return @items.size - @start_at # end if @next_token.nil? return @items.size end return @count if @count # puts '@params=' + @params.inspect params_for_count = @params.dup params_for_count[0] = :count params_for_count[1] = params_for_count[1].dup # for deep clone params_for_count[1].delete(:limit) params_for_count[1].delete(:per_token) params_for_count[1][:called_by] = :results_array # puts '@params2=' + @params.inspect # puts 'params_for_count=' + params_for_count.inspect @count = clz.find(*params_for_count) # puts '@count=' + @count.to_s @count end |
#total_entries ⇒ Object
168 169 170 |
# File 'lib/simple_record/results_array.rb', line 168 def total_entries return size end |
#total_pages ⇒ Object
for will_paginate support
151 152 153 154 155 156 157 158 |
# File 'lib/simple_record/results_array.rb', line 151 def total_pages #puts 'total_pages' # puts @params[1][:per_page].to_s return 1 if @params[1][:per_page].nil? ret = (size / @params[1][:per_page].to_f).ceil #puts 'ret=' + ret.to_s ret end |