Class: ArelExtensions::Nodes::Function

Inherits:
Arel::Nodes::Function
  • Object
show all
Includes:
Arel::Expressions, Arel::Math, Arel::OrderPredications, Predications
Defined in:
lib/arel_extensions/nodes/function.rb

Constant Summary collapse

RETURN_TYPE =

by default…

:string

Instance Method Summary collapse

Methods included from Predications

#cast, #imatches, #in, #matches, #not_in, #when

Instance Method Details

#as(other) ⇒ Object



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

def as other
  ArelExtensions::Nodes::As.new(self, Arel.sql(other))
end

#convert_to_date_node(object) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/arel_extensions/nodes/function.rb', line 104

def convert_to_date_node(object)
  case object
  when Arel::Attributes::Attribute, Arel::Nodes::Node
    object
  when DateTime, Time
    Arel::Nodes.build_quoted(Date.new(object.year, object.month, object.day), self)
  when String
    Arel::Nodes.build_quoted(Date.parse(object), self)
  when Date
    Arel::Nodes.build_quoted(object, self)
  else
    raise(ArgumentError, "#{object.class} can not be converted to Date")
  end
end

#convert_to_datetime_node(object) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/arel_extensions/nodes/function.rb', line 119

def convert_to_datetime_node(object)
  case object
  when Arel::Attributes::Attribute, Arel::Nodes::Node
    object
  when DateTime, Time
    Arel::Nodes.build_quoted(object, self)
  when String
    Arel::Nodes.build_quoted(Time.parse(object), self)
  when Date
    Arel::Nodes.build_quoted(Time.utc(object.year, object.month, object.day, 0, 0, 0), self)
  else
    raise(ArgumentError, "#{object.class} can not be converted to Datetime")
  end
end

#convert_to_node(object) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/arel_extensions/nodes/function.rb', line 51

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

#convert_to_number(object) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/arel_extensions/nodes/function.rb', line 134

def convert_to_number(object)
  case object
  when Arel::Attributes::Attribute, Arel::Nodes::Node
    object
  when Integer
    object.to_i.abs
  when DateTime, Date, Time, String, ActiveSupport::Duration
    object.to_i.abs
  when NilClass
    0
  else
    raise(ArgumentError, "#{object.class} can not be converted to NUMBER arg")
  end
end

#convert_to_string_node(object) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/arel_extensions/nodes/function.rb', line 72

def convert_to_string_node(object)
  case object
  when Arel::Nodes::Node
    object
  when Integer
    Arel::Nodes.build_quoted(object.to_s)
  when Arel::Attributes::Attribute
    case self.type_of_attribute(object)
    when :date
      ArelExtensions::Nodes::Format.new [object, 'yyyy-mm-dd']
    when :time
      ArelExtensions::Nodes::Format.new [object, '%H:%M:%S']
    else
      object
    end
  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, self)
  when NilClass
    Arel.sql('NULL')
  when ActiveSupport::Duration
    Arel::Nodes.build_quoted(object.to_i.to_s)
  else
    raise(ArgumentError, "#{object.class} can not be converted to CONCAT arg")
  end
end

#exprObject



24
25
26
# File 'lib/arel_extensions/nodes/function.rb', line 24

def expr
 	@expressions.first
end

#leftObject



28
29
30
# File 'lib/arel_extensions/nodes/function.rb', line 28

def left
  @expressions.first
end

#return_typeObject

overrides as to make new Node like AliasPredication



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

def return_type 
		self.class.const_get(:RETURN_TYPE)
end

#rightObject



32
33
34
# File 'lib/arel_extensions/nodes/function.rb', line 32

def right
  @expressions[1]
end

#type_of_attribute(att) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/arel_extensions/nodes/function.rb', line 36

def type_of_attribute(att)				
  case att
  when Arel::Attributes::Attribute
			begin  
				Arel::Table.engine.connection.schema_cache.columns_hash(att.relation.table_name)[att.name.to_s].type
			rescue
				att
			end
  when ArelExtensions::Nodes::Function
    att.return_type
#        else
#          nil
  end
end