Class: Spree::Money
Overview
Spree::Money is a relatively thin wrapper around Monetize which handles formatting via Spree::Config.
Constant Summary collapse
- DifferentCurrencyError =
Class.new(StandardError)
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
- .parse(amount, currency = Spree::Config[:currency]) ⇒ Object
- .parse_to_money(amount, currency) ⇒ Object private
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
-
#==(other) ⇒ Object
Delegates comparison to the internal ruby money instance.
-
#as_json ⇒ String
The value of this money object formatted according to its options.
-
#format(options = {}) ⇒ 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_wrap: true }) ⇒ String
The value of this money object formatted according to its options and any additional options, by default with html_wrap.
-
#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.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/spree/money.rb', line 34 def initialize(amount, = {}) if amount.is_a?(::Money) @money = amount else currency = [:currency] || Spree::Config[:currency] @money = Monetize.from_string(amount, currency) end @options = 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.
28 29 30 |
# File 'lib/spree/money.rb', line 28 def money @money end |
Class Method Details
.parse(amount, currency = Spree::Config[:currency]) ⇒ Object
13 14 15 |
# File 'lib/spree/money.rb', line 13 def parse(amount, currency = Spree::Config[:currency]) new(parse_to_money(amount, currency)) end |
.parse_to_money(amount, currency) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
18 19 20 |
# File 'lib/spree/money.rb', line 18 def parse_to_money(amount, currency) ::Monetize.parse(amount, currency) end |
Instance Method Details
#+(other) ⇒ Object
115 116 117 118 |
# File 'lib/spree/money.rb', line 115 def +(other) raise TypeError, "Can't add #{other.class} to Spree::Money" if !other.respond_to?(:money) self.class.new(@money + other.money) end |
#-(other) ⇒ Object
110 111 112 113 |
# File 'lib/spree/money.rb', line 110 def -(other) raise TypeError, "Can't subtract #{other.class} to Spree::Money" if !other.respond_to?(:money) self.class.new(@money - other.money) end |
#<=>(other) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/spree/money.rb', line 86 def <=>(other) if !other.respond_to?(:money) raise TypeError, "Can't compare #{other.class} to Spree::Money" end if currency != other.currency # By default, ::Money will try to run a conversion on `other.money` and # try a comparison on that. We do not want any currency conversion to # take place so we'll catch this here and raise an error. raise( DifferentCurrencyError, "Can't compare #{currency} with #{other.currency}" ) end @money <=> other.money end |
#==(other) ⇒ Object
Delegates comparison to the internal ruby money instance.
105 106 107 108 |
# File 'lib/spree/money.rb', line 105 def ==(other) raise TypeError, "Can't compare #{other.class} to Spree::Money" if !other.respond_to?(:money) @money == other.money end |
#as_json ⇒ String
Returns the value of this money object formatted according to its options.
82 83 84 |
# File 'lib/spree/money.rb', line 82 def as_json(*) to_s end |
#format(options = {}) ⇒ String
Returns the value of this money object formatted according to its options.
64 65 66 |
# File 'lib/spree/money.rb', line 64 def format( = {}) @money.format(@options.merge()) end |
#to_html(options = { html_wrap: true }) ⇒ String
If you pass in options, ensure you pass in the { html_wrap: true } as well.
Returns the value of this money object formatted according to its options and any additional options, by default with html_wrap.
72 73 74 75 76 77 78 79 |
# File 'lib/spree/money.rb', line 72 def to_html( = { html_wrap: true }) output = format() # Maintain compatibility by checking html option renamed to html_wrap. if [:html_wrap] output = output.html_safe end output end |
#to_s ⇒ String
Returns the value of this money object formatted according to its options.
47 48 49 |
# File 'lib/spree/money.rb', line 47 def to_s format end |