Class: Spree::Money

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Money.



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

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.



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

def default_formatting_rules
  @default_formatting_rules
end

Instance Attribute Details

#moneyObject (readonly)

Returns the value of attribute money.



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

def money
  @money
end

Instance Method Details

#==(obj) ⇒ Object



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

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

#amount_in_centsObject



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

def amount_in_cents
  (cents / currency.subunit_to_unit.to_f * 100).round
end

#as_jsonObject



49
50
51
# File 'lib/spree/money.rb', line 49

def as_json(*)
  to_s
end

#decimal_markObject



53
54
55
# File 'lib/spree/money.rb', line 53

def decimal_mark
  options[:decimal_mark] || money.decimal_mark
end

#thousands_separatorObject



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

def thousands_separator
  options[:thousands_separator] || money.thousands_separator
end

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

1) prevent blank, breaking spaces 2) prevent escaping of HTML character entities



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/spree/money.rb', line 35

def to_html(opts = { html: true })
  # html option is deprecated and we need to fallback to html_wrap
  opts[:html_wrap] = opts[:html]
  opts.delete(:html)

  output = money.format(options.merge(opts))
  if opts[:html_wrap]
    output.gsub!(/<\/?[^>]*>/, '') # we don't want wrap every element in span
    output = output.sub(' ', '&nbsp;').html_safe
  end

  output
end

#to_sObject



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

def to_s
  money.format(options)
end