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.



109
110
111
112
113
# File 'lib/simple_record/sharding.rb', line 109

def initialize(params)
  @params         = params
  @options        = 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_pageObject



217
218
219
# File 'lib/simple_record/sharding.rb', line 217

def current_page
  return query_options[: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

Returns:

  • (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

#firstObject



160
161
162
# File 'lib/simple_record/sharding.rb', line 160

def first
  @results_arrays.first.first
end

#include?(obj) ⇒ Boolean

Returns:

  • (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

#lastObject



164
165
166
# File 'lib/simple_record/sharding.rb', line 164

def last
  @results_arrays.last.last
end

#lengthObject



195
196
197
# File 'lib/simple_record/sharding.rb', line 195

def length
  return size
end

#next_pageObject

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

#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.



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.

Returns:

  • (Boolean)


232
233
234
# File 'lib/simple_record/sharding.rb', line 232

def out_of_bounds?
  current_page > total_pages
end

#previous_pageObject

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_optionsObject



221
222
223
# File 'lib/simple_record/sharding.rb', line 221

def query_options
  return @options
end

#sizeObject



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_countObject

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_entriesObject



225
226
227
# File 'lib/simple_record/sharding.rb', line 225

def total_entries
  return size
end

#total_pagesObject

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