Module: ArelExtensions::Predications

Instance Method Summary collapse

Instance Method Details

#cast(right) ⇒ Object



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

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

#convert_to_node(object) ⇒ Object



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

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

#imatches(other, escape = nil) ⇒ Object



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

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

#in(*other) ⇒ Object

In should handle nil element in the Array



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

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
    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



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

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

#not_in(*other) ⇒ Object

In should handle nil element in the Array



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

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
    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



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

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