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.
Constant Summary collapse
- RUBY_NUMERIC_STRING =
/\A-?\d+(\.\d+)?\z/
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
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: 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 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/spree/money.rb', line 35 def initialize(amount, = {}) if amount.is_a?(::Money) @money = amount else currency = ([:currency] || Spree::Config[:currency]) if amount.to_s =~ RUBY_NUMERIC_STRING @money = Monetize.from_string(amount, currency) else @money = Spree::Money.parse_to_money(amount, currency) Spree::Deprecation.warn "Spree::Money was initialized with \#{amount.inspect}, which will not be supported in the future.\nInstead use Spree::Money.new(\#{@money.to_s.inspect}, options) or Spree::Money.parse(\#{amount.inspect})\n".squish, caller end end = Spree::Money.default_formatting_rules.merge() end |
Class Attribute Details
.default_formatting_rules ⇒ Object
Returns the value of attribute default_formatting_rules.
12 13 14 |
# File 'lib/spree/money.rb', line 12 def default_formatting_rules @default_formatting_rules end |
Instance Attribute Details
#money ⇒ Object (readonly)
Returns the value of attribute money.
29 30 31 |
# File 'lib/spree/money.rb', line 29 def money @money end |
Class Method Details
.parse(amount, currency = Spree::Config[:currency]) ⇒ Object
14 15 16 |
# File 'lib/spree/money.rb', line 14 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.
19 20 21 |
# File 'lib/spree/money.rb', line 19 def parse_to_money(amount, currency) ::Monetize.parse(amount, currency) end |
Instance Method Details
#+(other) ⇒ Object
108 109 110 111 |
# File 'lib/spree/money.rb', line 108 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
103 104 105 106 |
# File 'lib/spree/money.rb', line 103 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
Delegates comparison to the internal ruby money instance.
98 99 100 101 |
# File 'lib/spree/money.rb', line 98 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.
91 92 93 |
# File 'lib/spree/money.rb', line 91 def as_json(*) to_s end |
#format(options = {}) ⇒ String
Returns the value of this money object formatted according to its options.
72 73 74 |
# File 'lib/spree/money.rb', line 72 def format( = {}) @money.format(.merge()) 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.
80 81 82 83 84 85 86 87 88 |
# File 'lib/spree/money.rb', line 80 def to_html( = { html: true }) output = format() 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.
55 56 57 |
# File 'lib/spree/money.rb', line 55 def to_s format end |