Class: Money

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Fields::Serializable
Defined in:
lib/money-rails/mongoid/two.rb,
lib/money-rails/money.rb,
lib/money-rails/mongoid/money.rb

Overview

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 Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_formatting_rulesObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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) ⇒ Object

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



15
16
17
18
19
20
21
22
23
24
25
26
# 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) ⇒ Object

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



41
42
43
44
45
46
# 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) ⇒ Object

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



30
31
32
33
34
35
36
37
# 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_rulesObject



6
# File 'lib/money-rails/money.rb', line 6

alias_method :orig_default_formatting_rules, :default_formatting_rules

Instance Method Details

#deserialize(object) ⇒ Object

Mongo friendly -> Money



9
10
11
12
13
14
# 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

#mongoizeObject

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



4
5
6
7
8
9
# 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) ⇒ Object

Money -> Mongo friendly



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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_hashObject

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



26
27
28
# File 'lib/money-rails/money.rb', line 26

def to_hash
  { cents: cents, currency_iso: currency.iso_code.to_s }
end