Sha256: a935a7fdc45c6e5e41c64f4a0fab64be7373a6803ebfcd9d332dff05340f5269

Contents?: true

Size: 790 Bytes

Versions: 1

Compression:

Stored size: 790 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
      ActiveSupport::NumberHelper.number_to_currency to_d
    end

    def inspect
      to_s
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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