Class: Repository::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/repository/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/repository/cursor.rb', line 8

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

Instance Method Details

#allObject



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

def all
  query_executor.execute_find(query)
end

#countObject



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

def count
  query_executor.execute_count(query)
end

#eq(field, value) ⇒ Object



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

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

#firstObject



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

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/repository/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/repository/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/repository/cursor.rb', line 44

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

#lastObject



103
104
105
106
107
108
109
# File 'lib/repository/cursor.rb', line 103

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

#like(field, value) ⇒ Object



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

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

#limit(limit) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/repository/cursor.rb', line 71

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/repository/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/repository/cursor.rb', line 29

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

#not_eq(field, value) ⇒ Object



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

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

#not_in(field, value) ⇒ Object



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

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

#offset(offset) ⇒ Object



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

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

#or(*filters) ⇒ Object



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

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

#queryObject



122
123
124
125
126
127
128
129
130
# File 'lib/repository/cursor.rb', line 122

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

#remove!Object



111
112
113
# File 'lib/repository/cursor.rb', line 111

def remove!
  query_executor.execute_remove!(query)
end

#sort(field, order) ⇒ Object



64
65
66
67
68
69
# File 'lib/repository/cursor.rb', line 64

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