Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/monetize/core_extensions/hash.rb

Instance Method Summary collapse

Instance Method Details

#to_money(currency = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/monetize/core_extensions/hash.rb', line 4

def to_money(currency = nil)
  money_hash = self.respond_to?(:with_indifferent_access) ? self.with_indifferent_access : self

  hash_currency = if money_hash[:currency].is_a?(Hash)
                    money_hash[:currency][:iso_code]
                  elsif money_hash[:currency_iso] && !money_hash[:currency_iso].empty?
                    money_hash[:currency_iso]
                  else
                    money_hash[:currency]
                  end

  Money.new(money_hash[:cents] || money_hash[:fractional], hash_currency || currency || Money.default_currency)
end