Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

document Money::BaseBank

  • Loading branch information...
commit 8f106b8a9bfc1d1cae0215080295dabba9b3e86d 1 parent e11cb73
Shane Emmons authored July 16, 2010

Showing 1 changed file with 23 additions and 0 deletions. Show diff stats Hide diff stats

  1. 23  lib/money/base_bank.rb
23  lib/money/base_bank.rb
... ...
@@ -1,9 +1,18 @@
1 1
 require 'thread'
2 2
 
  3
+# Class for aiding in the creating of other classes to exchange money between
  4
+# different currencies.
  5
+#
  6
+# When creating a subclass you will need to define methods to populate the
  7
+# +@rates+ hash using +#set_rate+ and +#get_rate+, or override the
  8
+# +#exchange_with+ method.
  9
+#
  10
+# See Money::VariableExchangeBank for an example.
3 11
 class Money
4 12
   class BaseBank
5 13
     class UnknownRate < StandardError; end
6 14
 
  15
+    # Returns the singleton instance of BaseBank.
7 16
     def self.instance
8 17
       @@singleton
9 18
     end
@@ -16,11 +25,21 @@ def initialize(&block)
16 25
       @rounding_method = block
17 26
     end
18 27
 
  28
+    # @depreciated +#exchange+ will be removed in v3.2.0, use +#exchange_with+
  29
+    #
  30
+    # Exchange the given amount of cents in +from_currency+ to +to_currency+.
  31
+    # Returns the amount of cents in +to_currency+ as an integer, rounded down.
  32
+    #
  33
+    # If the conversion rate is unknown, +UnknownRate+ will be raised.
19 34
     def exchange(cents, from_currency, to_currency, &block)
20 35
       warn '[DEPRECIATION] `exchange` will be removed in v3.2.0, use #exchange_with instead'
21 36
       exchange_with(Money.new(cents, from_currency), to_currency, &block).cents
22 37
     end
23 38
 
  39
+    # Exchange the given +Money+ object to a new +Money+ object in
  40
+    # +to_currency+. Returns a new +Money+ object.
  41
+    #
  42
+    # If the conversion rate is unknown, +UknownRate+ will be raised.
24 43
     def exchange_with(from, to_currency, &block)
25 44
       return from if same_currency?(from.currency, to_currency)
26 45
 
@@ -45,18 +64,22 @@ def exchange_with(from, to_currency, &block)
45 64
 
46 65
     private
47 66
 
  67
+    # Return the rate hashkey for the given currencies.
48 68
     def rate_key_for(from, to)
49 69
       "#{Currency.wrap(from).iso_code}_TO_#{Currency.wrap(to).iso_code}".upcase
50 70
     end
51 71
 
  72
+    # Set the rate for the given currencies.
52 73
     def set_rate(from, to, rate)
53 74
       @mutex.synchronize{ @rates[rate_key_for(from, to)] = rate }
54 75
     end
55 76
 
  77
+    # Retrieve the rate for the given currencies.
56 78
     def get_rate(from, to)
57 79
       @mutex.synchronize{ @rates[rate_key_for(from, to)] }
58 80
     end
59 81
 
  82
+    # Return +true+ if the currencies are the same, +false+ otherwise.
60 83
     def same_currency?(currency1, currency2)
61 84
       Currency.wrap(currency1) == Currency.wrap(currency2)
62 85
     end

0 notes on commit 8f106b8

Please sign in to comment.
Something went wrong with that request. Please try again.