Module: ArelExtensions::NullFunctions
- Defined in:
- lib/arel_extensions/null_functions.rb
Instance Method Summary collapse
-
#coalesce(*args) ⇒ Object
returns the first non-null expr in the expression list.
- #coalesce_blank(*args) ⇒ Object
-
#if_present ⇒ Object
if_present returns nil if the the value is nil or blank.
-
#is_not_null ⇒ Object
ISNOTNULL function lets you return an alternative value when an expression is NOT NULL.
-
#is_null ⇒ Object
ISNULL function lets you return an alternative value when an expression is NULL.
Instance Method Details
#coalesce(*args) ⇒ Object
returns the first non-null expr in the expression list. You must specify at least two expressions. If all occurrences of expr evaluate to null, then the function returns null.
24 25 26 27 |
# File 'lib/arel_extensions/null_functions.rb', line 24 def coalesce *args args.unshift(self) ArelExtensions::Nodes::Coalesce.new args end |
#coalesce_blank(*args) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/arel_extensions/null_functions.rb', line 29 def coalesce_blank *args res = Arel.when(self.cast(:string).present).then(self) args[0...-1].each do |a| val = a.is_a?(Arel::Nodes::Node) ? a : Arel.quoted(a) res = res.when(val.present).then(a) end res = res.else(args[-1]) res end |
#if_present ⇒ Object
if_present returns nil if the the value is nil or blank
8 9 10 |
# File 'lib/arel_extensions/null_functions.rb', line 8 def if_present Arel.when(self.cast(:string).present).then(self) end |
#is_not_null ⇒ Object
ISNOTNULL function lets you return an alternative value when an expression is NOT NULL.
18 19 20 |
# File 'lib/arel_extensions/null_functions.rb', line 18 def is_not_null ArelExtensions::Nodes::IsNotNull.new [self] end |
#is_null ⇒ Object
ISNULL function lets you return an alternative value when an expression is NULL.
13 14 15 |
# File 'lib/arel_extensions/null_functions.rb', line 13 def is_null ArelExtensions::Nodes::IsNull.new [self] end |