Class: Rectify::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/rectify/query.rb

Direct Known Subclasses

NullQuery, RSpec::StubQuery

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_queryObject



61
62
63
# File 'lib/rectify/query.rb', line 61

def cached_query
  @cached_query ||= query
end

#countObject



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

Returns:

  • (Boolean)


57
58
59
# File 'lib/rectify/query.rb', line 57

def eager?
  cached_query.is_a?(Array)
end

#exists?Boolean

Returns:

  • (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

#firstObject



31
32
33
# File 'lib/rectify/query.rb', line 31

def first
  cached_query.first
end

#none?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/rectify/query.rb', line 45

def none?
  !exists?
end

#queryObject



11
12
13
# File 'lib/rectify/query.rb', line 11

def query
  @scope
end

#relation?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rectify/query.rb', line 53

def relation?
  cached_query.is_a?(ActiveRecord::Relation)
end

#to_aObject



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