Class: I18n::Complements::Numisma::Currency

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/complements/numisma/currency.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @code = code.strip # .upcase
  @active = (options[:active] ? true : false)
  @cash = options[:cash].to_a.collect(&:to_f).sort
  @countries = options[:countries].to_a.collect(&:to_s).sort.collect(&:to_sym)
  @number = options[:number].to_i
  @precision = options[:precision].to_i
  @unit = (options[:unit].nil? ? nil : options[:unit].to_s)
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



5
6
7
# File 'lib/i18n/complements/numisma/currency.rb', line 5

def active
  @active
end

#cashObject (readonly)

Returns the value of attribute cash.



5
6
7
# File 'lib/i18n/complements/numisma/currency.rb', line 5

def cash
  @cash
end

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/i18n/complements/numisma/currency.rb', line 5

def code
  @code
end

#countriesObject (readonly)

Returns the value of attribute countries.



5
6
7
# File 'lib/i18n/complements/numisma/currency.rb', line 5

def countries
  @countries
end

#numberObject (readonly)

Returns the value of attribute number.



5
6
7
# File 'lib/i18n/complements/numisma/currency.rb', line 5

def number
  @number
end

#precisionObject (readonly)

Returns the value of attribute precision.



5
6
7
# File 'lib/i18n/complements/numisma/currency.rb', line 5

def precision
  @precision
end

#unitObject (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

#labelObject



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, options = {})
  return unless amount

  # options.symbolize_keys!

  defaults  = I18n.translate('number.format'.to_sym, locale: options[:locale], default: {})
  defaultt  = I18n.translate('number.currency.format'.to_sym, locale: options[:locale], default: {})
  defaultt[:negative_format] ||= ('-' + defaultt[:format]) if defaultt[:format]
  formatcy = I18n.translate("number.currency.formats.#{code}".to_sym, locale: options[: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

#nameObject



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, _options = {})
  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_currencyObject



39
40
41
# File 'lib/i18n/complements/numisma/currency.rb', line 39

def to_currency
  self
end