Class: Spree::Money

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Money



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/spree/money.rb', line 11

def initialize(amount, options={})
  @money = Monetize.parse([amount, (options[:currency] || Spree::Config[:currency])].join)
  @options = {}
  @options[:with_currency] = Spree::Config[:display_currency]
  @options[:symbol_position] = Spree::Config[:currency_symbol_position].to_sym
  @options[:no_cents] = Spree::Config[:hide_cents]
  @options[:decimal_mark] = Spree::Config[:currency_decimal_mark]
  @options[:thousands_separator] = Spree::Config[:currency_thousands_separator]
  @options[:sign_before_symbol] = Spree::Config[:currency_sign_before_symbol]
  @options.merge!(options)
  # Must be a symbol because the Money gem doesn't do the conversion
  @options[:symbol_position] = @options[:symbol_position].to_sym
end

Instance Attribute Details

#moneyObject (readonly)

Returns the value of attribute money



7
8
9
# File 'lib/spree/money.rb', line 7

def money
  @money
end

Instance Method Details

#==(obj) ⇒ Object



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

def ==(obj)
  @money == obj.money
end

#as_jsonObject



39
40
41
# File 'lib/spree/money.rb', line 39

def as_json(*)
  to_s
end

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



29
30
31
32
33
34
35
36
37
# File 'lib/spree/money.rb', line 29

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_sObject



25
26
27
# File 'lib/spree/money.rb', line 25

def to_s
  @money.format(@options)
end