class Money

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…

Public Class Methods

default_formatting_rules() click to toggle source
# File lib/money-rails/money.rb, line 8
def default_formatting_rules
  rules = orig_default_formatting_rules || {}
  defaults = {
    no_cents_if_whole: MoneyRails::Configuration.no_cents_if_whole,
    symbol: MoneyRails::Configuration.symbol,
    sign_before_symbol: MoneyRails::Configuration.sign_before_symbol
  }.reject { |_k, v| v.nil? }

  rules.reverse_merge!(defaults)

  unless MoneyRails::Configuration.default_format.nil?
    rules.reverse_merge!(MoneyRails::Configuration.default_format)
  end
  rules
end
demongoize(object) click to toggle source

Get the object as it was stored in the database, and instantiate this custom class from it.

# File lib/money-rails/mongoid/money.rb, line 15
def demongoize(object)
  if object.is_a?(Hash)
    if object.respond_to?(:deep_symbolize_keys)
      object = object.deep_symbolize_keys
    else
      object = object.symbolize_keys
    end
    object.has_key?(:cents) ? ::Money.new(object[:cents], object[:currency_iso]) : nil
  else
    nil
  end
end
evolve(object) click to toggle source

Converts the object that was supplied to a criteria and converts it into a database friendly form.

# File lib/money-rails/mongoid/money.rb, line 41
def evolve(object)
  case object
  when Money then object.mongoize
  else object
  end
end
mongoize(object) click to toggle source

Takes any possible object and converts it to how it would be stored in the database.

# File lib/money-rails/mongoid/money.rb, line 30
def mongoize(object)
  return object.mongoize if object.is_a?(Money)
  return mongoize_hash(object) if object.is_a?(Hash)
  return nil if object.nil?
  return mongoize_castable(object) if object.respond_to?(:to_money)

  object
end
orig_default_formatting_rules()

Private Class Methods

mongoize_castable(object) click to toggle source
# File lib/money-rails/mongoid/money.rb, line 63
def mongoize_castable(object)
  object.to_money.mongoize
rescue Money::Currency::UnknownCurrency, Monetize::ParseError => e
  return nil unless MoneyRails.raise_error_on_money_parsing
  raise MoneyRails::Error, e.message
end
mongoize_hash(hash) click to toggle source
# File lib/money-rails/mongoid/money.rb, line 50
def mongoize_hash(hash)
  if hash.respond_to?(:deep_symbolize_keys!)
    hash.deep_symbolize_keys!
  elsif hash.respond_to?(:symbolize_keys!)
    hash.symbolize_keys!
  end

  # Guard for a blank form
  return nil if hash[:cents] == '' && hash[:currency_iso] == ''

  ::Money.new(hash[:cents], hash[:currency_iso]).mongoize
end

Public Instance Methods

deserialize(object) click to toggle source

Mongo friendly -> Money

# File lib/money-rails/mongoid/two.rb, line 9
def deserialize(object)
  return nil if object.nil?

  object = object.with_indifferent_access
  ::Money.new object[:cents], object[:currency_iso]
end
mongoize() click to toggle source

Converts an object of this instance into a database friendly value.

# File lib/money-rails/mongoid/money.rb, line 4
def mongoize
  {
    cents:        cents.mongoize.to_f,
    currency_iso: currency.iso_code.mongoize
  }
end
serialize(object) click to toggle source

Money -> Mongo friendly

# File lib/money-rails/mongoid/two.rb, line 17
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
to_hash() click to toggle source

This is expected to be called by ActiveSupport when calling as_json an Money object

# File lib/money-rails/money.rb, line 26
def to_hash
  { cents: cents, currency_iso: currency.iso_code.to_s }
end