Module: ActiveRecord::Refined::AST::Predications

Included in:
Aggregate, Arithmetic, Cast, Column, DatetimeValueFunction, Extract, Function, Value
Defined in:
lib/active_record/refined/ast.rb

Overview

Predicate builders shared by symbols, qualified columns and expressions. Imported into the Symbol refinement with Refinement#import_methods, so every method must be defined with def.

Instance Method Summary collapse

Instance Method Details

#!=(other) ⇒ Object



35
36
37
38
39
40
# File 'lib/active_record/refined/ast.rb', line 35

def !=(other)
  if other.nil?
    raise ArgumentError, "!= does not take nil; use !null? instead"
  end
  Comparison.new(self, :!=, other)
end

#!~(pattern) ⇒ Object



62
63
64
# File 'lib/active_record/refined/ast.rb', line 62

def !~(pattern)
  Match.new(self, pattern, negated: true)
end

#<(other) ⇒ Object



50
51
52
# File 'lib/active_record/refined/ast.rb', line 50

def <(other)
  Comparison.new(self, :<, other)
end

#<=(other) ⇒ Object



54
55
56
# File 'lib/active_record/refined/ast.rb', line 54

def <=(other)
  Comparison.new(self, :<=, other)
end

#==(other) ⇒ Object

and != mean SQL = and <>, and = NULL is never true there, so nil

is rejected rather than silently rewritten to IS NULL. null? builds its node directly and stays clear of this check.



28
29
30
31
32
33
# File 'lib/active_record/refined/ast.rb', line 28

def ==(other)
  if other.nil?
    raise ArgumentError, "== does not take nil; use null? instead"
  end
  Comparison.new(self, :==, other)
end

#=~(pattern) ⇒ Object



58
59
60
# File 'lib/active_record/refined/ast.rb', line 58

def =~(pattern)
  Match.new(self, pattern)
end

#>(other) ⇒ Object



42
43
44
# File 'lib/active_record/refined/ast.rb', line 42

def >(other)
  Comparison.new(self, :>, other)
end

#>=(other) ⇒ Object



46
47
48
# File 'lib/active_record/refined/ast.rb', line 46

def >=(other)
  Comparison.new(self, :>=, other)
end

#between?(min, max) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/active_record/refined/ast.rb', line 86

def between?(min, max)
  In.new(self, min..max)
end

#casecmp?(value) ⇒ Boolean

Case-insensitive equality, folded on both sides rather than left to the collation, so it means the same thing on every adapter.

Returns:

  • (Boolean)


112
113
114
115
116
117
118
# File 'lib/active_record/refined/ast.rb', line 112

def casecmp?(value)
  if value.nil?
    raise ArgumentError, "casecmp? does not take nil; use null? instead"
  end
  Comparison.new(Function.new("LOWER", [self]), :==,
                 Function.new("LOWER", [value]))
end

#distinct_from?(value) ⇒ Boolean

Null-safe comparison: unlike = and <>, these treat NULL as a value, so not_distinct_from? is the one equality that may take nil.

Returns:

  • (Boolean)


122
123
124
# File 'lib/active_record/refined/ast.rb', line 122

def distinct_from?(value)
  DistinctFrom.new(self, value, negated: true)
end

#end_with?(*suffixes) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
140
141
142
# File 'lib/active_record/refined/ast.rb', line 137

def end_with?(*suffixes)
  if suffixes.empty?
    raise ArgumentError, "end_with? needs at least one suffix"
  end
  Like.any(self, suffixes.map {|suffix| "%#{Like.escape(suffix)}" })
end

#ilike?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/active_record/refined/ast.rb', line 102

def ilike?(pattern)
  Like.new(self, pattern, nil, case_sensitive: false)
end

#in?(values) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/active_record/refined/ast.rb', line 78

def in?(values)
  In.new(self, values)
end

#include?(substring) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/active_record/refined/ast.rb', line 144

def include?(substring)
  Like.new(self, "%#{Like.escape(substring)}%", Like::ESCAPE)
end

#intersect?(elements) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/active_record/refined/ast.rb', line 168

def intersect?(elements)
  ArrayPredicate.new(self, :"&&", ArrayPredicate.elements(elements, "intersect?"))
end

#like?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/active_record/refined/ast.rb', line 94

def like?(pattern)
  Like.new(self, pattern)
end

#member?(element) ⇒ Boolean

The array comparisons carry the meaning of their Ruby namesakes. member? is Enumerable's element test, so an Array argument is rejected rather than quietly meaning something Array#member? does not; whole-array comparisons go by the Set and Array names.

Returns:

  • (Boolean)


152
153
154
155
156
157
158
# File 'lib/active_record/refined/ast.rb', line 152

def member?(element)
  if element.is_a?(::Array) || element.is_a?(::Set)
    raise ArgumentError,
      "member? takes a single element; use superset? to require every element"
  end
  ArrayPredicate.new(self, :"@>", [element])
end

#not_between?(min, max) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/active_record/refined/ast.rb', line 90

def not_between?(min, max)
  In.new(self, min..max, negated: true)
end

#not_distinct_from?(value) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/active_record/refined/ast.rb', line 126

def not_distinct_from?(value)
  DistinctFrom.new(self, value)
end

#not_ilike?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/active_record/refined/ast.rb', line 106

def not_ilike?(pattern)
  Like.new(self, pattern, nil, case_sensitive: false, negated: true)
end

#not_in?(values) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/active_record/refined/ast.rb', line 82

def not_in?(values)
  In.new(self, values, negated: true)
end

#not_like?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/active_record/refined/ast.rb', line 98

def not_like?(pattern)
  Like.new(self, pattern, negated: true)
end

#not_null?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/active_record/refined/ast.rb', line 74

def not_null?
  Comparison.new(self, :!=, nil)
end

#null?Boolean

! negates any predicate, so these are here for the four that SQL spells for itself: IS NOT NULL rather than NOT (... IS NULL), and likewise NOT IN and NOT LIKE. They mean the same thing either way, including when the column is NULL; what they save is the reading.

Returns:

  • (Boolean)


70
71
72
# File 'lib/active_record/refined/ast.rb', line 70

def null?
  Comparison.new(self, :==, nil)
end

#start_with?(*prefixes) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
# File 'lib/active_record/refined/ast.rb', line 130

def start_with?(*prefixes)
  if prefixes.empty?
    raise ArgumentError, "start_with? needs at least one prefix"
  end
  Like.any(self, prefixes.map {|prefix| "#{Like.escape(prefix)}%" })
end

#subset?(elements) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/active_record/refined/ast.rb', line 164

def subset?(elements)
  ArrayPredicate.new(self, :"<@", ArrayPredicate.elements(elements, "subset?"))
end

#superset?(elements) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/active_record/refined/ast.rb', line 160

def superset?(elements)
  ArrayPredicate.new(self, :"@>", ArrayPredicate.elements(elements, "superset?"))
end