class Spree::Money
Spree::Money
is a relatively thin wrapper around Monetize which handles formatting via Spree::Config.
Constants
- DifferentCurrencyError
Attributes
Public Class Methods
@param amount [Money, to_s
] the value of the money object @param options [Hash] the default options for formatting the money object See format
# File lib/spree/money.rb, line 34 def initialize(amount, options = {}) if amount.is_a?(::Money) @money = amount else currency = (options[:currency] || Spree::Config[:currency]) @money = Monetize.from_string(amount, currency) end @options = Spree::Money.default_formatting_rules.merge(options) end
# File lib/spree/money.rb, line 13 def parse(amount, currency = Spree::Config[:currency]) new(parse_to_money(amount, currency)) end
@api private
# File lib/spree/money.rb, line 18 def parse_to_money(amount, currency) ::Monetize.parse(amount, currency) end
Public Instance Methods
# File lib/spree/money.rb, line 115 def +(other) raise TypeError, "Can't add #{other.class} to Spree::Money" if !other.respond_to?(:money) self.class.new(@money + other.money) end
# File lib/spree/money.rb, line 110 def -(other) raise TypeError, "Can't subtract #{other.class} to Spree::Money" if !other.respond_to?(:money) self.class.new(@money - other.money) end
# File lib/spree/money.rb, line 86 def <=>(other) if !other.respond_to?(:money) raise TypeError, "Can't compare #{other.class} to Spree::Money" end if currency != other.currency # By default, ::Money will try to run a conversion on `other.money` and # try a comparison on that. We do not want any currency conversion to # take place so we'll catch this here and raise an error. raise( DifferentCurrencyError, "Can't compare #{currency} with #{other.currency}" ) end @money <=> other.money end
Delegates comparison to the internal ruby money instance.
@see www.rubydoc.info/gems/money/Money/Arithmetic#%3D%3D-instance_method
# File lib/spree/money.rb, line 105 def ==(other) raise TypeError, "Can't compare #{other.class} to Spree::Money" if !other.respond_to?(:money) @money == other.money end
(see to_s
)
# File lib/spree/money.rb, line 82 def as_json(*) to_s end
@param options [Hash, String] the options for formatting the money object @option options [Boolean] with_currency when true, show the currency @option options [Boolean] no_cents when true, round to the closest dollar @option options [String] decimal_mark the mark for delimiting the
decimals
@option options [String, false, nil] thousands_separator the character to
delimit powers of 1000, if one is desired, otherwise false or nil
@option options [Boolean] sign_before_symbol when true the sign of the
value comes before the currency symbol
@option options [:before, :after] symbol_position the position of the
currency symbol
@return [String] the value of this money object formatted according to
its options
# File lib/spree/money.rb, line 64 def format(options = {}) @money.format(@options.merge(options)) end
@note If you pass in options, ensure you pass in the { html_wrap: true } as well. @param options [Hash] additional formatting options @return [String] the value of this money object formatted according to
its options and any additional options, by default with html_wrap.
# File lib/spree/money.rb, line 72 def to_html(options = { html_wrap: true }) output = format(options) # Maintain compatibility by checking html option renamed to html_wrap. if options[:html_wrap] output = output.html_safe end output end
@return [String] the value of this money object formatted according to
its options
# File lib/spree/money.rb, line 47 def to_s format end