class ElegantMoney
Attributes
amount[R]
currency[R]
Public Class Methods
configuration()
click to toggle source
# File lib/elegant_money.rb, line 10 def self.configuration @configuration ||= Configuration.new end
configure() { |configuration| ... }
click to toggle source
# File lib/elegant_money.rb, line 6 def self.configure yield configuration end
new(amount, currency = 'EUR')
click to toggle source
# File lib/elegant_money.rb, line 14 def initialize(amount, currency = 'EUR') @amount = BigDecimal.new(amount.to_s) @currency = currency end
Public Instance Methods
*(other)
click to toggle source
# File lib/elegant_money.rb, line 41 def *(other) operate_with_normalized_currency(other) do |other_amount| amount * other_amount end end
+(other)
click to toggle source
# File lib/elegant_money.rb, line 29 def +(other) operate_with_normalized_currency(other) do |other_amount| amount + other_amount end end
-(other)
click to toggle source
# File lib/elegant_money.rb, line 35 def -(other) operate_with_normalized_currency(other) do |other_amount| amount - other_amount end end
/(other)
click to toggle source
# File lib/elegant_money.rb, line 47 def /(other) operate_with_normalized_currency(other) do |other_amount| amount / other_amount end end
<=>(other)
click to toggle source
# File lib/elegant_money.rb, line 24 def <=>(other) other_amount = amount_with_normalized_currency(other) amount.round(2) <=> other_amount.round(2) end
convert_to(new_currency)
click to toggle source
# File lib/elegant_money.rb, line 53 def convert_to(new_currency) return build(amount, currency) if currency == new_currency conversion = conversion_for(new_currency) new_amount = if currency == default_currency amount * conversion else default_amount = amount / conversion_for(currency) default_amount * conversion end build(new_amount, new_currency) end
infinite?()
click to toggle source
# File lib/elegant_money.rb, line 67 def infinite? amount.infinite? end
inspect()
click to toggle source
# File lib/elegant_money.rb, line 19 def inspect "%.2f %s" % [amount, currency] end
Also aliased as: to_s
Private Instance Methods
amount_with_normalized_currency(other)
click to toggle source
# File lib/elegant_money.rb, line 78 def amount_with_normalized_currency(other) return other.convert_to(currency).amount if other.is_a?(ElegantMoney) return BigDecimal.new(other.to_s) if other.is_a?(Float) other end
build(amount, currency)
click to toggle source
# File lib/elegant_money.rb, line 84 def build(amount, currency) ElegantMoney.new(amount, currency) end
conversion_for(key)
click to toggle source
# File lib/elegant_money.rb, line 88 def conversion_for(key) return 1 if key == default_currency ElegantMoney.configuration.conversions.fetch(key) end
default_currency()
click to toggle source
# File lib/elegant_money.rb, line 93 def default_currency ElegantMoney.configuration.default_currency end
operate_with_normalized_currency(other) { |amount_with_normalized_currency(other)| ... }
click to toggle source
# File lib/elegant_money.rb, line 73 def operate_with_normalized_currency(other) result = yield amount_with_normalized_currency(other) build(result, currency) end