Class: Money::Bank::Base Abstract
- Inherits:
-
Object
- Object
- Money::Bank::Base
- Defined in:
- lib/money/bank/base.rb
Overview
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
Instance Attribute Summary collapse
-
#rounding_method ⇒ Proc
readonly
The rounding method to use when exchanging rates.
Class Method Summary collapse
-
.instance ⇒ Money::Bank::Base
Returns the singleton instance of the Base bank.
Instance Method Summary collapse
-
#exchange_with(from, to_currency) {|n| ... } ⇒ Money
abstract
Exchanges the given
Money
object to a newMoney
object into_currency
. -
#initialize {|n| ... } ⇒ Money::Bank::Base
constructor
Initializes a new
Money::Bank::Base
object. -
#same_currency?(currency1, currency2) ⇒ Boolean
Given two currency strings or object, checks whether they’re both the same currency.
-
#setup ⇒ self
abstract
Called after initialize.
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.
70 71 72 73 |
# File 'lib/money/bank/base.rb', line 70 def initialize(&block) @rounding_method = block setup end |
Instance Attribute Details
#rounding_method ⇒ Proc (readonly)
The rounding method to use when exchanging rates.
52 53 54 |
# File 'lib/money/bank/base.rb', line 52 def rounding_method @rounding_method end |
Class Method Details
.instance ⇒ Money::Bank::Base
Returns the singleton instance of the Base bank.
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
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
.
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.
123 124 125 |
# File 'lib/money/bank/base.rb', line 123 def same_currency?(currency1, currency2) Currency.wrap(currency1) == Currency.wrap(currency2) end |
#setup ⇒ self
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
.
82 83 |
# File 'lib/money/bank/base.rb', line 82 def setup end |