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



86
87
88
89
# File 'lib/spree/money.rb', line 86

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

#+(other) ⇒ Object



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

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

#-(other) ⇒ Object



81
82
83
84
# File 'lib/spree/money.rb', line 81

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

#-@Object



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

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

#<=>(other) ⇒ Object



91
92
93
# File 'lib/spree/money.rb', line 91

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

#==(obj) ⇒ Object



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

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



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

def as_json(*)
  to_s
end

#decimal_markObject



64
65
66
# File 'lib/spree/money.rb', line 64

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



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

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
# File 'lib/spree/money.rb', line 53

def to_html(opts = { html: true })
  opts.delete(:html)

  output = money.format(options.merge(opts).merge(html_wrap: false))
  output.sub(' ', '&nbsp;').html_safe
end

#to_sObject



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

def to_s
  money&.format(options)
end