Class: I18n::Complements::Numisma::Currency
- Inherits:
-
Object
- Object
- I18n::Complements::Numisma::Currency
- Defined in:
- lib/i18n/complements/numisma/currency.rb
Instance Attribute Summary collapse
-
#active ⇒ Object
readonly
Returns the value of attribute active.
-
#cash ⇒ Object
readonly
Returns the value of attribute cash.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#countries ⇒ Object
readonly
Returns the value of attribute countries.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#precision ⇒ Object
readonly
Returns the value of attribute precision.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Instance Method Summary collapse
- #==(other_currency) ⇒ Object
-
#initialize(code, options = {}) ⇒ Currency
constructor
A new instance of Currency.
- #label ⇒ Object
-
#localize(amount, options = {}) ⇒ Object
Produces a amount of the currency with the locale parameters TODO: Find a better way to specify number formats which are more complex that the default Rails use.
- #name ⇒ Object
- #round(value, _options = {}) ⇒ Object
- #to_currency ⇒ Object
Constructor Details
#initialize(code, options = {}) ⇒ Currency
Returns a new instance of Currency.
7 8 9 10 11 12 13 14 15 |
# File 'lib/i18n/complements/numisma/currency.rb', line 7 def initialize(code, = {}) @code = code.strip # .upcase @active = ([:active] ? true : false) @cash = [:cash].to_a.collect(&:to_f).sort @countries = [:countries].to_a.collect(&:to_s).sort.collect(&:to_sym) @number = [:number].to_i @precision = [:precision].to_i @unit = ([:unit].nil? ? nil : [:unit].to_s) end |
Instance Attribute Details
#active ⇒ Object (readonly)
Returns the value of attribute active.
5 6 7 |
# File 'lib/i18n/complements/numisma/currency.rb', line 5 def active @active end |
#cash ⇒ Object (readonly)
Returns the value of attribute cash.
5 6 7 |
# File 'lib/i18n/complements/numisma/currency.rb', line 5 def cash @cash end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
5 6 7 |
# File 'lib/i18n/complements/numisma/currency.rb', line 5 def code @code end |
#countries ⇒ Object (readonly)
Returns the value of attribute countries.
5 6 7 |
# File 'lib/i18n/complements/numisma/currency.rb', line 5 def countries @countries end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
5 6 7 |
# File 'lib/i18n/complements/numisma/currency.rb', line 5 def number @number end |
#precision ⇒ Object (readonly)
Returns the value of attribute precision.
5 6 7 |
# File 'lib/i18n/complements/numisma/currency.rb', line 5 def precision @precision end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
5 6 7 |
# File 'lib/i18n/complements/numisma/currency.rb', line 5 def unit @unit end |
Instance Method Details
#==(other_currency) ⇒ Object
35 36 37 |
# File 'lib/i18n/complements/numisma/currency.rb', line 35 def ==(other_currency) code == other_currency.code end |
#label ⇒ Object
21 22 23 |
# File 'lib/i18n/complements/numisma/currency.rb', line 21 def label ::I18n.translate('labels.currency_with_code', code: code, name: name, default: '%{name} (%{code})') end |
#localize(amount, options = {}) ⇒ Object
Produces a amount of the currency with the locale parameters TODO: Find a better way to specify number formats which are more complex that the default Rails use
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/i18n/complements/numisma/currency.rb', line 45 def localize(amount, = {}) return unless amount # options.symbolize_keys! defaults = I18n.translate('number.format'.to_sym, locale: [:locale], default: {}) defaultt = I18n.translate('number.currency.format'.to_sym, locale: [:locale], default: {}) defaultt[:negative_format] ||= ('-' + defaultt[:format]) if defaultt[:format] formatcy = I18n.translate("number.currency.formats.#{code}".to_sym, locale: [:locale], default: {}) formatcy[:negative_format] ||= '-' + formatcy[:format] if formatcy[:format] formatter = {} formatter[:separator] = formatcy[:separator] || defaultt[:separator] || defaults[:separator] || ',' formatter[:delimiter] = formatcy[:delimiter] || defaultt[:delimiter] || defaults[:delimiter] || '' formatter[:precision] = formatcy[:precision] || precision || defaultt[:precision] || 3 format = formatcy[:format] || defaultt[:format] || '%n-%u' # defaults[:format] || negative_format = formatcy[:negative_format] || defaultt[:negative_format] || defaults[:negative_format] || '-' + format unit = formatcy[:unit] || self.unit || code if amount.to_f < 0 format = negative_format # options.delete(:negative_format) amount = amount.respond_to?('abs') ? amount.abs : amount.sub(/^-/, '') end value = amount.to_s integers, decimals = value.split(/\./) value = integers.gsub(/^0+[1-9]+/, '').gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{formatter[:delimiter]}") unless decimals.to_s =~ /^\s*$/ value << formatter[:separator] value << decimals.gsub(/0+$/, '').ljust(formatter[:precision], '0').scan(/.{1,3}/).join(formatter[:delimiter]) end format.gsub(/%n/, value).gsub(/%u/, unit).gsub(/%s/, "\u{00A0}") end |
#name ⇒ Object
17 18 19 |
# File 'lib/i18n/complements/numisma/currency.rb', line 17 def name ::I18n.translate("currencies.#{code}") end |
#round(value, _options = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/i18n/complements/numisma/currency.rb', line 25 def round(value, = {}) precision = self.precision if RUBY_VERSION =~ /^1\.8/ magnitude = 10**precision return (value * magnitude).to_i.to_f * magnitude else return value.round(precision) end end |
#to_currency ⇒ Object
39 40 41 |
# File 'lib/i18n/complements/numisma/currency.rb', line 39 def to_currency self end |