Module: ArelExtensions::MathFunctions

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Object

Arel does not handle Decimal literal properly



15
16
17
18
19
20
21
22
# File 'lib/arel_extensions/math_functions.rb', line 15

def * other
  case other
  when Float, BigDecimal
    super(Arel.quoted(other))
  else
    super(other)
  end
end

#/(other) ⇒ Object

Arel does not handle Decimal literal properly



25
26
27
28
29
30
31
32
# File 'lib/arel_extensions/math_functions.rb', line 25

def / other
  case other
  when Float, BigDecimal
    super(Arel.quoted(other))
  else
    super(other)
  end
end

#absObject

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



35
36
37
# File 'lib/arel_extensions/math_functions.rb', line 35

def abs
  ArelExtensions::Nodes::Abs.new [self]
end

#ceilObject

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



40
41
42
# File 'lib/arel_extensions/math_functions.rb', line 40

def ceil
  ArelExtensions::Nodes::Ceil.new [self]
end

#floorObject

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



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

def floor
  ArelExtensions::Nodes::Floor.new [self]
end

#format_number(format_string, locale = nil) ⇒ Object

function returning a number at a specific format



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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.quoted('Wrong Format')
  end
end

#log10Object

function gives the base 10 log



50
51
52
# File 'lib/arel_extensions/math_functions.rb', line 50

def log10
  ArelExtensions::Nodes::Log10.new [self]
end

#pow(exposant = 0) ⇒ Object

function gives the power of a number



55
56
57
# File 'lib/arel_extensions/math_functions.rb', line 55

def pow exposant = 0
  ArelExtensions::Nodes::Power.new [self, exposant]
end

#power(exposant = 0) ⇒ Object

function gives the power of a number



60
61
62
# File 'lib/arel_extensions/math_functions.rb', line 60

def power exposant = 0
  ArelExtensions::Nodes::Power.new [self, exposant]
end

#round(precision = nil) ⇒ Object

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



88
89
90
91
92
93
94
# 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}) ⇒ Object

Aggregate Functions



65
66
67
# 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}) ⇒ Object



73
74
75
76
77
78
79
# 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}) ⇒ Object



69
70
71
# File 'lib/arel_extensions/math_functions.rb', line 69

def variance opts = {unbiased: true}
  ArelExtensions::Nodes::Variance.new self, **opts
end