Class: Spree::Money

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/spree/money.rb

Overview

Spree::Money is a relatively thin wrapper around Monetize which handles formatting via Spree::Config.

Constant Summary

DifferentCurrencyError =
Class.new(StandardError)
RUBY_NUMERIC_STRING =
/\A-?\d+(\.\d+)?\z/

Class Attribute Summary (collapse)

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Money) initialize(amount, options = {})

Returns a new instance of Money

Parameters:

  • amount (Money, #to_s)

    the value of the money object

  • options (Hash) (defaults to: {})

    the default options for formatting the money object See #format



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/spree/money.rb', line 37

def initialize(amount, options = {})
  if amount.is_a?(::Money)
    @money = amount
  else
    currency = (options[:currency] || Spree::Config[:currency])
    if amount.to_s =~ RUBY_NUMERIC_STRING
      @money = Monetize.from_string(amount, currency)
    else
      @money = Spree::Money.parse_to_money(amount, currency)
      Spree::Deprecation.warn <<-WARN.squish, caller
        Spree::Money was initialized with #{amount.inspect}, which will not be supported in the future.
        Instead use Spree::Money.new(#{@money.to_s.inspect}, options) or Spree::Money.parse(#{amount.inspect})
      WARN
    end
  end
  @options = Spree::Money.default_formatting_rules.merge(options)
end

Class Attribute Details

+ (Object) default_formatting_rules

Returns the value of attribute default_formatting_rules



14
15
16
# File 'lib/spree/money.rb', line 14

def default_formatting_rules
  @default_formatting_rules
end

Instance Attribute Details

- (Object) money (readonly)

Returns the value of attribute money



31
32
33
# File 'lib/spree/money.rb', line 31

def money
  @money
end

Class Method Details

+ (Object) parse(amount, currency = Spree::Config[:currency])



16
17
18
# File 'lib/spree/money.rb', line 16

def parse(amount, currency = Spree::Config[:currency])
  new(parse_to_money(amount, currency))
end

+ (Object) parse_to_money(amount, currency)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



21
22
23
# File 'lib/spree/money.rb', line 21

def parse_to_money(amount, currency)
  ::Monetize.parse(amount, currency)
end

Instance Method Details

- (Object) +(other)

Raises:

  • (TypeError)


126
127
128
129
# File 'lib/spree/money.rb', line 126

def +(other)
  raise TypeError, "Can't add #{other.class} to Spree::Money" if !other.respond_to?(:money)
  self.class.new(@money + other.money)
end

- (Object) -(other)

Raises:

  • (TypeError)


121
122
123
124
# File 'lib/spree/money.rb', line 121

def -(other)
  raise TypeError, "Can't subtract #{other.class} to Spree::Money" if !other.respond_to?(:money)
  self.class.new(@money - other.money)
end

- (Object) <=>(other)



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/spree/money.rb', line 97

def <=>(other)
  if !other.respond_to?(:money)
    raise TypeError, "Can't compare #{other.class} to Spree::Money"
  end
  if self.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 #{self.currency} with #{other.currency}"
    )
  end
  @money <=> other.money
end

- (Object) ==(other)

Delegates comparison to the internal ruby money instance.



116
117
118
119
# File 'lib/spree/money.rb', line 116

def ==(other)
  raise TypeError, "Can't compare #{other.class} to Spree::Money" if !other.respond_to?(:money)
  @money == other.money
end

- (String) as_json

Returns the value of this money object formatted according to its options

Returns:

  • (String)

    the value of this money object formatted according to its options



93
94
95
# File 'lib/spree/money.rb', line 93

def as_json(*)
  to_s
end

- (String) format(options = {})

Returns the value of this money object formatted according to its options

Parameters:

  • options (Hash, String) (defaults to: {})

    the options for formatting the money object

Options Hash (options):

  • with_currency (Boolean)

    when true, show the currency

  • no_cents (Boolean)

    when true, round to the closest dollar

  • decimal_mark (String)

    the mark for delimiting the decimals

  • thousands_separator (String, false, nil)

    the character to delimit powers of 1000, if one is desired, otherwise false or nil

  • sign_before_symbol (Boolean)

    when true the sign of the value comes before the currency symbol

  • symbol_position (:before, :after)

    the position of the currency symbol

Returns:

  • (String)

    the value of this money object formatted according to its options



74
75
76
# File 'lib/spree/money.rb', line 74

def format(options = {})
  @money.format(@options.merge(options))
end

- (String) to_html(options = { html: true })

Note:

If you pass in options, ensure you pass in the html: true as well.

Returns the value of this money object formatted according to its options and any additional options, by default as html.

Parameters:

  • options (Hash) (defaults to: { html: true })

    additional formatting options

Returns:

  • (String)

    the value of this money object formatted according to its options and any additional options, by default as html.



82
83
84
85
86
87
88
89
90
# File 'lib/spree/money.rb', line 82

def to_html(options = { html: true })
  output = format(options)
  if options[:html]
    # 1) prevent blank, breaking spaces
    # 2) prevent escaping of HTML character entities
    output = output.sub(" ", "&nbsp;").html_safe
  end
  output
end

- (String) to_s

Returns the value of this money object formatted according to its options

Returns:

  • (String)

    the value of this money object formatted according to its options



57
58
59
# File 'lib/spree/money.rb', line 57

def to_s
  format
end