Class: ForeignCurrencyExchange::Money

Inherits:
Object
  • Object
show all
Extended by:
Configuration, Forwardable
Includes:
Comparable
Defined in:
lib/foreign_currency_exchange/money.rb,
lib/foreign_currency_exchange/money/exceptions.rb,
lib/foreign_currency_exchange/money/configuration.rb

Overview

This class represents a money object.

Defined Under Namespace

Modules: Configuration

Constant Summary collapse

AMOUNT_FORMAT =
'%.2f'.freeze
ROUND_LEVEL =
2
UnknownCurrencyError =
Class.new(StandardError)
InvalidRatesError =
Class.new(StandardError)
InvalidBaseCurrencyRateError =
Class.new(StandardError)

Constants included from Configuration

Configuration::BASE_CURRENCY_RATE

Instance Attribute Summary collapse

Attributes included from Configuration

#base_currency, #rates

Instance Method Summary collapse

Methods included from Configuration

check_configuration, conversion_rates

Constructor Details

#initialize(amount, currency) ⇒ Money

Returns a new instance of Money.



17
18
19
20
21
22
# File 'lib/foreign_currency_exchange/money.rb', line 17

def initialize(amount, currency)
  check_configuration # Configuration should be valid.
  check_currency(currency) # Currency should be configured before.
  @amount = prepare_amount(amount)
  @currency = currency
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



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

def amount
  @amount
end

#currencyObject (readonly)

Returns the value of attribute currency.



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

def currency
  @currency
end

Instance Method Details

#*(multiplier) ⇒ Object

Returns new money object with multiplied current object amount,

parameter multiplier should be kind of Numeric object.


47
48
49
# File 'lib/foreign_currency_exchange/money.rb', line 47

def *(multiplier)
  new(amount * multiplier, currency)
end

#+(other) ⇒ Object



35
36
37
38
# File 'lib/foreign_currency_exchange/money.rb', line 35

def +(other)
  new_amount = amount + other.convert_to(currency).amount
  new(new_amount, currency)
end

#-(other) ⇒ Object



40
41
42
43
# File 'lib/foreign_currency_exchange/money.rb', line 40

def -(other)
  new_amount = amount - other.convert_to(currency).amount
  new(new_amount, currency)
end

#/(divider) ⇒ Object

Returns new money object with divided current object amount,

parameter divider should be kind of Numeric object.

Raises:

  • (ZeroDivisionError)


53
54
55
56
# File 'lib/foreign_currency_exchange/money.rb', line 53

def /(divider)
  raise ZeroDivisionError if divider.zero?
  new(amount / divider.to_f, currency)
end

#<=>(other) ⇒ Object



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

def <=>(other)
  base_amount <=> other.base_amount
end

#base_amountObject

Returns current object amount if currency equals to base_currency

or converted current object amount to base_currency otherwise.


26
27
28
29
# File 'lib/foreign_currency_exchange/money.rb', line 26

def base_amount
  return amount if currency == base_currency
  prepare_amount(amount / rates[currency].to_f)
end

#convert_to(other_currency) ⇒ Object

Returns new money object converted to given currency,

raises UnknownCurrencyError in case when given currency is unknown.


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

def convert_to(other_currency)
  new_amount = calculate_amount(other_currency)
  new(new_amount, other_currency)
end

#inspectObject



62
63
64
# File 'lib/foreign_currency_exchange/money.rb', line 62

def inspect
  to_s
end

#to_sObject



58
59
60
# File 'lib/foreign_currency_exchange/money.rb', line 58

def to_s
  "#{formatted_amount} #{currency}"
end