Class: Bankroll::Decimal

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/bankroll/decimal.rb

Constant Summary collapse

ROUNDING =
:half_even

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Decimal

Returns a new instance of Decimal.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bankroll/decimal.rb', line 21

def initialize(value)
  decimal = case value
              when self.class
                value.value.to_s
              else
                value.to_s
            end

  @value = BigDecimal(Types["string"][decimal])
  super(@value)
end

Instance Attribute Details

#valueObject (readonly) Also known as: __getobj__

Returns the value of attribute value.



17
18
19
# File 'lib/bankroll/decimal.rb', line 17

def value
  @value
end

Class Method Details

.[](value) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/bankroll/decimal.rb', line 9

def self.[](value)
  if value.is_a? self
    value
  else
    new(value.to_s)
  end
end

Instance Method Details

#*(other) ⇒ Object



45
46
47
# File 'lib/bankroll/decimal.rb', line 45

def *(other)
  wrap super(Decimal[other].value)
end

#**(other) ⇒ Object



53
54
55
# File 'lib/bankroll/decimal.rb', line 53

def **(other)
  wrap super(Decimal[other].value)
end

#+(other) ⇒ Object



37
38
39
# File 'lib/bankroll/decimal.rb', line 37

def +(other)
  wrap super(Decimal[other].value)
end

#-(other) ⇒ Object



41
42
43
# File 'lib/bankroll/decimal.rb', line 41

def -(other)
  wrap super(Decimal[other].value)
end

#/(other) ⇒ Object



49
50
51
# File 'lib/bankroll/decimal.rb', line 49

def /(other)
  wrap super(Decimal[other].value)
end

#round(precision = 2, rounding = ROUNDING) ⇒ Object



33
34
35
# File 'lib/bankroll/decimal.rb', line 33

def round(precision = 2, rounding = ROUNDING)
  wrap @value.round(precision, rounding)
end