Class: Money

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid_money_field/field.rb

Overview

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.



17
18
19
20
21
22
23
24
# File 'lib/mongoid_money_field/field.rb', line 17

def demongoize(object)
  if object.is_a?(Hash)
    object = object.symbolize_keys
    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.



42
43
44
45
46
47
# File 'lib/mongoid_money_field/field.rb', line 42

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.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mongoid_money_field/field.rb', line 28

def mongoize(object)
  case
    when object.is_a?(Money) then object.mongoize
    when object.is_a?(Hash) then
      object.symbolize_keys! if object.respond_to?(:symbolize_keys!)
      ::Money.new(object[:cents], object[:currency_iso]).mongoize
    when object.respond_to?(:to_money) then
      object.to_money.mongoize
    else object
  end
end

Instance Method Details

#mongoizeObject

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



6
7
8
9
10
11
# File 'lib/mongoid_money_field/field.rb', line 6

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