Sha256: 749c5e67d06ad9d2ab50eafb6662df5e92fc6a19a290d206463048b0cf337b95

Contents?: true

Size: 905 Bytes

Versions: 13

Compression:

Stored size: 905 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.respond_to?(:to_money)
      begin
        serialize(object.to_money)
      rescue ArgumentError
        raise if MoneyRails.raise_error_on_money_parsing
        nil
      end
    else nil
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
money-rails-1.6.2 lib/money-rails/mongoid/two.rb
money-rails-1.6.1 lib/money-rails/mongoid/two.rb
money-rails-1.6.0 lib/money-rails/mongoid/two.rb
money-rails-1.5.0 lib/money-rails/mongoid/two.rb
money-rails-1.4.1 lib/money-rails/mongoid/two.rb
money-rails-1.4.0 lib/money-rails/mongoid/two.rb
money-rails-1.3.0 lib/money-rails/mongoid/two.rb
money-rails-1.2.0 lib/money-rails/mongoid/two.rb
money-rails-1.1.0 lib/money-rails/mongoid/two.rb
money-rails-1.0.0 lib/money-rails/mongoid/two.rb
money-rails-0.12.0 lib/money-rails/mongoid/two.rb
money-rails-0.11.0 lib/money-rails/mongoid/two.rb
money-rails-0.10.0 lib/money-rails/mongoid/two.rb