class Rectify::Query

def self.merge(*queries)

def self.merge(*queries)
  queries.reduce(NullQuery.new) { |a, e| a.merge(e) }
end

def cached_query

def cached_query
  @cached_query ||= query
end

def count

def count
  cached_query.count
end

def each(&block)

def each(&block)
  cached_query.each(&block)
end

def eager?

def eager?
  cached_query.is_a?(Array)
end

def exists?

def exists?
  return cached_query.exists? if relation?
  cached_query.present?
end

def first

def first
  cached_query.first
end

def initialize(scope = ActiveRecord::NullRelation)

def initialize(scope = ActiveRecord::NullRelation)
  @scope = scope
end

def none?

def none?
  !exists?
end

def query

def query
  @scope
end

def relation?

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

def to_a

def to_a
  cached_query.to_a
end

def |(other)

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