Sha256: 55b9826d79e088cb24576fc2be91f95759a25dde3bb13563e73d812893ce5e91

Contents?: true

Size: 767 Bytes

Versions: 1

Compression:

Stored size: 767 Bytes

Contents

require 'delegate'

module ActsAsMoney
  class Money < DelegateClass(BigDecimal)
    attr_reader :amount, :decimals
    
    def initialize amount, decimals = 2
      @decimals = decimals
      amount = 0 unless amount
      @amount = BigDecimal.new(amount.to_s).round(@decimals)
      super @amount
    end

    %w(+ - * /).each do |op|
      class_eval %Q{
        def #{op} other
          other = BigDecimal.new(other.to_s) if !other.is_a?(BigDecimal) && !other.is_a?(Money) 
          Money.new super, decimals
        end        
      }
    end
    
    def hash
      @amount.hash
    end
    
    def eql? other
      @amount == other.amount
    end  
    
    def to_s
      "$%.#{@decimals}f" % @amount
    end

    def inspect
      to_s
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eeng-money-0.0.1 lib/acts_as_money/money.rb