Class: Money

Inherits:
Struct
  • Object
show all
Defined in:
lib/money/base.rb,
lib/money/money.rb,
lib/money/version.rb,
lib/money/currency.rb,
lib/money/attributes.rb

Defined Under Namespace

Modules: Attributes Classes: Currency

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, currency, date = nil) ⇒ Money

Returns a new instance of Money.



5
6
7
# File 'lib/money/money.rb', line 5

def initialize(amount, currency, date=nil)
  super amount, Currency[currency], date
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount

Returns:

  • (Object)

    the current value of amount



1
2
3
# File 'lib/money/base.rb', line 1

def amount
  @amount
end

#currencyObject

Returns the value of attribute currency

Returns:

  • (Object)

    the current value of currency



1
2
3
# File 'lib/money/base.rb', line 1

def currency
  @currency
end

#dateObject

Returns the value of attribute date

Returns:

  • (Object)

    the current value of date



1
2
3
# File 'lib/money/base.rb', line 1

def date
  @date
end

Instance Method Details

#*(other) ⇒ Object



25
26
27
# File 'lib/money/money.rb', line 25

def *(other)
  calculate(:*, other)
end

#+(other) ⇒ Object



13
14
15
# File 'lib/money/money.rb', line 13

def +(other)
  calculate(:+, other)
end

#-(other) ⇒ Object



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

def -(other)
  calculate(:-, other)
end

#/(other) ⇒ Object



21
22
23
# File 'lib/money/money.rb', line 21

def /(other)
  calculate(:/, other)
end

#coerce(other) ⇒ Object



30
31
32
# File 'lib/money/money.rb', line 30

def coerce(other)
  [Money.new(other, currency, date), amount]
end

#exchange_to(other_currency, on: date) ⇒ Object



9
10
11
# File 'lib/money/money.rb', line 9

def exchange_to(other_currency, on: date)
  currency.to(Currency[other_currency], on: on) * amount
end

#inspectObject



42
43
44
# File 'lib/money/money.rb', line 42

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

#to_sObject



34
35
36
37
38
39
40
# File 'lib/money/money.rb', line 34

def to_s
  if date.nil?
    "#{amount} #{currency}"
  else
    "#{amount} #{currency} on #{date}"
  end
end