Class: SimpleRecord::Sharding::ShardedResults
- Inherits:
-
Object
- Object
- SimpleRecord::Sharding::ShardedResults
- Includes:
- Enumerable
- Defined in:
- lib/simple_record/sharding.rb
Instance Method Summary collapse
- #<<(val) ⇒ Object
- #[](*i) ⇒ Object
- #add_results(rs) ⇒ Object
- #current_page ⇒ Object
- #delete(item) ⇒ Object
- #delete_at(index) ⇒ Object
- #each(&blk) ⇒ Object
- #element_at(index) ⇒ Object
- #empty? ⇒ Boolean
- #first ⇒ Object
- #include?(obj) ⇒ Boolean
-
#initialize(params) ⇒ ShardedResults
constructor
A new instance of ShardedResults.
- #last ⇒ Object
- #length ⇒ Object
-
#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
-
#sum_count ⇒ Object
only used for count queries.
- #total_entries ⇒ Object
-
#total_pages ⇒ Object
for will_paginate support.
Constructor Details
#initialize(params) ⇒ ShardedResults
Returns a new instance of ShardedResults.
109 110 111 112 113 |
# File 'lib/simple_record/sharding.rb', line 109 def initialize(params) @params = params = params.size > 1 ? params[1] : {} @results_arrays = [] end |
Instance Method Details
#<<(val) ⇒ Object
129 130 131 |
# File 'lib/simple_record/sharding.rb', line 129 def <<(val) raise "Not supported." end |
#[](*i) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/simple_record/sharding.rb', line 142 def [](*i) if i.size == 1 # puts '[] i=' + i.to_s index = i[0] return element_at(index) else offset = i[0] rows = i[1] ret = [] x = offset while x < (offset+rows) ret << element_at(x) x+=1 end ret end end |
#add_results(rs) ⇒ Object
115 116 117 118 |
# File 'lib/simple_record/sharding.rb', line 115 def add_results(rs) # puts 'adding results=' + rs.inspect @results_arrays << rs end |
#current_page ⇒ Object
217 218 219 |
# File 'lib/simple_record/sharding.rb', line 217 def current_page return [:page] || 1 end |
#delete(item) ⇒ Object
255 256 257 |
# File 'lib/simple_record/sharding.rb', line 255 def delete(item) raise "Not supported" end |
#delete_at(index) ⇒ Object
259 260 261 |
# File 'lib/simple_record/sharding.rb', line 259 def delete_at(index) raise "Not supported" end |
#each(&blk) ⇒ Object
199 200 201 202 203 204 205 |
# File 'lib/simple_record/sharding.rb', line 199 def each(&blk) i = 0 @results_arrays.each do |rs| rs.each(&blk) i+=1 end end |
#element_at(index) ⇒ Object
133 134 135 136 137 138 139 140 |
# File 'lib/simple_record/sharding.rb', line 133 def element_at(index) @results_arrays.each do |rs| if rs.size > index return rs[index] end index -= rs.size end end |
#empty? ⇒ Boolean
168 169 170 171 172 173 |
# File 'lib/simple_record/sharding.rb', line 168 def empty? @results_arrays.each do |rs| return false if !rs.empty? end true end |
#first ⇒ Object
160 161 162 |
# File 'lib/simple_record/sharding.rb', line 160 def first @results_arrays.first.first end |
#include?(obj) ⇒ Boolean
175 176 177 178 179 180 181 |
# File 'lib/simple_record/sharding.rb', line 175 def include?(obj) @results_arrays.each do |rs| x = rs.include?(obj) return true if x end false end |
#last ⇒ Object
164 165 166 |
# File 'lib/simple_record/sharding.rb', line 164 def last @results_arrays.last.last end |
#length ⇒ Object
195 196 197 |
# File 'lib/simple_record/sharding.rb', line 195 def length return size end |
#next_page ⇒ Object
current_page + 1 or nil if there is no next page
250 251 252 |
# File 'lib/simple_record/sharding.rb', line 250 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.
240 241 242 |
# File 'lib/simple_record/sharding.rb', line 240 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.
232 233 234 |
# File 'lib/simple_record/sharding.rb', line 232 def out_of_bounds? current_page > total_pages end |
#previous_page ⇒ Object
current_page - 1 or nil if there is no previous page
245 246 247 |
# File 'lib/simple_record/sharding.rb', line 245 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#query_options ⇒ Object
221 222 223 |
# File 'lib/simple_record/sharding.rb', line 221 def return end |
#size ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/simple_record/sharding.rb', line 183 def size return @size if @size s = 0 @results_arrays.each do |rs| # puts 'rs=' + rs.inspect # puts 'rs.size=' + rs.size.inspect s += rs.size end @size = s s end |
#sum_count ⇒ Object
only used for count queries
121 122 123 124 125 126 127 |
# File 'lib/simple_record/sharding.rb', line 121 def sum_count x = 0 @results_arrays.each do |rs| x += rs if rs end x end |
#total_entries ⇒ Object
225 226 227 |
# File 'lib/simple_record/sharding.rb', line 225 def total_entries return size end |
#total_pages ⇒ Object
for will_paginate support
208 209 210 211 212 213 214 215 |
# File 'lib/simple_record/sharding.rb', line 208 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 |