Module: ArelExtensions::MathFunctions

Included in:
Attributes
Defined in:
lib/arel_extensions/math_functions.rb

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Object

Arel does not handle Decimal literal properly



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

def * other
  case other
  when Float, BigDecimal
    super(Arel::Nodes.build_quoted(other))
  else
    super(other)
  end
end

#/(other) ⇒ Object

Arel does not handle Decimal literal properly



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

def / other
  case other
  when Float, BigDecimal
    super(Arel::Nodes.build_quoted(other))
  else
    super(other)
  end
end

#absObject

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



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

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

#ceilObject

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



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

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

#floorObject

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



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

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

#format_number(format_string, locale = nil) ⇒ Object

function returning a number at a specific format



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/arel_extensions/math_functions.rb', line 94

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

#log10Object

function gives the base 10 log



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

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

#pow(exposant = 0) ⇒ Object

function gives the power of a number



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

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

#power(exposant = 0) ⇒ Object

function gives the power of a number



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

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



85
86
87
88
89
90
91
# File 'lib/arel_extensions/math_functions.rb', line 85

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



66
67
68
# File 'lib/arel_extensions/math_functions.rb', line 66

def std opts={unbiased: true}
  ArelExtensions::Nodes::Std.new self, opts
end

#sum(opts = {unbiased: true}) ⇒ Object



74
75
76
# File 'lib/arel_extensions/math_functions.rb', line 74

def sum opts={unbiased: true}
  ArelExtensions::Nodes::Sum.new self, opts
end

#variance(opts = {unbiased: true}) ⇒ Object



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

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