Class: Rectify::Query
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- 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.
9
10
11
|
# File 'lib/rectify/query.rb', line 9
def initialize(scope = ActiveRecord::NullRelation)
@scope = scope
end
|
Class Method Details
.merge(*queries) ⇒ Object
5
6
7
|
# File 'lib/rectify/query.rb', line 5
def self.merge(*queries)
queries.reduce(NullQuery.new) { |a, e| a.merge(e) }
end
|
Instance Method Details
#cached_query ⇒ Object
65
66
67
|
# File 'lib/rectify/query.rb', line 65
def cached_query
@cached_query ||= query
end
|
#count ⇒ Object
29
30
31
|
# File 'lib/rectify/query.rb', line 29
def count
cached_query.count
end
|
#each(&block) ⇒ Object
37
38
39
|
# File 'lib/rectify/query.rb', line 37
def each(&block)
cached_query.each(&block)
end
|
#eager? ⇒ Boolean
61
62
63
|
# File 'lib/rectify/query.rb', line 61
def eager?
cached_query.is_a?(Array)
end
|
#exists? ⇒ Boolean
41
42
43
44
45
|
# File 'lib/rectify/query.rb', line 41
def exists?
return cached_query.exists? if relation?
cached_query.present?
end
|
#first ⇒ Object
33
34
35
|
# File 'lib/rectify/query.rb', line 33
def first
cached_query.first
end
|
#none? ⇒ Boolean
47
48
49
|
# File 'lib/rectify/query.rb', line 47
def none?
!exists?
end
|
#query ⇒ Object
13
14
15
|
# File 'lib/rectify/query.rb', line 13
def query
@scope
end
|
#relation? ⇒ Boolean
57
58
59
|
# File 'lib/rectify/query.rb', line 57
def relation?
cached_query.is_a?(ActiveRecord::Relation)
end
|
#to_a ⇒ Object
Also known as:
to_ary
51
52
53
|
# File 'lib/rectify/query.rb', line 51
def to_a
cached_query.to_a
end
|
#|(other) ⇒ Object
Also known as:
merge
17
18
19
20
21
22
23
24
25
|
# File 'lib/rectify/query.rb', line 17
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
raise UnableToComposeQueries.new(self, other)
end
end
|