Class: Money
- Inherits:
-
Object
- Object
- Money
- Includes:
- Mongoid::Fields::Serializable
- Defined in:
- lib/money-rails/mongoid/two.rb,
lib/money-rails/money.rb,
lib/money-rails/mongoid/money.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.
- #format_with_settings(*rules) ⇒ Object
-
#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 23 24 25 26 |
# File 'lib/money-rails/mongoid/money.rb', line 15 def demongoize(object) if object.is_a?(Hash) if object.respond_to?(:deep_symbolize_keys) object = object.deep_symbolize_keys else object = object.symbolize_keys end 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.
53 54 55 56 57 58 |
# File 'lib/money-rails/mongoid/money.rb', line 53 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.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/money-rails/mongoid/money.rb', line 30 def mongoize(object) case when object.is_a?(Money) then object.mongoize when object.is_a?(Hash) then if object.respond_to?(:deep_symbolize_keys!) object.deep_symbolize_keys! elsif object.respond_to?(:symbolize_keys!) object.symbolize_keys! end ::Money.new(object[:cents], object[:currency_iso]).mongoize when object.respond_to?(:to_money) then begin object.to_money.mongoize rescue ArgumentError, Money::Currency::UnknownCurrency raise if MoneyRails.raise_error_on_money_parsing nil end 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 |
#format_with_settings(*rules) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/money-rails/money.rb', line 6 def format_with_settings(*rules) rules = normalize_formatting_rules(rules) # Apply global defaults for money only for non-nil values defaults = { no_cents_if_whole: MoneyRails::Configuration.no_cents_if_whole, symbol: MoneyRails::Configuration.symbol, sign_before_symbol: MoneyRails::Configuration.sign_before_symbol }.reject { |k,v| v.nil? } rules.reverse_merge!(defaults) unless MoneyRails::Configuration.default_format.nil? rules.reverse_merge!(MoneyRails::Configuration.default_format) end format_without_settings(rules) 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/money.rb', line 4 def mongoize { :cents => cents.mongoize.to_f, :currency_iso => currency.iso_code.mongoize } end |
#serialize(object) ⇒ Object
Money -> Mongo friendly
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/money-rails/mongoid/two.rb', line 17 def serialize(object) case when object.is_a?(Money) { :cents => object.cents.is_a?(BigDecimal) ? object.cents.to_s : object.cents, :currency_iso => object.currency.iso_code } when object.respond_to?(:to_money) begin serialize(object.to_money) rescue ArgumentError raise if MoneyRails.raise_error_on_money_parsing nil end else nil end end |