Class: ArelExtensions::Nodes::Function

Inherits:
Arel::Nodes::Function 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
MBSTRING =

Support multibyte string if they are available.

defined?(ActiveSupport::Multibyte::Chars) ? ActiveSupport::Multibyte::Chars : String

Instance Method Summary collapse

Methods included from Predications

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

Methods included from ArelExtensions::NullFunctions

#coalesce, #coalesce_blank, #if_present, #is_not_null, #is_null

Methods included from BooleanFunctions

#and, #or, #then, #⋀, #⋁

Methods included from StringFunctions

#&, #[], #ai_collate, #ai_imatches, #ai_matches, #blank, #byte_length, #char_length, #ci_collate, #collate, #concat, #downcase, #edit_distance, #group_concat, #idoes_not_match, #idoes_not_match_all, #idoes_not_match_any, #imatches, #imatches_all, #imatches_any, #length, #levenshtein_distance, #locate, #ltrim, #md5, #not_blank, #regexp_replace, #repeat, #replace, #rtrim, #smatches, #soundex, #substring, #trim, #upcase

Methods included from MathFunctions

#*, #/, #abs, #ceil, #floor, #format_number, #log10, #pow, #power, #round, #std, #sum, #variance

Methods included from DateDuration

#day, #format, #format_date, #hour, #minute, #month, #second, #wday, #week, #year

Methods included from Comparators

#!~, #<, #<=, #=~, #>, #>=

Methods included from Math

#+, #-

Methods included from Aliases

#xas

Instance Method Details

#!=(other) ⇒ Object



53
54
55
# File 'lib/arel_extensions/nodes/function.rb', line 53

def !=(other)
  Arel::Nodes::NotEqual.new self, Arel.quoted(other, self)
end

#==(other) ⇒ Object



49
50
51
# File 'lib/arel_extensions/nodes/function.rb', line 49

def ==(other)
  Arel::Nodes::Equality.new self, Arel.quoted(other, self)
end

#as(other) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/arel_extensions/nodes/function.rb', line 29

def as other
  res = Arel::Nodes::As.new(self.clone, Arel.sql(other))
  if Gem::Version.new(Arel::VERSION) >= Gem::Version.new('9.0.0')
    self.alias = Arel.sql(other)
  end
  res
end

#convert_to_date_node(object) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/arel_extensions/nodes/function.rb', line 123

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

#convert_to_datetime_node(object) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/arel_extensions/nodes/function.rb', line 138

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

#convert_to_node(object) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/arel_extensions/nodes/function.rb', line 68

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 MBSTRING, String, Symbol
    Arel.quoted(object.to_s)
  when Date
    Arel.quoted(object.to_s, self)
  when NilClass
    Arel.sql('NULL')
  when ActiveSupport::Duration
    Arel.sql(object.to_i)
  when Array
    Arel.grouping(object.map{|e| convert_to_node(e)})
  else
    raise(ArgumentError, "#{object.class} cannot be converted to CONCAT arg")
  end
end

#convert_to_number(object) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/arel_extensions/nodes/function.rb', line 153

def convert_to_number(object)
  case object
  when ArelExtensions::Nodes::Duration
    object.with_interval = true
    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} cannot be converted to NUMBER arg")
  end
end

#convert_to_string_node(object) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/arel_extensions/nodes/function.rb', line 91

def convert_to_string_node(object)
  case object
  when Arel::Nodes::Node
    object
  when Integer
    Arel.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.quoted(object, self)
  when Time
    Arel.quoted(object.strftime('%H:%M:%S'), self)
  when MBSTRING, String
    Arel.quoted(object.to_s)
  when Date
    Arel.quoted(object, self)
  when NilClass
    Arel.sql(nil)
  when ActiveSupport::Duration
    Arel.quoted(object.to_i.to_s)
  else
    raise(ArgumentError, "#{object.class} cannot be converted to CONCAT arg")
  end
end

#exprObject



37
38
39
# File 'lib/arel_extensions/nodes/function.rb', line 37

def expr
  @expressions.first
end

#leftObject



41
42
43
# File 'lib/arel_extensions/nodes/function.rb', line 41

def left
  @expressions.first
end

#return_typeObject

overrides as to make new Node like AliasPredication



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

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

#rightObject



45
46
47
# File 'lib/arel_extensions/nodes/function.rb', line 45

def right
  @expressions[1]
end

#type_of_attribute(att) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/arel_extensions/nodes/function.rb', line 57

def type_of_attribute(att)
  case att
  when Arel::Attributes::Attribute
    Arel.column_of(att.relation.table_name, att.name.to_s)&.type || att
  when ArelExtensions::Nodes::Function
    att.return_type
    #        else
    #          nil
  end
end