Class: SimpleRecord::Sharding::ShardedResults

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

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ ShardedResults

Returns a new instance of ShardedResults.



137
138
139
140
141
# File 'lib/simple_record/sharding.rb', line 137

def initialize(params)
  @params = params
  @options = params.size > 1 ? params[1] : {}
  @results_arrays = []
end

Instance Method Details

#<<(val) ⇒ Object



157
158
159
# File 'lib/simple_record/sharding.rb', line 157

def <<(val)
  raise "Not supported."
end

#[](*i) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/simple_record/sharding.rb', line 170

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



143
144
145
146
# File 'lib/simple_record/sharding.rb', line 143

def add_results(rs)
#        puts 'adding results=' + rs.inspect
  @results_arrays << rs
end

#current_pageObject



245
246
247
# File 'lib/simple_record/sharding.rb', line 245

def current_page
  return query_options[:page] || 1
end

#delete(item) ⇒ Object



283
284
285
# File 'lib/simple_record/sharding.rb', line 283

def delete(item)
  raise "Not supported"
end

#delete_at(index) ⇒ Object



287
288
289
# File 'lib/simple_record/sharding.rb', line 287

def delete_at(index)
  raise "Not supported"
end

#each(&blk) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/simple_record/sharding.rb', line 227

def each(&blk)
  i = 0
  @results_arrays.each do |rs|
    rs.each(&blk)
    i+=1
  end
end

#element_at(index) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/simple_record/sharding.rb', line 161

def element_at(index)
  @results_arrays.each do |rs|
    if rs.size > index
      return rs[index]
    end
    index -= rs.size
  end
end

#empty?Boolean

Returns:

  • (Boolean)


196
197
198
199
200
201
# File 'lib/simple_record/sharding.rb', line 196

def empty?
  @results_arrays.each do |rs|
    return false if !rs.empty?
  end
  true
end

#firstObject



188
189
190
# File 'lib/simple_record/sharding.rb', line 188

def first
  @results_arrays.first.first
end

#include?(obj) ⇒ Boolean

Returns:

  • (Boolean)


203
204
205
206
207
208
209
# File 'lib/simple_record/sharding.rb', line 203

def include?(obj)
  @results_arrays.each do |rs|
    x = rs.include?(obj)
    return true if x
  end
  false
end

#lastObject



192
193
194
# File 'lib/simple_record/sharding.rb', line 192

def last
  @results_arrays.last.last
end

#lengthObject



223
224
225
# File 'lib/simple_record/sharding.rb', line 223

def length
  return size
end

#next_pageObject

current_page + 1 or nil if there is no next page



278
279
280
# File 'lib/simple_record/sharding.rb', line 278

def next_page
  current_page < total_pages ? (current_page + 1) : nil
end

#offsetObject

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.



268
269
270
# File 'lib/simple_record/sharding.rb', line 268

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.

Returns:

  • (Boolean)


260
261
262
# File 'lib/simple_record/sharding.rb', line 260

def out_of_bounds?
  current_page > total_pages
end

#previous_pageObject

current_page - 1 or nil if there is no previous page



273
274
275
# File 'lib/simple_record/sharding.rb', line 273

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end

#query_optionsObject



249
250
251
# File 'lib/simple_record/sharding.rb', line 249

def query_options
  return @options
end

#sizeObject



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/simple_record/sharding.rb', line 211

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_countObject

only used for count queries



149
150
151
152
153
154
155
# File 'lib/simple_record/sharding.rb', line 149

def sum_count
  x = 0
  @results_arrays.each do |rs|
    x += rs if rs
  end
  x
end

#total_entriesObject



253
254
255
# File 'lib/simple_record/sharding.rb', line 253

def total_entries
  return size
end

#total_pagesObject

for will_paginate support



236
237
238
239
240
241
242
243
# File 'lib/simple_record/sharding.rb', line 236

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