-
-
Save djbender/5240771 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module MyApp | |
class Money | |
include Mongoid::Fields::Serializable | |
def cast_on_read?; true; end | |
def deserialize(object) | |
return nil if object.blank? | |
::Money.new(object[:cents] || object["cents"], object[:currency] || object["currency"]) | |
end | |
def serialize(object) | |
return nil if object.blank? | |
{:cents => object.cents, :currency => object.currency_as_string} | |
end | |
end | |
end |
This file contains hidden or 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
class MyModel | |
include Mongoid::Document | |
field :price, type: MyApp::Money | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment