Class: Reorm::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/reorm/cursor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, query, order_by = nil) ⇒ Cursor

Returns a new instance of Cursor.



7
8
9
10
11
12
13
14
# 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

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



15
16
17
# File 'lib/reorm/cursor.rb', line 15

def model_class
  @model_class
end

Instance Method Details

#closeObject Also known as: reset



17
18
19
20
21
22
# File 'lib/reorm/cursor.rb', line 17

def close
  @cursor.close if @cursor && !@cursor.kind_of?(Array)
  @cursor = nil
  @offset = @total = 0
  self
end

#countObject



33
34
35
36
37
# File 'lib/reorm/cursor.rb', line 33

def count
  Reorm.connection do |connection|
    @query.count.run(connection)
  end
end

#deleteObject



120
121
122
123
124
# File 'lib/reorm/cursor.rb', line 120

def delete
  Reorm.connection do |connection|
    @query.delete.run(connection)
  end
end

#each(&block) ⇒ Object



66
67
68
# 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?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/reorm/cursor.rb', line 39

def exhausted?
  open? && @offset == @total
end

#filter(predicate = nil, &block) ⇒ Object



25
26
27
28
29
30
31
# 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

#findObject Also known as: detect



43
44
45
46
47
48
49
50
51
52
53
# 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

#firstObject



87
88
89
# File 'lib/reorm/cursor.rb', line 87

def first
  nth(0)
end

#inject(token = nil) ⇒ Object



70
71
72
73
74
75
# File 'lib/reorm/cursor.rb', line 70

def inject(token=nil)
  each do |record|
    yield token, record
  end
  token
end

#lastObject



91
92
93
# File 'lib/reorm/cursor.rb', line 91

def last
  nth(count - 1)
end

#limit(size) ⇒ Object



99
100
101
# File 'lib/reorm/cursor.rb', line 99

def limit(size)
  Cursor.new(model_class, @query.limit(size), @order_by)
end

#nextObject



56
57
58
59
60
61
62
63
64
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
# 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) ⇒ Object Also known as: skip



103
104
105
# File 'lib/reorm/cursor.rb', line 103

def offset(quantity)
  Cursor.new(model_class, @query.skip(quantity), @order_by)
end

#order_by(*arguments) ⇒ Object



116
117
118
# File 'lib/reorm/cursor.rb', line 116

def order_by(*arguments)
  Cursor.new(model_class, @query, arguments)
end

#slice(start_at, end_at = nil, left_bound = 'closed', right_bound = "open") ⇒ Object



108
109
110
111
112
113
114
# 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_aObject



95
96
97
# File 'lib/reorm/cursor.rb', line 95

def to_a
  inject([]) {|list, record| list << record}
end