Class: Cash

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/cashrb/cash.rb

Overview

Provides methods for performing financial calculations without using floats.

Defined Under Namespace

Classes: IncompatibleCurrency

Constant Summary collapse

DEFAULT_CENTS_IN_WHOLE =
100
DEFAULT_ROUNDING_METHOD =
BigDecimal::ROUND_HALF_UP
DEFAULT_CURRENCY =
nil
DEFAULT_FROM =
:cents
VALID_FROMS =
[:cents, :decimal]
CURRENCY_AWARE_METHODS =
[:+, :-, :/, :%, :divmod, :<=>]
BD_ONE =
bd(1)
BD_TEN =
bd(10)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amt = 0, options = {}) ⇒ Cash

Returns a new instance of Cash.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cashrb/cash.rb', line 56

def initialize(amt = 0, options = {})
  opts = parse_initialize_options(options)
  @cents = bd(amt)

  if opts[:from] == :decimal
    dollars, cents = @cents.divmod(BD_ONE)
    @cents = (dollars * @cents_in_whole)
    @cents += (cents * (BD_TEN ** @decimal_places))
  end

  @cents = @cents.round(0, @rounding_method)
end

Class Attribute Details

.default_cents_in_wholeObject

Returns the value of attribute default_cents_in_whole.



35
36
37
# File 'lib/cashrb/cash.rb', line 35

def default_cents_in_whole
  @default_cents_in_whole
end

.default_currencyObject

Returns the value of attribute default_currency.



37
38
39
# File 'lib/cashrb/cash.rb', line 37

def default_currency
  @default_currency
end

.default_fromObject

Returns the value of attribute default_from.



38
39
40
# File 'lib/cashrb/cash.rb', line 38

def default_from
  @default_from
end

.default_rounding_methodObject

Returns the value of attribute default_rounding_method.



36
37
38
# File 'lib/cashrb/cash.rb', line 36

def default_rounding_method
  @default_rounding_method
end

Instance Attribute Details

#currencyObject (readonly)

Returns the value of attribute currency.



54
55
56
# File 'lib/cashrb/cash.rb', line 54

def currency
  @currency
end

Class Method Details

.bd(val) ⇒ Object



31
32
33
# File 'lib/cashrb/cash.rb', line 31

def bd(val)
  BigDecimal(val.to_s)
end

.reset_defaultsObject



20
21
22
23
24
25
# File 'lib/cashrb/cash.rb', line 20

def reset_defaults
  @default_cents_in_whole  = DEFAULT_CENTS_IN_WHOLE
  @default_rounding_method = DEFAULT_ROUNDING_METHOD
  @default_currency        = DEFAULT_CURRENCY
  @default_from            = DEFAULT_FROM
end

.valid_from?(from) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/cashrb/cash.rb', line 27

def valid_from?(from)
  VALID_FROMS.include? from
end

Instance Method Details

#%(value) ⇒ Object



95
96
97
98
99
# File 'lib/cashrb/cash.rb', line 95

def %(value)
  Cash.new(@cents % value.cents, new_options)
rescue NoMethodError
  Cash.new(@cents % bd(value), new_options)
end

#*(value) ⇒ Object



85
86
87
# File 'lib/cashrb/cash.rb', line 85

def *(value)
  Cash.new(@cents * bd(value), new_options)
end

#+(value) ⇒ Object



73
74
75
# File 'lib/cashrb/cash.rb', line 73

def +(value)
  Cash.new(@cents + value.cents, new_options)
end

#-(value) ⇒ Object



77
78
79
# File 'lib/cashrb/cash.rb', line 77

def -(value)
  Cash.new(@cents - value.cents, new_options)
end

#-@Object



81
82
83
# File 'lib/cashrb/cash.rb', line 81

def -@
  Cash.new(-@cents, new_options)
end

#/(value) ⇒ Object



89
90
91
92
93
# File 'lib/cashrb/cash.rb', line 89

def /(value)
  @cents / value.cents
rescue NoMethodError
  Cash.new(@cents / bd(value), new_options)
end

#<=>(value) ⇒ Object



113
114
115
# File 'lib/cashrb/cash.rb', line 113

def <=>(value)
  @cents <=> value.cents
end

#absObject



109
110
111
# File 'lib/cashrb/cash.rb', line 109

def abs
  Cash.new(@cents.abs, new_options)
end

#centsObject



69
70
71
# File 'lib/cashrb/cash.rb', line 69

def cents
  @cents.to_i
end

#divmod(value) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/cashrb/cash.rb', line 101

def divmod(value)
  quotient, remainder = @cents.divmod value.cents
  [quotient, Cash.new(remainder, new_options)]
rescue NoMethodError
  quotient, remainder = @cents.divmod bd(value)
  [Cash.new(quotient, new_options), Cash.new(remainder, new_options)]
end

#to_fObject



124
125
126
# File 'lib/cashrb/cash.rb', line 124

def to_f
  self.to_s.to_f
end

#to_sObject



117
118
119
120
121
122
# File 'lib/cashrb/cash.rb', line 117

def to_s
  return self.cents.to_s if @cents_in_whole == 1

  dollars, cents = dollars_and_cents
  "#{"-" if @cents < 0}#{dollars}.#{formatted_cents(cents)}"
end