1+ class Money
2+ # Provides classes that aid in the ability of exchange one currency with
3+ # another.
4+ module Bank
5+ # The lowest Money::Bank error class.
6+ # All Money::Bank errors should inherit from it.
7+ class Error < StandardError
8+ end
9+
10+ # Raised when the bank doesn't know about the conversion rate
11+ # for specified currencies.
12+ class UnknownRate < Error
13+ end
14+
15+ # Money::Bank::Base is the basic interface for creating a money exchange
16+ # object, also called Bank.
17+ #
18+ # A Bank is responsible for storing exchange rates, take a Money object as
19+ # input and returns the corresponding Money object converted into an other
20+ # currency.
21+ #
22+ # This class exists for aiding in the creating of other classes to exchange
23+ # money between different currencies. When creating a subclass you will
24+ # need to implement the following methods to exchange money between
25+ # currencies:
26+ #
27+ # - #exchange_with(Money) #=> Money
28+ #
29+ # See Money::Bank::VariableExchange for a real example.
30+ #
31+ # Also, you can extend +Money::Bank::VariableExchange+ instead of
32+ # +Money::Bank::Base+ if your bank implementation needs to store rates
33+ # internally.
34+ #
35+ # @abstract Subclass and override +#exchange_with+ to implement a custom
36+ # +Money::Bank+ class. You can also override +#setup+ instead of
37+ # +#initialize+ to setup initial variables, etc.
38+ class Base
39+ @singleton : Money::Bank::Base
40+
41+ # Returns the singleton instance of the Base bank.
42+ #
43+ # @return [Money::Bank::Base]
44+ def self.instance : () -> Money::Bank::Base
45+
46+ # The rounding method to use when exchanging rates.
47+ #
48+ # @return [Proc]
49+ attr_reader rounding_method: untyped
50+
51+ # Initializes a new +Money::Bank::Base+ object. An optional block can be
52+ # passed to dictate the rounding method that +#exchange_with+ can use.
53+ #
54+ # @yield [n] Optional block to use when rounding after exchanging one
55+ # currency for another.
56+ # @yieldparam [Float] n The resulting float after exchanging one currency
57+ # for another.
58+ # @yieldreturn [Integer]
59+ #
60+ # @return [Money::Bank::Base]
61+ #
62+ # @example
63+ # Money::Bank::Base.new #=> #<Money::Bank::Base @rounding_method=nil>
64+ # Money::Bank::Base.new {|n|
65+ # n.floor
66+ # } #=> #<Money::Bank::Base @round_method=#<Proc>>
67+ def initialize : () { () -> untyped } -> void
68+
69+ # Called after initialize. Subclasses can use this method to setup
70+ # variables, etc that they normally would in +#initialize+.
71+ #
72+ # @abstract Subclass and override +#setup+ to implement a custom
73+ # +Money::Bank+ class.
74+ #
75+ # @return [self]
76+ def setup : () -> nil
77+
78+ # Exchanges the given +Money+ object to a new +Money+ object in
79+ # +to_currency+.
80+ #
81+ # @abstract Subclass and override +#exchange_with+ to implement a custom
82+ # +Money::Bank+ class.
83+ #
84+ # @raise NotImplementedError
85+ #
86+ # @param [Money] from The +Money+ object to exchange from.
87+ # @param [Money::Currency, String, Symbol] to_currency The currency
88+ # string or object to exchange to.
89+ # @yield [n] Optional block to use to round the result after making
90+ # the exchange.
91+ # @yieldparam [Float] n The result after exchanging from one currency to
92+ # the other.
93+ # @yieldreturn [Integer]
94+ #
95+ # @return [Money]
96+ def exchange_with : (Money from, (Money::Currency | string | Symbol) to_currency) { () -> int } -> Money
97+
98+ # Given two currency strings or object, checks whether they're both the
99+ # same currency. Return +true+ if the currencies are the same, +false+
100+ # otherwise.
101+ #
102+ # @param [Money::Currency, String, Symbol] currency1 The first currency
103+ # to compare.
104+ # @param [Money::Currency, String, Symbol] currency2 The second currency
105+ # to compare.
106+ #
107+ # @return [Boolean]
108+ #
109+ # @example
110+ # same_currency?("usd", "USD") #=> true
111+ # same_currency?("usd", "EUR") #=> false
112+ # same_currency?("usd", Currency.new("USD")) #=> true
113+ # same_currency?("usd", "USD") #=> true
114+ def same_currency? : ((Money::Currency | string | Symbol) currency1, (Money::Currency | string | Symbol) currency2) -> bool
115+ end
116+ end
117+ end
0 commit comments