Class: Mongoid::Criteria

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

Instance Method Summary collapse

Instance Method Details

#ruby_orObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mongoid_ruby_or.rb', line 3

def ruby_or
  selector   = self.selector.dup
  or_clauses = selector.delete('$or')
  limit      = options[:limit]
  sort       = options[:sort]
  criteria   = klass.where(selector).limit(limit)
  results    = []

  or_clauses.each do |or_clause|
    results += criteria.where(or_clause).sort(sort).to_a
  end

  results.uniq!

  if sort
    field, direction = sort.first
    if direction == 1
      results.sort! { |x,y| x[field] <=> y[field] }
    else
      results.sort! { |x,y| y[field] <=> x[field] }
    end
  end

  limit ? results[0...limit] : results
end