-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Mongoid versions >= 3.0.0
- Loading branch information
Showing
2 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class Money | ||
|
||
# Converts an object of this instance into a database friendly value. | ||
def mongoize | ||
{ | ||
:cents => cents, | ||
:currency_iso => currency.iso_code | ||
} | ||
end | ||
|
||
class << self | ||
|
||
# Get the object as it was stored in the database, and instantiate | ||
# this custom class from it. | ||
def demongoize(object) | ||
return nil if object.nil? | ||
|
||
::Money.new(object[:cents], object[:currency_iso]) | ||
end | ||
|
||
# Takes any possible object and converts it to how it would be | ||
# stored in the database. | ||
def mongoize(object) | ||
case object | ||
when Money then object.mongoize | ||
when Hash then ::Money.new(object[:cents], object[:currency]).mongoize | ||
else object | ||
end | ||
end | ||
|
||
# Converts the object that was supplied to a criteria and converts it | ||
# into a database friendly form. | ||
def evolve(object) | ||
case object | ||
when Money then object.mongoize | ||
else object | ||
end | ||
end | ||
end | ||
end |