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
Moneyobject to a newMoneyobject into_currency. -
#initialize {|n| ... } ⇒ Money::Bank::Base
constructor
Initializes a new
Money::Bank::Baseobject. -
#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.
72 73 74 75 |
# File 'lib/money/bank/base.rb', line 72 def initialize(&block) @rounding_method = block setup end |
Instance Attribute Details
#rounding_method ⇒ Proc (readonly)
The rounding method to use when exchanging rates.
54 55 56 |
# File 'lib/money/bank/base.rb', line 54 def rounding_method @rounding_method end |
Class Method Details
.instance ⇒ Money::Bank::Base
Returns the singleton instance of the Base bank.
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
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.
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.
125 126 127 |
# File 'lib/money/bank/base.rb', line 125 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.
84 85 |
# File 'lib/money/bank/base.rb', line 84 def setup end |