Class: Money

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Fields::Serializable
Defined in:
lib/money-rails/mongoid/two.rb,
lib/money-rails/mongoid/three.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

.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
# File 'lib/money-rails/mongoid/three.rb', line 15

def demongoize(object)
  return nil if object.nil?
  object.symbolize_keys!
  ::Money.new(object[:cents], object[:currency_iso])
end

.evolve(object) ⇒ Object

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



35
36
37
38
39
40
# File 'lib/money-rails/mongoid/three.rb', line 35

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.



23
24
25
26
27
28
29
30
31
# File 'lib/money-rails/mongoid/three.rb', line 23

def mongoize(object)
  case object
  when Money then object.mongoize
  when Hash then
    object.symbolize_keys!
    ::Money.new(object[:cents], object[:currency]).mongoize
  else object
  end
end

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/three.rb', line 4

def mongoize
  {
    :cents        => cents,
    :currency_iso => currency.iso_code
  }
end

#serialize(object) ⇒ Object

Money -> Mongo friendly



17
18
19
20
21
22
23
24
# File 'lib/money-rails/mongoid/two.rb', line 17

def serialize(object)
  return nil unless object.is_a? Money

  {
    :cents        => object.cents,
    :currency_iso => object.currency.iso_code
  }
end