module ArelExtensions::MathFunctions

Public Instance Methods

*(other) click to toggle source

Arel does not handle Decimal literal properly

Calls superclass method
# File lib/arel_extensions/math_functions.rb, line 15
def * other
  case other
  when Float, BigDecimal
    super(Arel::Nodes.build_quoted(other))
  else
    super(other)
  end
end
/(other) click to toggle source

Arel does not handle Decimal literal properly

Calls superclass method
# File lib/arel_extensions/math_functions.rb, line 25
def / other
  case other
  when Float, BigDecimal
    super(Arel::Nodes.build_quoted(other))
  else
    super(other)
  end
end
abs() click to toggle source

Abs function returns the absolute value of a number passed as argument #

# File lib/arel_extensions/math_functions.rb, line 35
def abs
    ArelExtensions::Nodes::Abs.new [self]
end
ceil() click to toggle source

will rounded up any positive or negative decimal value within the function upwards #

# File lib/arel_extensions/math_functions.rb, line 40
def ceil
    ArelExtensions::Nodes::Ceil.new [self]
end
floor() click to toggle source

function rounded up any positive or negative decimal value down to the next least integer

# File lib/arel_extensions/math_functions.rb, line 45
def floor
    ArelExtensions::Nodes::Floor.new [self]
end
format_number(format_string, locale = nil) click to toggle source

function returning a number at a specific format

# File lib/arel_extensions/math_functions.rb, line 97
def format_number format_string, locale = nil
  begin
    sprintf(format_string,0) # this line is to get the right error message if the format_string is not correct
    m = /^(.*)%([ #+\-0]*)([1-9][0-9]+|[1-9]?)[.]?([0-9]*)([a-zA-Z])(.*)$/.match(format_string)
    opts = {
      prefix: m[1],
      flags: m[2].split(//).uniq.join,
      width: m[3].to_i,
      precision: m[4] != '' ? m[4].to_i : 6,
      type: m[5],
      suffix: m[6],
      locale: locale,
      original_string: format_string
    }
    # opts = {:locale => 'fr_FR', :type => "e"/"f"/"d", :prefix => "$ ", :suffix => " %", :flags => " +-#0", :width => 5, :precision => 6}
    ArelExtensions::Nodes::FormattedNumber.new [self,opts]
  rescue Exception
    Arel::Nodes.build_quoted('Wrong Format')
  end
end
log10() click to toggle source

function gives the base 10 log

# File lib/arel_extensions/math_functions.rb, line 50
def log10
    ArelExtensions::Nodes::Log10.new [self]
end
pow(exposant = 0) click to toggle source

function gives the power of a number

# File lib/arel_extensions/math_functions.rb, line 55
def pow exposant = 0
    ArelExtensions::Nodes::Power.new [self,exposant]
end
power(exposant = 0) click to toggle source

function gives the power of a number

# File lib/arel_extensions/math_functions.rb, line 60
def power exposant = 0
    ArelExtensions::Nodes::Power.new [self,exposant]
end
round(precision = nil) click to toggle source

function is used to round a numeric field to the number of decimals specified

# File lib/arel_extensions/math_functions.rb, line 88
def round precision = nil
    if precision
        ArelExtensions::Nodes::Round.new [self, precision]
    else
        ArelExtensions::Nodes::Round.new [self]
    end
end
std(opts = {unbiased: true}) click to toggle source

Aggregate Functions

# File lib/arel_extensions/math_functions.rb, line 65
def std opts = {unbiased: true}
  ArelExtensions::Nodes::Std.new self, **opts
end
sum(opts = {unbiased: true}) click to toggle source
# File lib/arel_extensions/math_functions.rb, line 73
def sum opts = {unbiased: true}
  if Gem::Version.new(Arel::VERSION) >= Gem::Version.new("9.0.0")
    Arel::Nodes::Sum.new [self]
  else
    ArelExtensions::Nodes::Sum.new self, **opts
  end
end
variance(opts = {unbiased: true}) click to toggle source
# File lib/arel_extensions/math_functions.rb, line 69
def variance opts = {unbiased: true}
  ArelExtensions::Nodes::Variance.new self, **opts
end