Class: Money
- Inherits:
-
Object
- Object
- Money
- Defined in:
- lib/money-rails/money.rb,
lib/money-rails/mongoid/money.rb
Class Method Summary collapse
- .default_formatting_rules ⇒ Object
-
.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.
- .orig_default_formatting_rules ⇒ Object
Instance Method Summary collapse
-
#mongoize ⇒ Object
Converts an object of this instance into a database friendly value.
-
#to_hash ⇒ Object
This is expected to be called by ActiveSupport when calling as_json an Money object.
Class Method Details
.default_formatting_rules ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/money-rails/money.rb', line 8 def default_formatting_rules rules = orig_default_formatting_rules || {} 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 rules end |
.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 |
# File 'lib/money-rails/mongoid/money.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 end end |
.evolve(object) ⇒ Object
Converts the object that was supplied to a criteria and converts it into a database friendly form.
35 36 37 38 39 40 |
# File 'lib/money-rails/mongoid/money.rb', line 35 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.
24 25 26 27 28 29 30 31 |
# File 'lib/money-rails/mongoid/money.rb', line 24 def mongoize(object) return object.mongoize if object.is_a?(Money) return mongoize_hash(object) if object.is_a?(Hash) return nil if object.nil? return mongoize_castable(object) if object.respond_to?(:to_money) object end |
.orig_default_formatting_rules ⇒ Object
6 |
# File 'lib/money-rails/money.rb', line 6 alias_method :orig_default_formatting_rules, :default_formatting_rules |
Instance Method Details
#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.to_f, currency_iso: currency.iso_code } end |
#to_hash ⇒ Object
This is expected to be called by ActiveSupport when calling as_json an Money object
26 27 28 |
# File 'lib/money-rails/money.rb', line 26 def to_hash { cents: cents, currency_iso: currency.iso_code.to_s } end |