Skip to content

Instantly share code, notes, and snippets.

@djbender
Forked from nilakanta/my_app_money.rb
Created March 25, 2013 21:08
Show Gist options
  • Save djbender/5240771 to your computer and use it in GitHub Desktop.
Save djbender/5240771 to your computer and use it in GitHub Desktop.
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
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