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



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

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



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 52

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