predications.rb 5.6 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
      if infinity?(other.begin)
G
Greg Navis 已提交
39
        if other.end.nil? || infinity?(other.end)
40 41 42 43 44 45
          not_in([])
        elsif other.exclude_end?
          lt(other.end)
        else
          lteq(other.end)
        end
G
Greg Navis 已提交
46
      elsif other.end.nil? || infinity?(other.end)
47 48 49 50
        gteq(other.begin)
      elsif other.exclude_end?
        gteq(other.begin).and(lt(other.end))
      else
51 52
        left = quoted_node(other.begin)
        right = quoted_node(other.end)
53 54
        Nodes::Between.new(self, left.and(right))
      end
S
Sean Griffin 已提交
55 56
    end

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

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

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

M
Matthew Draper 已提交
83
    def not_between(other)
84
      if infinity?(other.begin)
85
        if other.end.nil? || infinity?(other.end)
86 87 88 89 90 91
          self.in([])
        elsif other.exclude_end?
          gteq(other.end)
        else
          gt(other.end)
        end
92
      elsif other.end.nil? || infinity?(other.end)
93 94 95 96 97 98 99 100 101 102 103 104
        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 已提交
105
    def not_in(other)
106 107
      case other
      when Arel::SelectManager
108
        Arel::Nodes::NotIn.new(self, other.ast)
109
      when Range
110 111 112 113
        if $VERBOSE
          warn <<-eowarn
Passing a range to `#not_in` is deprecated. Call `#not_between`, instead.
          eowarn
114
        end
115
        not_between(other)
116
      when Enumerable
117
        Nodes::NotIn.new self, quoted_array(other)
118
      else
119
        Nodes::NotIn.new self, quoted_node(other)
120 121 122
      end
    end

M
Matthew Draper 已提交
123
    def not_in_any(others)
124 125 126
      grouping_any :not_in, others
    end

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

M
Matthew Draper 已提交
131
    def matches(other, escape = nil, case_sensitive = false)
K
Keenan Brock 已提交
132
      Nodes::Matches.new self, quoted_node(other), escape, case_sensitive
133 134
    end

M
Matthew Draper 已提交
135
    def matches_regexp(other, case_sensitive = true)
136 137 138
      Nodes::Regexp.new self, quoted_node(other), case_sensitive
    end

M
Matthew Draper 已提交
139
    def matches_any(others, escape = nil, case_sensitive = false)
K
Keenan Brock 已提交
140
      grouping_any :matches, others, escape, case_sensitive
141 142
    end

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

M
Matthew Draper 已提交
147
    def does_not_match(other, escape = nil, case_sensitive = false)
K
Keenan Brock 已提交
148
      Nodes::DoesNotMatch.new self, quoted_node(other), escape, case_sensitive
149 150
    end

M
Matthew Draper 已提交
151
    def does_not_match_regexp(other, case_sensitive = true)
152 153 154
      Nodes::NotRegexp.new self, quoted_node(other), case_sensitive
    end

M
Matthew Draper 已提交
155
    def does_not_match_any(others, escape = nil)
156
      grouping_any :does_not_match, others, escape
157 158
    end

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

M
Matthew Draper 已提交
163
    def gteq(right)
164
      Nodes::GreaterThanOrEqual.new self, quoted_node(right)
165 166
    end

M
Matthew Draper 已提交
167
    def gteq_any(others)
168 169 170
      grouping_any :gteq, others
    end

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

M
Matthew Draper 已提交
175
    def gt(right)
176
      Nodes::GreaterThan.new self, quoted_node(right)
177 178
    end

M
Matthew Draper 已提交
179
    def gt_any(others)
180 181 182
      grouping_any :gt, others
    end

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

M
Matthew Draper 已提交
187
    def lt(right)
188
      Nodes::LessThan.new self, quoted_node(right)
189 190
    end

M
Matthew Draper 已提交
191
    def lt_any(others)
192 193 194
      grouping_any :lt, others
    end

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

M
Matthew Draper 已提交
199
    def lteq(right)
200
      Nodes::LessThanOrEqual.new self, quoted_node(right)
201 202
    end

M
Matthew Draper 已提交
203
    def lteq_any(others)
204 205 206
      grouping_any :lteq, others
    end

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

M
Matthew Draper 已提交
211
    def when(right)
212 213 214
      Nodes::Case.new(self).when quoted_node(right)
    end

M
Matthew Draper 已提交
215
    def concat(other)
216 217 218
      Nodes::Concat.new self, other
    end

219 220
    private

M
Matthew Draper 已提交
221 222 223 224 225 226
      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
227

M
Matthew Draper 已提交
228 229 230 231
      def grouping_all(method_id, others, *extras)
        nodes = others.map { |expr| send(method_id, expr, *extras) }
        Nodes::Grouping.new Nodes::And.new(nodes)
      end
232

M
Matthew Draper 已提交
233 234 235
      def quoted_node(other)
        Nodes.build_quoted(other, self)
      end
236

M
Matthew Draper 已提交
237 238 239
      def quoted_array(others)
        others.map { |v| quoted_node(v) }
      end
240

241 242
      def infinity?(value)
        value.respond_to?(:infinite?) && value.infinite?
243
      end
244
  end
A
Aaron Patterson 已提交
245
end