Top Level Namespace

Defined Under Namespace

Classes: Monies

Instance Method Summary collapse

Instance Method Details

#Monies(object, currency = Monies.currency) ⇒ Object



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/monies.rb', line 490

def Monies(object, currency = Monies.currency)
  case object
  when Monies
    object
  when Integer
    Monies.new(object, 0, currency)
  when Rational
    Monies.new(object.numerator, 0, currency) / object.denominator
  when String
    Monies::Digits.load(object, currency)
  else
    if defined?(BigDecimal) && object.is_a?(BigDecimal)
      sign, significant_digits, base, exponent = object.split

      value = significant_digits.to_i * sign

      length = significant_digits.length

      if exponent.positive? && length < exponent
        value *= base ** (exponent - length)
      end

      scale = object.scale

      return Monies.new(value, scale, currency)
    end

    raise TypeError, "can't convert #{object.inspect} into #{Monies}"
  end
end