Class: Spree::Currency
- Inherits:
-
Object
- Object
- Spree::Currency
- Includes:
- ActiveModel::Model, Comparable
- Defined in:
- app/models/spree/currency.rb
Overview
Virtual model for a currency. Wraps a currency code (ISO 4217) and exposes
its display name and select label — the single home for that logic instead
of being restated in helpers. Mirrors Spree::Locale.
Instance Attribute Summary collapse
-
#code ⇒ String
The currency code, e.g.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare/equality by code so a Currency slots into string-keyed collections.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#label ⇒ String
Select label, e.g.
-
#name ⇒ String
Display name, e.g.
- #to_s ⇒ Object
Instance Attribute Details
#code ⇒ String
Returns the currency code, e.g. "USD", "EUR".
11 12 13 |
# File 'app/models/spree/currency.rb', line 11 def code @code end |
Instance Method Details
#<=>(other) ⇒ Object
Compare/equality by code so a Currency slots into string-keyed collections.
35 36 37 |
# File 'app/models/spree/currency.rb', line 35 def <=>(other) to_s <=> other.to_s end |
#eql?(other) ⇒ Boolean
39 40 41 |
# File 'app/models/spree/currency.rb', line 39 def eql?(other) other.is_a?(Spree::Currency) && to_s == other.to_s end |
#hash ⇒ Object
43 44 45 |
# File 'app/models/spree/currency.rb', line 43 def hash to_s.hash end |
#label ⇒ String
Select label, e.g. "USD — United States Dollar".
23 24 25 26 27 28 |
# File 'app/models/spree/currency.rb', line 23 def label upper = code.to_s.upcase return upper if name.blank? || name.casecmp?(code.to_s) "#{upper} — #{name}" end |
#name ⇒ String
Display name, e.g. "United States Dollar". Falls back to the code for an unknown currency.
16 17 18 19 |
# File 'app/models/spree/currency.rb', line 16 def name # `find` returns nil (it does not raise) for unknown codes. ::Money::Currency.find(code.to_s.upcase)&.name || code.to_s.upcase end |
#to_s ⇒ Object
30 31 32 |
# File 'app/models/spree/currency.rb', line 30 def to_s code.to_s.upcase end |