Class: MemoryDb::Cursor

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

Instance Method Summary collapse

Constructor Details

#initialize(query_executor) ⇒ Cursor

Returns a new instance of Cursor.



8
9
10
11
12
# File 'lib/memorydb/cursor.rb', line 8

def initialize(query_executor)
  @query_executor = query_executor
  @filters = []
  @sorts   = []
end

Instance Method Details

#allObject



106
107
108
# File 'lib/memorydb/cursor.rb', line 106

def all
  query_executor.execute_find(query)
end

#and(*filters) ⇒ Object



69
70
71
72
# File 'lib/memorydb/cursor.rb', line 69

def and(*filters)
  @filters << filter_factory.and(*filters)
  self
end

#contains(field, value) ⇒ Object



49
50
51
52
# File 'lib/memorydb/cursor.rb', line 49

def contains(field, value)
  @filters << filter_factory.contains(field, value)
  self
end

#countObject



102
103
104
# File 'lib/memorydb/cursor.rb', line 102

def count
  query_executor.execute_count(query)
end

#eq(field, value) ⇒ Object



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

def eq(field, value)
  @filters << filter_factory.eq(field, value)
  self
end

#firstObject



110
111
112
113
114
115
116
# File 'lib/memorydb/cursor.rb', line 110

def first
  query_executor.execute_find(
    query.merge(
      sorts: sorts_for_first,
      limit: 1
    )).first
end

#gt(field, value) ⇒ Object



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

def gt(field, value)
  @filters << filter_factory.gt(field, value)
  self
end

#gte(field, value) ⇒ Object



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

def gte(field, value)
  @filters << filter_factory.gte(field, value)
  self
end

#in(field, value) ⇒ Object



44
45
46
47
# File 'lib/memorydb/cursor.rb', line 44

def in(field, value)
  @filters << filter_factory.in(field, value)
  self
end

#lastObject



118
119
120
121
122
123
124
# File 'lib/memorydb/cursor.rb', line 118

def last
  query_executor.execute_find(
    query.merge(
      sorts: sorts_for_last,
      limit: 1
    )).first
end

#like(field, value) ⇒ Object



59
60
61
62
# File 'lib/memorydb/cursor.rb', line 59

def like(field, value)
  @filters << filter_factory.like(field, value)
  self
end

#limit(limit) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/memorydb/cursor.rb', line 86

def limit(limit)
  if limit
    assert_int!('Limit', limit)
    @limit = limit.to_i
  end
  self
end

#lt(field, value) ⇒ Object



24
25
26
27
# File 'lib/memorydb/cursor.rb', line 24

def lt(field, value)
  @filters << filter_factory.lt(field, value)
  self
end

#lte(field, value) ⇒ Object



29
30
31
32
# File 'lib/memorydb/cursor.rb', line 29

def lte(field, value)
  @filters << filter_factory.lte(field, value)
  self
end

#not(filter) ⇒ Object



74
75
76
77
# File 'lib/memorydb/cursor.rb', line 74

def not(filter)
  @filters << filter_factory.not(filter)
  self
end

#not_eq(field, value) ⇒ Object



19
20
21
22
# File 'lib/memorydb/cursor.rb', line 19

def not_eq(field, value)
  @filters << filter_factory.not_eq(field, value)
  self
end

#not_in(field, value) ⇒ Object



54
55
56
57
# File 'lib/memorydb/cursor.rb', line 54

def not_in(field, value)
  @filters << filter_factory.not_in(field, value)
  self
end

#offset(offset) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/memorydb/cursor.rb', line 94

def offset(offset)
  if offset
    assert_int!('Offset', offset)
    @offset = offset.to_i
  end
  self
end

#or(*filters) ⇒ Object



64
65
66
67
# File 'lib/memorydb/cursor.rb', line 64

def or(*filters)
  @filters << filter_factory.or(*filters)
  self
end

#queryObject



137
138
139
140
141
142
143
144
145
# File 'lib/memorydb/cursor.rb', line 137

def query
  {
    :type    => @type,
    :filters => @filters,
    :sorts   => @sorts,
    :offset  => @offset,
    :limit   => @limit
  }
end

#remove!Object



126
127
128
# File 'lib/memorydb/cursor.rb', line 126

def remove!
  query_executor.execute_remove!(query)
end

#sort(field, order) ⇒ Object



79
80
81
82
83
84
# File 'lib/memorydb/cursor.rb', line 79

def sort(field, order)
  assert_field!(field)
  assert_order!(order)
  @sorts << Sort.new(field, order.to_sym)
  self
end