aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/algebra/relations/operations/where.rb
blob: b447b18809960b9ef4bd8c08954af48f6e2b4624 (plain) (blame)
1
2
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
28
29
30
31
32
33
34
35
36
37
38
module Arel
  class Where < Compound
    attr_reader :predicates

    def initialize(relation, *predicates)
      super(relation)
      predicates = [yield(relation)] + predicates if block_given?
      @predicates = predicates.map { |p| p.bind(relation) }
      @wheres = nil
    end

    def wheres
      @wheres ||= relation.wheres + predicates
    end

    def == other
      super ||
        Where === other &&
        relation == other.relation &&
        predicates == other.predicates
    end

    def engine
      engine   = relation.engine

      # Temporary check of whether or not the engine supports where.
      if engine.respond_to?(:supports) && !engine.supports(:restricting)
        Memory::Engine.new
      else
        engine
      end
    end

    def eval
      unoperated_rows.select { |row| predicates.all? { |p| p.eval(row) } }
    end
  end
end