Class: Spree::Money

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

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.



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

def initialize(amount, options = {})
  ::Money.default_currency ||= Spree::Store.default.default_currency || 'USD'
  @money   = Monetize.parse(amount, (options[:currency] || Spree::Store.default.default_currency))
  @options = Spree::Money.default_formatting_rules.merge(options)
end

Class Attribute Details

.default_formatting_rulesObject

Returns the value of attribute default_formatting_rules.



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

def default_formatting_rules
  @default_formatting_rules
end

Instance Attribute Details

#moneyObject (readonly)

Returns the value of attribute money.



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

def money
  @money
end

Class Method Details

.from_cents(amount_in_cents, options = {}) ⇒ Object



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

def from_cents(amount_in_cents, options = {})
  money = ::Money.from_cents(amount_in_cents, options[:currency])
  new(money.to_d, options)
end

Instance Method Details

#*(value) ⇒ Object



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

def *(value)
  result_money = money * value
  self.class.new(result_money.to_s, options)
end

#+(other) ⇒ Object



83
84
85
86
# File 'lib/spree/money.rb', line 83

def +(other)
  result_money = money + other.money
  self.class.new(result_money.to_s, options)
end

#-(other) ⇒ Object



88
89
90
91
# File 'lib/spree/money.rb', line 88

def -(other)
  result_money = money - other.money
  self.class.new(result_money.to_s, options)
end

#-@Object



102
103
104
# File 'lib/spree/money.rb', line 102

def -@
  self.class.new((-money).to_s, options)
end

#<=>(other) ⇒ Object



98
99
100
# File 'lib/spree/money.rb', line 98

def <=>(other)
  money <=> other.money
end

#==(obj) ⇒ Object



79
80
81
# File 'lib/spree/money.rb', line 79

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

#absObject



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

def abs
  self.class.new(money.abs, options)
end

#amount_in_centsObject



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

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

#as_jsonObject



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

def as_json(*)
  to_s
end

#decimal_markObject



71
72
73
# File 'lib/spree/money.rb', line 71

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

#inspectObject



47
48
49
# File 'lib/spree/money.rb', line 47

def inspect
  "#{self.class}(cents: #{cents}, currency: #{currency})"
end

#thousands_separatorObject



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

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



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/spree/money.rb', line 53

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



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

def to_s
  money&.format(options)
end