Module: ArelExtensions::Predications

Included in:
Attributes, Nodes::Function
Defined in:
lib/arel_extensions/predications.rb

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



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

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



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

def in(other) #In should handle nil element in the Array
  case other
  when Range
    self.between(other)
  when Enumerable
    if other.include?(nil)
      other.delete(nil)
      case other.length
      when 0
        self.is_null
      when 1
        self.is_null.or(self==other[0])
      else
        self.is_null.or(Arel::Nodes::In.new(self,quoted_array(other)))
      end
    else
      Arel::Nodes::In.new(self,quoted_array(other))
    end
  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::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



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

def not_in(other) #In should handle nil element in the Array
  case other
  when Range
    Arel::Nodes::Not.new(self.between(other))
  when Enumerable
    if other.include?(nil)
      other.delete(nil)
      case other.length
      when 0
        self.is_not_null
      when 1
        self.is_not_null.and(self!=other[0])
      else
        self.is_not_null.and(Arel::Nodes::NotIn.new(self,quoted_array(other)))
      end
    else
      Arel::Nodes::NotIn.new(self,quoted_array(other))
    end
  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