Class: Spree::Money
Class Attribute Summary collapse
-
.default_formatting_rules ⇒ Object
Returns the value of attribute default_formatting_rules.
Instance Attribute Summary collapse
-
#money ⇒ Object
readonly
Returns the value of attribute money.
Class Method Summary collapse
Instance Method Summary collapse
- #*(value) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #<=>(other) ⇒ Object
- #==(obj) ⇒ Object
- #abs ⇒ Object
- #amount_in_cents ⇒ Object
- #as_json ⇒ Object
- #decimal_mark ⇒ Object
-
#initialize(amount, options = {}) ⇒ Money
constructor
A new instance of Money.
- #inspect ⇒ Object
- #thousands_separator ⇒ Object
-
#to_html(opts = { html: true }) ⇒ Object
1) prevent blank, breaking spaces 2) prevent escaping of HTML character entities.
- #to_s ⇒ Object
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, = {}) ::Money.default_currency ||= Spree::Store.default.default_currency || 'USD' @money = Monetize.parse(amount, ([:currency] || Spree::Store.default.default_currency)) = Spree::Money.default_formatting_rules.merge() end |
Class Attribute Details
.default_formatting_rules ⇒ Object
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
#money ⇒ Object (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, = {}) money = ::Money.from_cents(amount_in_cents, [:currency]) new(money.to_d, ) 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, ) 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, ) 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, ) end |
#-@ ⇒ Object
102 103 104 |
# File 'lib/spree/money.rb', line 102 def -@ self.class.new((-money).to_s, ) 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 |
#abs ⇒ Object
39 40 41 |
# File 'lib/spree/money.rb', line 39 def abs self.class.new(money.abs, ) end |
#amount_in_cents ⇒ Object
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_json ⇒ Object
67 68 69 |
# File 'lib/spree/money.rb', line 67 def as_json(*) to_s end |
#decimal_mark ⇒ Object
71 72 73 |
# File 'lib/spree/money.rb', line 71 def decimal_mark [:decimal_mark] || money.decimal_mark end |
#inspect ⇒ Object
47 48 49 |
# File 'lib/spree/money.rb', line 47 def inspect "#{self.class}(cents: #{cents}, currency: #{currency})" end |
#thousands_separator ⇒ Object
75 76 77 |
# File 'lib/spree/money.rb', line 75 def thousands_separator [: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(.merge(opts)) if opts[:html_wrap] output.gsub!(/<\/?[^>]*>/, '') # we don't want wrap every element in span output = output.sub(' ', ' ').html_safe end output end |
#to_s ⇒ Object
43 44 45 |
# File 'lib/spree/money.rb', line 43 def to_s money&.format() end |