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

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)


72
73
74
75
# File 'lib/money/bank/base.rb', line 72

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

Instance Attribute Details

#rounding_methodProc (readonly)

The rounding method to use when exchanging rates.

Returns:

  • (Proc)


54
55
56
# File 'lib/money/bank/base.rb', line 54

def rounding_method
  @rounding_method
end

Class Method Details

.instanceMoney::Bank::Base

Returns the singleton instance of the Base bank.

Returns:



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

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



105
106
107
# File 'lib/money/bank/base.rb', line 105

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:

Returns:

  • (Boolean)


126
127
128
# File 'lib/money/bank/base.rb', line 126

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)


84
85
# File 'lib/money/bank/base.rb', line 84

def setup
end