Class: Cash

Inherits:
Object
  • Object
show all
Includes:
Comparable, Equalable
Defined in:
lib/cash.rb,
lib/cash/currency.rb,
lib/cash/currency/iso4217.rb

Defined Under Namespace

Modules: Format Classes: Currency

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Equalable

#==, #hash

Constructor Details

#initialize(amount, currency) ⇒ Cash

Returns a new instance of Cash.



12
13
14
# File 'lib/cash.rb', line 12

def initialize(amount, currency)
  @amount, @currency = StrictDecimal(amount), Currency.find!(currency)
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



16
17
18
# File 'lib/cash.rb', line 16

def amount
  @amount
end

#currencyObject (readonly)

Returns the value of attribute currency.



16
17
18
# File 'lib/cash.rb', line 16

def currency
  @currency
end

Instance Method Details

#*(factor) ⇒ Object



78
79
80
81
82
83
# File 'lib/cash.rb', line 78

def *(factor)
  factor = StrictDecimal(factor)
  Cash.new(amount * factor, currency)
rescue ArgumentError
  raise ArgumentError, "cannot multiply #{self.inspect} by #{factor}"
end

#+(o) ⇒ Object



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

def +(o)
  check_type(o, :add)
  Cash.new(amount + o.amount, currency)
end

#-(o) ⇒ Object



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

def -(o)
  check_type(o, :subtract)
  Cash.new(amount - o.amount, currency)
end

#/(factor) ⇒ Object



85
86
87
88
89
90
# File 'lib/cash.rb', line 85

def /(factor)
  factor = StrictDecimal(factor)
  Cash.new(amount / factor, currency)
rescue ArgumentError
  raise ArgumentError, "cannot divide #{self.inspect} by #{factor}"
end

#<=>(o) ⇒ Object



63
64
65
66
# File 'lib/cash.rb', line 63

def <=>(o)
  check_type(o, :compare)
  amount <=> o.amount
end

#amount_stringObject



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

def amount_string
 "%.#{currency.offset}f" % amount.round(currency.offset)
end

#inspectObject



44
45
46
# File 'lib/cash.rb', line 44

def inspect
  "<Cash #{to_s}>"
end

#pretty_printObject



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

def pretty_print
  Format.display(self)
end

#roundObject



59
60
61
# File 'lib/cash.rb', line 59

def round
  Cash.new(amount.round, currency)
end

#StrictDecimal(arg) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cash.rb', line 18

def StrictDecimal(arg)
  case arg
  when BigDecimal
    arg
  when Float
    fail ArgumentError, "innacurate float for StrictDecimal(): #{arg.inspect}"
  when Integer
    BigDecimal(arg.to_s)
  when String
    Float(arg)
    BigDecimal(arg)
  else
    fail TypeError
  end
rescue TypeError
  fail ArgumentError, "invalid value for StrictDecimal(): #{arg.inspect}"
end

#to_hObject



48
49
50
51
52
53
# File 'lib/cash.rb', line 48

def to_h
  {
    :amount => amount_string,
    :currency => currency.code
  }
end

#to_sObject



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

def to_s
  "#{amount_string} #{currency.code}"
end