@@ -161,7 +161,8 @@ class << self
161161 :default_infinite_precision ,
162162 :conversion_precision ,
163163 :strict_eql_compare
164- attr_reader :locale_backend
164+ attr_reader :locale_backend ,
165+ :currency_helpers
165166 attr_writer :default_bank ,
166167 :default_currency
167168
@@ -219,6 +220,21 @@ def self.locale_backend=(value)
219220 @locale_backend = value ? LocaleBackend . find ( value ) : nil
220221 end
221222
223+ def self . currency_helpers = ( hash )
224+ hash . each do |name , currency |
225+ next if singleton_class . method_defined? ( name )
226+
227+ singleton_class . define_method ( name ) do |cents |
228+ new ( cents , currency )
229+ end
230+
231+ define_method ( "as_#{ name } " ) do
232+ exchange_to ( currency )
233+ end
234+ end
235+ @currency_helpers = hash
236+ end
237+
222238 def self . setup_defaults
223239 # Set the default bank for creating new +Money+ objects.
224240 self . default_bank = Bank ::VariableExchange . instance
@@ -235,9 +251,21 @@ def self.setup_defaults
235251 # Default the conversion of Rationals precision to 16
236252 self . conversion_precision = 16
237253
238- # Defaults to the deprecated behavior where
254+ # Default to the deprecated behavior where
239255 # `Money.new(0, "USD").eql?(Money.new(0, "EUR"))` is true.
240256 self . strict_eql_compare = false
257+
258+ # Default to currencies previously hardcoded as methods
259+ self . currency_helpers = {
260+ cad : "CAD" ,
261+ ca_dollar : "CAD" ,
262+ eur : "EUR" ,
263+ euro : "EUR" ,
264+ gbp : "GBP" ,
265+ pound_sterling : "GBP" ,
266+ usd : "USD" ,
267+ us_dollar : "USD" ,
268+ }
241269 end
242270
243271 def self . inherited ( base )
@@ -298,41 +326,6 @@ def self.disallow_currency_conversion!
298326 self . default_bank = Bank ::SingleCurrency . instance
299327 end
300328
301- # Creates a new Money object of value given in the +unit+ of the given
302- # +currency+.
303- #
304- # @param [Numeric] amount The numerical value of the money.
305- # @param [Currency, String, Symbol] currency The currency format.
306- # @param [Hash] options Optional settings for the new Money instance
307- # @option [Money::Bank::*] :bank The exchange bank to use.
308- #
309- # @example
310- # Money.from_amount(23.45, "USD") # => #<Money fractional:2345 currency:USD>
311- # Money.from_amount(23.45, "JPY") # => #<Money fractional:23 currency:JPY>
312- #
313- # @return [Money]
314- #
315- # @see #initialize
316- def self . from_amount ( amount , currency = default_currency , options = { } )
317- raise ArgumentError , "'amount' must be numeric" unless amount . is_a? ( Numeric )
318-
319- currency = Currency . wrap ( currency ) || Money . default_currency
320- raise Currency ::NoCurrency , "must provide a currency" if currency . nil?
321-
322- value = amount . to_d * currency . subunit_to_unit
323- new ( value , currency , options )
324- end
325-
326- # DEPRECATED.
327- #
328- # @see Money.from_amount
329- def self . from_dollars ( amount , currency = default_currency , options = { } )
330- warn "[DEPRECATION] `Money.from_dollars` is deprecated in favor of " \
331- "`Money.from_amount`."
332-
333- from_amount ( amount , currency , options )
334- end
335-
336329 # Creates a new Money object of value given in the
337330 # +fractional unit+ of the given +currency+.
338331 #
@@ -427,7 +420,7 @@ def inspect
427420 # @return [String]
428421 #
429422 # @example
430- # Money.ca_dollar (100).to_s #=> "1.00"
423+ # Money.new (100, "CAD" ).to_s #=> "1.00"
431424 def to_s
432425 format thousands_separator : "" ,
433426 no_cents_if_whole : currency . decimal_places == 0 ,
@@ -440,7 +433,7 @@ def to_s
440433 # @return [BigDecimal]
441434 #
442435 # @example
443- # Money.us_dollar (1_00).to_d #=> BigDecimal("1.00")
436+ # Money.new (1_00, "USD" ).to_d #=> BigDecimal("1.00")
444437 def to_d
445438 as_d ( fractional ) / as_d ( currency . subunit_to_unit )
446439 end
@@ -450,20 +443,20 @@ def to_d
450443 # @return [Integer]
451444 #
452445 # @example
453- # Money.us_dollar (1_00).to_i #=> 1
446+ # Money.new (1_00, "USD" ).to_i #=> 1
454447 def to_i
455448 to_d . to_i
456449 end
457450
458- # Return the amount of money as a float. Floating points cannot guarantee
451+ # Returns the amount of money as a float. Floating points cannot guarantee
459452 # precision. Therefore, this function should only be used when you no longer
460453 # need to represent currency or working with another system that requires
461454 # floats.
462455 #
463456 # @return [Float]
464457 #
465458 # @example
466- # Money.us_dollar (100).to_f #=> 1.0
459+ # Money.new (100, "USD" ).to_f #=> 1.0
467460 def to_f
468461 to_d . to_f
469462 end
@@ -520,42 +513,6 @@ def exchange_to(other_currency, &)
520513 end
521514 end
522515
523- # Receive a money object with the same amount as the current Money object
524- # in United States dollar.
525- #
526- # @return [Money]
527- #
528- # @example
529- # n = Money.new(100, "CAD").as_us_dollar
530- # n.currency #=> #<Money::Currency id: usd>
531- def as_us_dollar
532- exchange_to ( "USD" )
533- end
534-
535- # Receive a money object with the same amount as the current Money object
536- # in Canadian dollar.
537- #
538- # @return [Money]
539- #
540- # @example
541- # n = Money.new(100, "USD").as_ca_dollar
542- # n.currency #=> #<Money::Currency id: cad>
543- def as_ca_dollar
544- exchange_to ( "CAD" )
545- end
546-
547- # Receive a money object with the same amount as the current Money object
548- # in euro.
549- #
550- # @return [Money]
551- #
552- # @example
553- # n = Money.new(100, "USD").as_euro
554- # n.currency #=> #<Money::Currency id: eur>
555- def as_euro
556- exchange_to ( "EUR" )
557- end
558-
559516 # Splits a given amount in parts without losing pennies. The left-over pennies will be
560517 # distributed round-robin amongst the parties. This means that parts listed first will likely
561518 # receive more pennies than ones listed later.
0 commit comments