Class: Spree::Money

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

Overview

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Money.

Parameters:

  • amount (#to_s)

    the value of the money object

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

    the options for creating the money object

Options Hash (options):

  • currency (String)

    the currency for the money object

  • 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



35
36
37
38
# File 'lib/spree/money.rb', line 35

def initialize(amount, options={})
  @money = Monetize.parse([amount, (options[:currency] || Spree::Config[:currency])].join)
  @options = Spree::Money.default_formatting_rules.merge(options)
end

Class Attribute Details

.default_formatting_rulesObject

Returns the value of attribute default_formatting_rules.



10
11
12
# File 'lib/spree/money.rb', line 10

def default_formatting_rules
  @default_formatting_rules
end

Instance Attribute Details

#moneyObject (readonly)

Returns the value of attribute money.



18
19
20
# File 'lib/spree/money.rb', line 18

def money
  @money
end

Instance Method Details

#==(obj) ⇒ Object

Delegates comparison to the internal ruby money instance.



68
69
70
# File 'lib/spree/money.rb', line 68

def ==(obj)
  @money == obj.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



61
62
63
# File 'lib/spree/money.rb', line 61

def as_json(*)
  to_s
end

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

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.



50
51
52
53
54
55
56
57
58
# File 'lib/spree/money.rb', line 50

def to_html(options = { html: true })
  output = @money.format(@options.merge(options))
  if options[:html]
    # 1) prevent blank, breaking spaces
    # 2) prevent escaping of HTML character entities
    output = output.sub(" ", " ").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



42
43
44
# File 'lib/spree/money.rb', line 42

def to_s
  @money.format(@options)
end