Class: Money
- Inherits:
-
Object
- Object
- Money
- 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
-
.demongoize(object) ⇒ Object
Get the object as it was stored in the database, and instantiate this custom class from it.
-
.evolve(object) ⇒ Object
Converts the object that was supplied to a criteria and converts it into a database friendly form.
-
.mongoize(object) ⇒ Object
Takes any possible object and converts it to how it would be stored in the database.
Instance Method Summary collapse
-
#deserialize(object) ⇒ Object
Mongo friendly -> Money.
-
#mongoize ⇒ Object
Converts an object of this instance into a database friendly value.
-
#serialize(object) ⇒ Object
Money -> Mongo friendly.
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 20 21 22 |
# File 'lib/money-rails/mongoid/three.rb', line 15 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.
40 41 42 43 44 45 |
# File 'lib/money-rails/mongoid/three.rb', line 40 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.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/money-rails/mongoid/three.rb', line 26 def mongoize(object) case when object.is_a?(Money) then object.mongoize when object.is_a?(Hash) then object.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
#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 |
#mongoize ⇒ Object
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 25 26 27 28 |
# File 'lib/money-rails/mongoid/two.rb', line 17 def serialize(object) case when object.is_a?(Money) { :cents => object.cents, :currency_iso => object.currency.iso_code } when object.respond_to?(:to_money) serialize(object.to_money) else nil end end |