Sha256: 757bbd99b041a96327eaab62923f3c55cb12216382e8996e5829da446eff041e

Contents?: true

Size: 976 Bytes

Versions: 3

Compression:

Stored size: 976 Bytes

Contents

# Class name does not really matches the folder hierarchy, because
# in order for (de)serialization to work, the class must be re-opened.
# But this file brings mongoid 2.X compat., so...

class Money
  include ::Mongoid::Fields::Serializable

  # Mongo friendly -> Money
  def deserialize(object)
    return nil if object.nil?

    object = object.with_indifferent_access
    ::Money.new object[:cents], object[:currency_iso]
  end

  # Money -> Mongo friendly
  def serialize(object)
    case
    when object.is_a?(Money)
      {
        :cents        => object.cents.is_a?(BigDecimal) ? object.cents.to_s : object.cents,
        :currency_iso => object.currency.iso_code
      }
    when object.nil? then nil
    when object.respond_to?(:to_money)
      begin
        serialize(object.to_money)
      rescue Monetize::ParseError => e
        raise MoneyRails::Error, e.message if MoneyRails.raise_error_on_money_parsing
        nil
      end
    else nil
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
money-rails-1.10.0 lib/money-rails/mongoid/two.rb
money-rails-1.9.0 lib/money-rails/mongoid/two.rb
money-rails-1.8.0 lib/money-rails/mongoid/two.rb