Class: Money::Bank::VariableExchange
- Defined in:
- lib/money/bank/variable_exchange.rb
Overview
Class for aiding in exchanging money between different currencies. By default, the Money
class uses an object of this class (accessible through Money#bank) for performing currency exchanges.
By default, Money::Bank::VariableExchange
has no knowledge about conversion rates. One must manually specify them with add_rate
, after which one can perform exchanges with #exchange_with
.
Constant Summary collapse
- RATE_FORMATS =
Available formats for importing/exporting rates.
[:json, :ruby, :yaml]
Instance Attribute Summary collapse
-
#rates ⇒ Object
readonly
Returns the value of attribute rates.
Attributes inherited from Base
Instance Method Summary collapse
-
#add_rate(from, to, rate) ⇒ Numeric
Registers a conversion rate and returns it (uses
#set_rate
). -
#exchange_with(from, to_currency) {|n| ... } ⇒ Money
Exchanges the given
Money
object to a newMoney
object into_currency
. -
#export_rates(format, file = nil) ⇒ String
Return the known rates as a string in the format specified.
-
#get_rate(from, to) ⇒ Numeric
Retrieve the rate for the given currencies.
-
#import_rates(format, s) ⇒ self
Loads rates provided in
s
given the specified format. - #marshal_dump ⇒ Object
- #marshal_load(arr) ⇒ Object
-
#set_rate(from, to, rate) ⇒ Numeric
Set the rate for the given currencies.
-
#setup ⇒ self
Setup rates hash and mutex for rates locking.
Methods inherited from Base
#initialize, instance, #same_currency?
Constructor Details
This class inherits a constructor from Money::Bank::Base
Instance Attribute Details
#rates ⇒ Object (readonly)
Returns the value of attribute rates.
33 34 35 |
# File 'lib/money/bank/variable_exchange.rb', line 33 def rates @rates end |
Instance Method Details
#add_rate(from, to, rate) ⇒ Numeric
Registers a conversion rate and returns it (uses #set_rate
).
122 123 124 |
# File 'lib/money/bank/variable_exchange.rb', line 122 def add_rate(from, to, rate) set_rate(from, to, rate) end |
#exchange_with(from, to_currency) {|n| ... } ⇒ Money
Exchanges the given Money
object to a new Money
object in to_currency
.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/money/bank/variable_exchange.rb', line 87 def exchange_with(from, to_currency) return from if same_currency?(from.currency, to_currency) rate = get_rate(from.currency, to_currency) unless rate raise UnknownRate, "No conversion rate known for '#{from.currency.iso_code}' -> '#{to_currency}'" end _to_currency_ = Currency.wrap(to_currency) cents = BigDecimal.new(from.cents.to_s) / (BigDecimal.new(from.currency.subunit_to_unit.to_s) / BigDecimal.new(_to_currency_.subunit_to_unit.to_s)) ex = cents * BigDecimal.new(rate.to_s) ex = ex.to_f ex = if block_given? yield ex elsif @rounding_method @rounding_method.call(ex) else ex.to_s.to_i end Money.new(ex, _to_currency_) end |
#export_rates(format, file = nil) ⇒ String
Return the known rates as a string in the format specified. If file
is given will also write the string out to the file specified. Available formats are :json
, :ruby
and :yaml
.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/money/bank/variable_exchange.rb', line 180 def export_rates(format, file=nil) raise Money::Bank::UnknownRateFormat unless RATE_FORMATS.include? format s = "" @mutex.synchronize { s = case format when :json JSON.dump(@rates) when :ruby Marshal.dump(@rates) when :yaml YAML.dump(@rates) end unless file.nil? File.open(file, "w").write(s) end } s end |
#get_rate(from, to) ⇒ Numeric
Retrieve the rate for the given currencies. Uses Mutex
to synchronize data access.
158 159 160 |
# File 'lib/money/bank/variable_exchange.rb', line 158 def get_rate(from, to) @mutex.synchronize { @rates[rate_key_for(from, to)] } end |
#import_rates(format, s) ⇒ self
Loads rates provided in s
given the specified format. Available formats are :json
, :ruby
and :yaml
.
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/money/bank/variable_exchange.rb', line 219 def import_rates(format, s) raise Money::Bank::UnknownRateFormat unless RATE_FORMATS.include? format @mutex.synchronize { @rates = case format when :json JSON.load(s) when :ruby Marshal.load(s) when :yaml YAML.load(s) end } self end |
#marshal_dump ⇒ Object
47 48 49 |
# File 'lib/money/bank/variable_exchange.rb', line 47 def marshal_dump [@rates, @rounding_method] end |
#marshal_load(arr) ⇒ Object
51 52 53 54 |
# File 'lib/money/bank/variable_exchange.rb', line 51 def marshal_load(arr) @rates, @rounding_method = arr @mutex = Mutex.new end |
#set_rate(from, to, rate) ⇒ Numeric
Set the rate for the given currencies. Uses Mutex
to synchronize data access.
139 140 141 |
# File 'lib/money/bank/variable_exchange.rb', line 139 def set_rate(from, to, rate) @mutex.synchronize { @rates[rate_key_for(from, to)] = rate } end |
#setup ⇒ self
Setup rates hash and mutex for rates locking
41 42 43 44 45 |
# File 'lib/money/bank/variable_exchange.rb', line 41 def setup @rates = {} @mutex = Mutex.new self end |