aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/predicates.rb
blob: fba1cc90b6cd14b79d05059b9aeaafca687235cf (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
module ActiveRelation
  module Predicates
    class Base
      def ==(other)
        self.class == other.class
      end
    end
  
    class Binary < Base
      attr_reader :attribute1, :attribute2

      def initialize(attribute1, attribute2)
        @attribute1, @attribute2 = attribute1, attribute2
      end

      def ==(other)
        super and @attribute1 == other.attribute1 and @attribute2 == other.attribute2
      end

      def qualify
        self.class.new(attribute1.qualify, attribute2.qualify)
      end

      def to_sql(options = {})
        "#{attribute1.to_sql} #{predicate_sql} #{attribute2.to_sql}"
      end
    end
  
    class Equality < Binary
      def ==(other)
        self.class == other.class and
          ((attribute1 == other.attribute1 and attribute2 == other.attribute2) or
           (attribute1 == other.attribute2 and attribute2 == other.attribute1))
      end

      protected
      def predicate_sql
        '='
      end
    end
  
    class GreaterThanOrEqualTo < Binary
      protected
      def predicate_sql
        '>='
      end
    end
  
    class GreaterThan < Binary
      protected
      def predicate_sql
        '>'
      end
    end
  
    class LessThanOrEqualTo < Binary
      protected
      def predicate_sql
        '<='
      end
    end
  
    class LessThan < Binary
      protected
      def predicate_sql
        '<'
      end
    end
  
    class Match < Base
      attr_reader :attribute, :regexp

      def initialize(attribute, regexp)
        @attribute, @regexp = attribute, regexp
      end
    end
  
    class RelationInclusion < Base
      attr_reader :attribute, :relation

      def initialize(attribute, relation)
        @attribute, @relation = attribute, relation
      end

      def ==(other)
        super and attribute == other.attribute and relation == other.relation
      end
      
      def to_sql(options = {})
        "#{attribute.to_sql} IN (#{relation.to_sql})"
      end
    end
  end
end