predications.rb 6.0 KB
Newer Older
1
# frozen_string_literal: true
M
Matthew Draper 已提交
2

M
Matthew Draper 已提交
3
module Arel # :nodoc: all
4
  module Predications
M
Matthew Draper 已提交
5
    def not_eq(other)
6
      Nodes::NotEqual.new self, quoted_node(other)
7 8
    end

M
Matthew Draper 已提交
9
    def not_eq_any(others)
10 11 12
      grouping_any :not_eq, others
    end

M
Matthew Draper 已提交
13
    def not_eq_all(others)
14 15 16
      grouping_all :not_eq, others
    end

M
Matthew Draper 已提交
17
    def eq(other)
18
      Nodes::Equality.new self, quoted_node(other)
19 20
    end

21 22 23 24 25 26 27 28
    def is_not_distinct_from(other)
      Nodes::IsNotDistinctFrom.new self, quoted_node(other)
    end

    def is_distinct_from(other)
      Nodes::IsDistinctFrom.new self, quoted_node(other)
    end

M
Matthew Draper 已提交
29
    def eq_any(others)
30 31 32
      grouping_any :eq, others
    end

M
Matthew Draper 已提交
33
    def eq_all(others)
34
      grouping_all :eq, quoted_array(others)
35 36
    end

M
Matthew Draper 已提交
37
    def between(other)
38 39 40 41
      if unboundable?(other.begin) == 1 || unboundable?(other.end) == -1
        self.in([])
      elsif open_ended?(other.begin)
        if other.end.nil? || open_ended?(other.end)
42 43 44 45 46 47
          not_in([])
        elsif other.exclude_end?
          lt(other.end)
        else
          lteq(other.end)
        end
48
      elsif other.end.nil? || open_ended?(other.end)
49 50 51 52
        gteq(other.begin)
      elsif other.exclude_end?
        gteq(other.begin).and(lt(other.end))
      else
53 54
        left = quoted_node(other.begin)
        right = quoted_node(other.end)
55 56
        Nodes::Between.new(self, left.and(right))
      end
S
Sean Griffin 已提交
57 58
    end

M
Matthew Draper 已提交
59
    def in(other)
60 61
      case other
      when Arel::SelectManager
62
        Arel::Nodes::In.new(self, other.ast)
63
      when Range
64 65 66 67
        if $VERBOSE
          warn <<-eowarn
Passing a range to `#in` is deprecated. Call `#between`, instead.
          eowarn
68
        end
69
        between(other)
70
      when Enumerable
71
        Nodes::In.new self, quoted_array(other)
72
      else
73
        Nodes::In.new self, quoted_node(other)
74 75 76
      end
    end

M
Matthew Draper 已提交
77
    def in_any(others)
78 79 80
      grouping_any :in, others
    end

M
Matthew Draper 已提交
81
    def in_all(others)
82 83 84
      grouping_all :in, others
    end

M
Matthew Draper 已提交
85
    def not_between(other)
86 87 88 89
      if unboundable?(other.begin) == 1 || unboundable?(other.end) == -1
        not_in([])
      elsif open_ended?(other.begin)
        if other.end.nil? || open_ended?(other.end)
90 91 92 93 94 95
          self.in([])
        elsif other.exclude_end?
          gteq(other.end)
        else
          gt(other.end)
        end
96
      elsif other.end.nil? || open_ended?(other.end)
97 98 99 100 101 102 103 104 105 106 107 108
        lt(other.begin)
      else
        left = lt(other.begin)
        right = if other.exclude_end?
          gteq(other.end)
        else
          gt(other.end)
        end
        left.or(right)
      end
    end

M
Matthew Draper 已提交
109
    def not_in(other)
110 111
      case other
      when Arel::SelectManager
112
        Arel::Nodes::NotIn.new(self, other.ast)
113
      when Range
114 115 116 117
        if $VERBOSE
          warn <<-eowarn
Passing a range to `#not_in` is deprecated. Call `#not_between`, instead.
          eowarn
118
        end
119
        not_between(other)
120
      when Enumerable
121
        Nodes::NotIn.new self, quoted_array(other)
122
      else
123
        Nodes::NotIn.new self, quoted_node(other)
124 125 126
      end
    end

M
Matthew Draper 已提交
127
    def not_in_any(others)
128 129 130
      grouping_any :not_in, others
    end

M
Matthew Draper 已提交
131
    def not_in_all(others)
132 133 134
      grouping_all :not_in, others
    end

M
Matthew Draper 已提交
135
    def matches(other, escape = nil, case_sensitive = false)
K
Keenan Brock 已提交
136
      Nodes::Matches.new self, quoted_node(other), escape, case_sensitive
137 138
    end

M
Matthew Draper 已提交
139
    def matches_regexp(other, case_sensitive = true)
140 141 142
      Nodes::Regexp.new self, quoted_node(other), case_sensitive
    end

M
Matthew Draper 已提交
143
    def matches_any(others, escape = nil, case_sensitive = false)
K
Keenan Brock 已提交
144
      grouping_any :matches, others, escape, case_sensitive
145 146
    end

M
Matthew Draper 已提交
147
    def matches_all(others, escape = nil, case_sensitive = false)
K
Keenan Brock 已提交
148
      grouping_all :matches, others, escape, case_sensitive
149 150
    end

M
Matthew Draper 已提交
151
    def does_not_match(other, escape = nil, case_sensitive = false)
K
Keenan Brock 已提交
152
      Nodes::DoesNotMatch.new self, quoted_node(other), escape, case_sensitive
153 154
    end

M
Matthew Draper 已提交
155
    def does_not_match_regexp(other, case_sensitive = true)
156 157 158
      Nodes::NotRegexp.new self, quoted_node(other), case_sensitive
    end

M
Matthew Draper 已提交
159
    def does_not_match_any(others, escape = nil)
160
      grouping_any :does_not_match, others, escape
161 162
    end

M
Matthew Draper 已提交
163
    def does_not_match_all(others, escape = nil)
164
      grouping_all :does_not_match, others, escape
165 166
    end

M
Matthew Draper 已提交
167
    def gteq(right)
168
      Nodes::GreaterThanOrEqual.new self, quoted_node(right)
169 170
    end

M
Matthew Draper 已提交
171
    def gteq_any(others)
172 173 174
      grouping_any :gteq, others
    end

M
Matthew Draper 已提交
175
    def gteq_all(others)
176 177 178
      grouping_all :gteq, others
    end

M
Matthew Draper 已提交
179
    def gt(right)
180
      Nodes::GreaterThan.new self, quoted_node(right)
181 182
    end

M
Matthew Draper 已提交
183
    def gt_any(others)
184 185 186
      grouping_any :gt, others
    end

M
Matthew Draper 已提交
187
    def gt_all(others)
188 189 190
      grouping_all :gt, others
    end

M
Matthew Draper 已提交
191
    def lt(right)
192
      Nodes::LessThan.new self, quoted_node(right)
193 194
    end

M
Matthew Draper 已提交
195
    def lt_any(others)
196 197 198
      grouping_any :lt, others
    end

M
Matthew Draper 已提交
199
    def lt_all(others)
200 201 202
      grouping_all :lt, others
    end

M
Matthew Draper 已提交
203
    def lteq(right)
204
      Nodes::LessThanOrEqual.new self, quoted_node(right)
205 206
    end

M
Matthew Draper 已提交
207
    def lteq_any(others)
208 209 210
      grouping_any :lteq, others
    end

M
Matthew Draper 已提交
211
    def lteq_all(others)
212 213 214
      grouping_all :lteq, others
    end

M
Matthew Draper 已提交
215
    def when(right)
216 217 218
      Nodes::Case.new(self).when quoted_node(right)
    end

M
Matthew Draper 已提交
219
    def concat(other)
220 221 222
      Nodes::Concat.new self, other
    end

223 224
    private

M
Matthew Draper 已提交
225 226 227 228 229 230
      def grouping_any(method_id, others, *extras)
        nodes = others.map { |expr| send(method_id, expr, *extras) }
        Nodes::Grouping.new nodes.inject { |memo, node|
          Nodes::Or.new(memo, node)
        }
      end
231

M
Matthew Draper 已提交
232 233 234 235
      def grouping_all(method_id, others, *extras)
        nodes = others.map { |expr| send(method_id, expr, *extras) }
        Nodes::Grouping.new Nodes::And.new(nodes)
      end
236

M
Matthew Draper 已提交
237 238 239
      def quoted_node(other)
        Nodes.build_quoted(other, self)
      end
240

M
Matthew Draper 已提交
241 242 243
      def quoted_array(others)
        others.map { |v| quoted_node(v) }
      end
244

245 246
      def infinity?(value)
        value.respond_to?(:infinite?) && value.infinite?
247
      end
248 249 250 251 252 253 254 255

      def unboundable?(value)
        value.respond_to?(:unboundable?) && value.unboundable?
      end

      def open_ended?(value)
        infinity?(value) || unboundable?(value)
      end
256
  end
A
Aaron Patterson 已提交
257
end