Module: ArelExtensions::Predications

Instance Method Summary collapse

Instance Method Details

#cast(right) ⇒ Object



19
20
21
# File 'lib/arel_extensions/predications.rb', line 19

def cast right
  ArelExtensions::Nodes::Cast.new([self, right])
end

#convert_to_node(object) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/arel_extensions/predications.rb', line 79

def convert_to_node(object)
  case object
  when Arel::Attributes::Attribute, Arel::Nodes::Node, Integer
    object
  when DateTime
    Arel.quoted(object, self)
  when Time
    Arel.quoted(object.strftime('%H:%M:%S'), self)
  when String
    Arel.quoted(object)
  when Date
    Arel.quoted(object.to_s, self)
  when NilClass
    Arel.sql('NULL')
  when ActiveSupport::Duration
    object.to_i
  else
    raise(ArgumentError, "#{object.class} cannot be converted to CONCAT arg")
  end
end

#imatches(other, escape = nil) ⇒ Object



15
16
17
# File 'lib/arel_extensions/predications.rb', line 15

def imatches(other, escape = nil)
  ArelExtensions::Nodes::IMatches.new(self, other, escape)
end

#in(*other) ⇒ Object

In should handle nil element in the Array



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
# File 'lib/arel_extensions/predications.rb', line 23

def in(*other) # In should handle nil element in the Array
  other = other.first if other.length <= 1
  case other
  when Range
    self.between(other)
  when Arel::Nodes::Grouping, ArelExtensions::Nodes::Union, ArelExtensions::Nodes::UnionAll
    Arel::Nodes::In.new(self, quoted_node(other))
  when Enumerable
    nils, values   = other.partition{ |v| v.nil? }
    ranges, values = values.partition{ |v| v.is_a?(Range) || v.is_a?(Arel::SelectManager)}
    # In order of (imagined) decreasing efficiency: nil, values, and then more complex.
    clauses =
      nils.uniq.map { |r| self.in(r) } \
      + (case values.uniq.size
          when 0 then []
          when 1 then [values[0].is_a?(Arel::Nodes::Grouping) ? self.in(values[0]) : self.eq(values[0])]
          else [Arel::Nodes::In.new(self, quoted_array(values))] end) \
      + ranges.uniq.map { |r| self.in(r) }
    clauses.empty? ? Arel.false : clauses.reduce(&:or)
  when nil
    self.is_null
  when Arel::SelectManager
    Arel::Nodes::In.new(self, other.ast)
  else
    Arel::Nodes::In.new(self, quoted_node(other))
  end
end

#matches(other, escape = nil, case_sensitive = nil) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/arel_extensions/predications.rb', line 7

def matches(other, escape = nil, case_sensitive = nil)
  if Arel::VERSION.to_i < 7
    Arel::Nodes::Matches.new(self, Arel.quoted(other), escape)
  else
    Arel::Nodes::Matches.new(self, Arel.quoted(other), escape, case_sensitive)
  end
end

#not_in(*other) ⇒ Object

In should handle nil element in the Array



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
# File 'lib/arel_extensions/predications.rb', line 51

def not_in(*other) # In should handle nil element in the Array
  other = other.first if other.length <= 1
  case other
  when Range
    Arel::Nodes::Not.new(self.between(other))
  when Arel::Nodes::Grouping, ArelExtensions::Nodes::Union, ArelExtensions::Nodes::UnionAll
    Arel::Nodes::NotIn.new(self, quoted_node(other))
  when Enumerable
    nils, values   = other.partition{ |v| v.nil? }
    ranges, values = values.partition{ |v| v.is_a?(Range) || v.is_a?(Arel::SelectManager)}
    # In order of (imagined) decreasing efficiency: nil, values, and then more complex.
    clauses =
      nils.uniq.map { |r| self.not_in(r) } \
      + (case values.uniq.size
          when 0 then []
          when 1 then [values[0].is_a?(Arel::Nodes::Grouping) ? self.not_in(values[0]) : self.not_eq(values[0])]
          else [Arel::Nodes::NotIn.new(self, quoted_array(values))] end) \
      + ranges.uniq.map { |r| self.not_in(r) }
    Arel::Nodes::And.new clauses
  when nil
    self.is_not_null
  when Arel::SelectManager
    Arel::Nodes::NotIn.new(self, other.ast)
  else
    Arel::Nodes::NotIn.new(self, quoted_node(other))
  end
end

#when(right, expression = nil) ⇒ Object



3
4
5
# File 'lib/arel_extensions/predications.rb', line 3

def when right, expression = nil
  ArelExtensions::Nodes::Case.new(self).when(right, expression)
end