Class: Money::Bank::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/money/bank/base.rb

Overview

This class is abstract.

Subclass and override #exchange_with to implement a custom Money::Bank class. You can also override #setup instead of #initialize to setup initial variables, etc.

Money::Bank::Base is the basic interface for creating a money exchange object, also called Bank.

A Bank is responsible for storing exchange rates, take a Money object as input and returns the corresponding Money object converted into an other currency.

This class exists for aiding in the creating of other classes to exchange money between different currencies. When creating a subclass you will need to implement the following methods to exchange money between currencies:

  • #exchange_with(Money) #=> Money

See Money::Bank::VariableExchange for a real example.

Also, you can extend Money::Bank::VariableExchange instead of Money::Bank::Base if your bank implementation needs to store rates internally.

Direct Known Subclasses

SingleCurrency, VariableExchange

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|n| ... } ⇒ Money::Bank::Base

Initializes a new Money::Bank::Base object. An optional block can be passed to dictate the rounding method that #exchange_with can use.

Examples:

Money::Bank::Base.new #=> #<Money::Bank::Base @rounding_method=nil>
Money::Bank::Base.new {|n|
  n.floor
} #=> #<Money::Bank::Base @round_method=#<Proc>>

Yields:

  • (n)

    Optional block to use when rounding after exchanging one currency for another.

Yield Parameters:

  • n (Float)

    The resulting float after exchanging one currency for another.

Yield Returns:

  • (Integer)


70
71
72
73
# File 'lib/money/bank/base.rb', line 70

def initialize(&block)
  @rounding_method = block
  setup
end

Instance Attribute Details

#rounding_methodProc (readonly)

The rounding method to use when exchanging rates.

Returns:

  • (Proc)


52
53
54
# File 'lib/money/bank/base.rb', line 52

def rounding_method
  @rounding_method
end

Class Method Details

.instanceMoney::Bank::Base

Returns the singleton instance of the Base bank.

Returns:



45
46
47
# File 'lib/money/bank/base.rb', line 45

def self.instance
  @singleton ||= self.new
end

Instance Method Details

#exchange_with(from, to_currency) {|n| ... } ⇒ Money

This method is abstract.

Subclass and override #exchange_with to implement a custom Money::Bank class.

Exchanges the given Money object to a new Money object in to_currency.

Parameters:

  • from (Money)

    The Money object to exchange from.

  • to_currency (Money::Currency, String, Symbol)

    The currency string or object to exchange to.

Yields:

  • (n)

    Optional block to use to round the result after making the exchange.

Yield Parameters:

  • n (Float)

    The result after exchanging from one currency to the other.

Yield Returns:

  • (Integer)

Returns:

Raises:

  • NotImplementedError



103
104
105
# File 'lib/money/bank/base.rb', line 103

def exchange_with(from, to_currency, &block)
  raise NotImplementedError, "#exchange_with must be implemented"
end

#same_currency?(currency1, currency2) ⇒ Boolean

Given two currency strings or object, checks whether they’re both the same currency. Return true if the currencies are the same, false otherwise.

Examples:

same_currency?("usd", "USD")                #=> true
same_currency?("usd", "EUR")                #=> false
same_currency?("usd", Currency.new("USD"))   #=> true
same_currency?("usd", "USD")                #=> true

Parameters:

  • currency1 (Money::Currency, String, Symbol)

    The first currency to compare.

  • currency2 (Money::Currency, String, Symbol)

    The second currency to compare.

Returns:

  • (Boolean)


123
124
125
# File 'lib/money/bank/base.rb', line 123

def same_currency?(currency1, currency2)
  Currency.wrap(currency1) == Currency.wrap(currency2)
end

#setupself

This method is abstract.

Subclass and override #setup to implement a custom Money::Bank class.

Called after initialize. Subclasses can use this method to setup variables, etc that they normally would in #initialize.

Returns:

  • (self)


82
83
# File 'lib/money/bank/base.rb', line 82

def setup
end