class Spree::Money

Attributes

default_formatting_rules[RW]
money[R]
options[R]

Public Class Methods

new(amount, options = {}) click to toggle source
# File lib/spree/money.rb, line 21
def initialize(amount, options = {})
  use_default_currency
  @money   = Monetize.parse([amount, (options[:currency] || Spree::Config[:currency])].join)
  @options = Spree::Money.default_formatting_rules.merge(options)
end

Public Instance Methods

==(obj) click to toggle source
# File lib/spree/money.rb, line 63
def ==(obj)
  money == obj.money
end
amount_in_cents() click to toggle source
# File lib/spree/money.rb, line 27
def amount_in_cents
  (cents / currency.subunit_to_unit.to_f * 100).round
end
as_json(*) click to toggle source
# File lib/spree/money.rb, line 51
def as_json(*)
  to_s
end
decimal_mark() click to toggle source
# File lib/spree/money.rb, line 55
def decimal_mark
  options[:decimal_mark] || money.decimal_mark
end
thousands_separator() click to toggle source
# File lib/spree/money.rb, line 59
def thousands_separator
  options[:thousands_separator] || money.thousands_separator
end
to_html(opts = { html: true }) click to toggle source

1) prevent blank, breaking spaces 2) prevent escaping of HTML character entities

# File lib/spree/money.rb, line 37
def to_html(opts = { html: true })
  # html option is deprecated and we need to fallback to html_wrap
  opts[:html_wrap] = opts[:html]
  opts.delete(:html)

  output = money.format(options.merge(opts))
  if opts[:html_wrap]
    output.gsub!(/<\/?[^>]*>/, '') # we don't want wrap every element in span
    output = output.sub(' ', '&nbsp;').html_safe
  end

  output
end
to_s() click to toggle source
# File lib/spree/money.rb, line 31
def to_s
  money&.format(options)
end
use_default_currency() click to toggle source
# File lib/spree/money.rb, line 67
def use_default_currency
  currency = Spree::Store.default.default_currency || Spree::Config[:currency]
  ::Money.default_currency = currency
end