Class: Rectify::Query
- Inherits:
-
Object
show all
- Defined in:
- lib/rectify/query.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(scope = ActiveRecord::NullRelation) ⇒ Query
Returns a new instance of Query.
7
8
9
|
# File 'lib/rectify/query.rb', line 7
def initialize(scope = ActiveRecord::NullRelation)
@scope = scope
end
|
Class Method Details
.merge(*queries) ⇒ Object
3
4
5
|
# File 'lib/rectify/query.rb', line 3
def self.merge(*queries)
queries.reduce(NullQuery.new) { |a, e| a.merge(e) }
end
|
Instance Method Details
#cached_query ⇒ Object
61
62
63
|
# File 'lib/rectify/query.rb', line 61
def cached_query
@cached_query ||= query
end
|
#count ⇒ Object
27
28
29
|
# File 'lib/rectify/query.rb', line 27
def count
cached_query.count
end
|
#each(&block) ⇒ Object
35
36
37
|
# File 'lib/rectify/query.rb', line 35
def each(&block)
cached_query.each(&block)
end
|
#eager? ⇒ Boolean
57
58
59
|
# File 'lib/rectify/query.rb', line 57
def eager?
cached_query.is_a?(Array)
end
|
#exists? ⇒ Boolean
39
40
41
42
43
|
# File 'lib/rectify/query.rb', line 39
def exists?
return cached_query.exists? if relation?
cached_query.present?
end
|
#first ⇒ Object
31
32
33
|
# File 'lib/rectify/query.rb', line 31
def first
cached_query.first
end
|
#none? ⇒ Boolean
45
46
47
|
# File 'lib/rectify/query.rb', line 45
def none?
!exists?
end
|
#query ⇒ Object
11
12
13
|
# File 'lib/rectify/query.rb', line 11
def query
@scope
end
|
#relation? ⇒ Boolean
53
54
55
|
# File 'lib/rectify/query.rb', line 53
def relation?
cached_query.is_a?(ActiveRecord::Relation)
end
|
#to_a ⇒ Object
49
50
51
|
# File 'lib/rectify/query.rb', line 49
def to_a
cached_query.to_a
end
|
#|(other) ⇒ Object
Also known as:
merge
15
16
17
18
19
20
21
22
23
|
# File 'lib/rectify/query.rb', line 15
def |(other)
if relation? && other.relation?
Rectify::Query.new(cached_query.merge(other.cached_query))
elsif eager? && other.eager?
Rectify::Query.new(cached_query | other.cached_query)
else
fail UnableToComposeQueries.new(self, other)
end
end
|