Class: Spree::Money
- Inherits:
-
Object
- Object
- Spree::Money
- Defined in:
- lib/spree/money.rb
Overview
Spree::Money is a relatively thin wrapper around Monetize which handles formatting via Spree::Config.
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.
Instance Method Summary collapse
-
#==(obj) ⇒ Object
Delegates comparison to the internal ruby money instance.
-
#as_json ⇒ String
The value of this money object formatted according to its options.
-
#initialize(amount, options = {}) ⇒ Money
constructor
A new instance of Money.
-
#to_html(options = { html: true }) ⇒ String
The value of this money object formatted according to its options and any additional options, by default as html.
-
#to_s ⇒ String
The value of this money object formatted according to its options.
Constructor Details
#initialize(amount, options = {}) ⇒ Money
Returns a new instance of Money.
35 36 37 38 |
# File 'lib/spree/money.rb', line 35 def initialize(amount, ={}) @money = Monetize.parse([amount, ([:currency] || Spree::Config[:currency])].join) @options = Spree::Money.default_formatting_rules.merge() end |
Class Attribute Details
.default_formatting_rules ⇒ Object
Returns the value of attribute default_formatting_rules.
10 11 12 |
# File 'lib/spree/money.rb', line 10 def default_formatting_rules @default_formatting_rules end |
Instance Attribute Details
#money ⇒ Object (readonly)
Returns the value of attribute money.
18 19 20 |
# File 'lib/spree/money.rb', line 18 def money @money end |
Instance Method Details
#==(obj) ⇒ Object
Delegates comparison to the internal ruby money instance.
68 69 70 |
# File 'lib/spree/money.rb', line 68 def ==(obj) @money == obj.money end |
#as_json ⇒ String
Returns the value of this money object formatted according to its options.
61 62 63 |
# File 'lib/spree/money.rb', line 61 def as_json(*) to_s end |
#to_html(options = { html: true }) ⇒ String
If you pass in options, ensure you pass in the html: true as well.
Returns the value of this money object formatted according to its options and any additional options, by default as html.
50 51 52 53 54 55 56 57 58 |
# File 'lib/spree/money.rb', line 50 def to_html( = { html: true }) output = @money.format(@options.merge()) if [:html] # 1) prevent blank, breaking spaces # 2) prevent escaping of HTML character entities output = output.sub(" ", " ").html_safe end output end |
#to_s ⇒ String
Returns the value of this money object formatted according to its options.
42 43 44 |
# File 'lib/spree/money.rb', line 42 def to_s @money.format(@options) end |