Sha256: d8efd1b1c7481c201452c05af107b8bd8bbdc62b5e92e9ec6d7283d006e41f75
Contents?: true
Size: 1.92 KB
Versions: 6
Compression:
Stored size: 1.92 KB
Contents
class Numeric # Converts this numeric to a Money object in the default currency. It # multiplies the numeric value by 100 and treats that as cents. # # 100.to_money => #<Money @cents=10000> # 100.37.to_money => #<Money @cents=10037> # 1000.to_money(:millicents) => #<Money @cents=1> def to_money(units=:dollars) if units == :millicents MicroMoney.new(self) elsif units == :cents Money.new(self) else Money.new(self * 100) end end def to_micro_money to_money(:millicents) end def millicents MicroMoney.new(self).to_money end alias :millicent :millicents def cents Money.new(self) end alias :cent :cents def dollars Money.new(self * 100) end alias :dollar :dollars end class String # Parses the current string and converts it to a Money object. # Excess characters will be discarded. # # '100'.to_money # => #<Money @cents=10000> # '100.37'.to_money # => #<Money @cents=10037> # '100 USD'.to_money # => #<Money @cents=10000, @currency="USD"> # 'USD 100'.to_money # => #<Money @cents=10000, @currency="USD"> # '$100 USD'.to_money # => #<Money @cents=10000, @currency="USD"> # '1000'.to_money(:millicents) # => #<Money @cents=1, @currency="USD"> def to_money(units=:dollars) # Get the currency. matches = scan /([A-Z]{2,3})/ currency = matches[0] ? matches[0][0] : Money.default_currency # Get the cents amount sans_spaces = gsub(/\s+/, '') matches = sans_spaces.scan /(\-?\d+(?:[\.,]\d+)?)/ money = if matches[0] value = matches[0][0].gsub(/,/, '.') value.to_f else 0 end if units == :millicents MicroMoney.new(money, currency) elsif units == :cents Money.new(money, currency) else Money.new(money * 100, currency) end end def to_micro_money to_money(:millicents) end end
Version data entries
6 entries across 6 versions & 1 rubygems