class Reorm::Cursor

Attributes

model_class[R]

Public Class Methods

new(model_class, query, order_by=nil) click to toggle source
# File lib/reorm/cursor.rb, line 7
def initialize(model_class, query, order_by=nil)
  @model_class = model_class
  @query       = query
  @cursor      = nil
  @offset      = 0
  @total       = 0
  @order_by    = order_by
end

Public Instance Methods

close() click to toggle source
# File lib/reorm/cursor.rb, line 17
def close
  @cursor.close if @cursor && !@cursor.kind_of?(Array)
  @cursor = nil
  @offset = @total = 0
  self
end
Also aliased as: reset
count() click to toggle source
# File lib/reorm/cursor.rb, line 33
def count
  Reorm.connection do |connection|
    @query.count.run(connection)
  end
end
delete() click to toggle source
# File lib/reorm/cursor.rb, line 120
def delete
  Reorm.connection do |connection|
    @query.delete.run(connection)
  end
end
detect()
Alias for: find
each(&block) click to toggle source
# File lib/reorm/cursor.rb, line 66
def each(&block)
  @order_by.nil? ? each_without_order_by(&block) : each_with_order_by(&block)
end
exhausted?() click to toggle source
# File lib/reorm/cursor.rb, line 39
def exhausted?
  open? && @offset == @total
end
filter(predicate=nil, &block) click to toggle source
# File lib/reorm/cursor.rb, line 25
def filter(predicate=nil, &block)
  if predicate.nil?
    Cursor.new(model_class, @query.filter(&block), @order_by)
  else
    Cursor.new(model_class, @query.filter(predicate), @order_by)
  end
end
find() { |record| ... } click to toggle source
# File lib/reorm/cursor.rb, line 43
def find
  model = nil
  each do |record|
    found = yield(record)
    if found
      model = record
      break
    end
  end
  model
end
Also aliased as: detect
first() click to toggle source
# File lib/reorm/cursor.rb, line 87
def first
  nth(0)
end
inject(token=nil) { |token, record| ... } click to toggle source
# File lib/reorm/cursor.rb, line 70
def inject(token=nil)
  each do |record|
    yield token, record
  end
  token
end
last() click to toggle source
# File lib/reorm/cursor.rb, line 91
def last
  nth(count - 1)
end
limit(size) click to toggle source
# File lib/reorm/cursor.rb, line 99
def limit(size)
  Cursor.new(model_class, @query.limit(size), @order_by)
end
next() click to toggle source
# File lib/reorm/cursor.rb, line 56
def next
  open if !open?
  if exhausted?
    raise Error, "There are no more matching records."
  end
  data    = @order_by.nil? ? @cursor.next : @cursor[@offset]
  @offset += 1
  model_for(data)
end
nth(offset) click to toggle source
# File lib/reorm/cursor.rb, line 77
def nth(offset)
  model = nil
  if offset >= 0 && offset < count
    Reorm.connection do |connection|
      model = model_for(@query.nth(offset).run(connection))
    end
  end
  model
end
offset(quantity) click to toggle source
# File lib/reorm/cursor.rb, line 103
def offset(quantity)
  Cursor.new(model_class, @query.skip(quantity), @order_by)
end
Also aliased as: skip
order_by(*arguments) click to toggle source
# File lib/reorm/cursor.rb, line 116
def order_by(*arguments)
  Cursor.new(model_class, @query, arguments)
end
reset()
Alias for: close
skip(quantity)
Alias for: offset
slice(start_at, end_at=nil, left_bound='closed', right_bound="open") click to toggle source
# File lib/reorm/cursor.rb, line 108
def slice(start_at, end_at=nil, left_bound='closed', right_bound="open")
  if end_at
    Cursor.new(model_class, @query.slice(start_at, end_at, left_bound, right_bound), @order_by)
  else
    Cursor.new(model_class, @query.slice(start_at), @order_by)
  end
end
to_a() click to toggle source
# File lib/reorm/cursor.rb, line 95
def to_a
  inject([]) {|list, record| list << record}
end

Private Instance Methods

each_with_order_by() { |model_for(record)| ... } click to toggle source
# File lib/reorm/cursor.rb, line 151
def each_with_order_by
  Reorm.connection do |connection|
    @query.order_by(*@order_by).run(connection).each do |record|
      yield model_for(record)
    end
  end
end
each_without_order_by() { |model_for(record)| ... } click to toggle source
# File lib/reorm/cursor.rb, line 159
def each_without_order_by
  Reorm.connection do |connection|
    cursor = @query.run(connection)
    begin
      cursor.each do |record|
        yield model_for(record)
      end
    ensure
      cursor.close
    end
  end
end
model_for(properties) click to toggle source
# File lib/reorm/cursor.rb, line 172
def model_for(properties)
  model = model_class.new
  model.assign_properties(properties)
  model
end
open() click to toggle source
# File lib/reorm/cursor.rb, line 128
def open
  Reorm.connection do |connection|
    array_based = false
    if @order_by && @order_by.size > 0
      clause = @order_by.find {|entry| entry.kind_of?(Hash)}
      array_based = clause.nil? || clause.keys != [:index]
    end

    @offset = 0
    if !array_based
      @total  = @query.count.run(connection)
      @cursor = @query.run(connection)
    else
      @cursor = @query.order_by(*@order_by).run(connection)
      @total  = @cursor.size
    end
  end
end
open?() click to toggle source
# File lib/reorm/cursor.rb, line 147
def open?
  !@cursor.nil?
end