Class: Spree::Money

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

Overview

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

Constant Summary collapse

DifferentCurrencyError =
Class.new(StandardError)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, options = {}) ⇒ Money

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



35
36
37
38
39
40
41
42
43
44
# File 'app/models/spree/money.rb', line 35

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

Class Attribute Details

.default_formatting_rulesObject

Returns the value of attribute default_formatting_rules.



12
13
14
# File 'app/models/spree/money.rb', line 12

def default_formatting_rules
  @default_formatting_rules
end

Instance Attribute Details

#moneyObject (readonly)

Returns the value of attribute money.



29
30
31
# File 'app/models/spree/money.rb', line 29

def money
  @money
end

Class Method Details

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



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

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

.parse_to_money(amount, currency) ⇒ Object

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.



19
20
21
# File 'app/models/spree/money.rb', line 19

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

Instance Method Details

#+(other) ⇒ Object

Raises:

  • (TypeError)


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

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

#-(other) ⇒ Object

Raises:

  • (TypeError)


111
112
113
114
# File 'app/models/spree/money.rb', line 111

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

#<=>(other) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/spree/money.rb', line 87

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

#==(other) ⇒ Object

Delegates comparison to the internal ruby money instance.



106
107
108
109
# File 'app/models/spree/money.rb', line 106

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

#as_jsonString

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



83
84
85
# File 'app/models/spree/money.rb', line 83

def as_json(*)
  to_s
end

#format(options = {}) ⇒ String

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



65
66
67
# File 'app/models/spree/money.rb', line 65

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

#to_html(options = {html_wrap: true}) ⇒ String

Note:

If you pass in options, ensure you pass in the { html_wrap: true } as well.

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

Parameters:

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

    additional formatting options

Returns:

  • (String)

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



73
74
75
76
77
78
79
80
# File 'app/models/spree/money.rb', line 73

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

#to_sString

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



48
49
50
# File 'app/models/spree/money.rb', line 48

def to_s
  format
end