Class: Redmineup::Currency
- Inherits:
-
Object
- Object
- Redmineup::Currency
- Extended by:
- Enumerable, Formatting, Heuristics, Loader
- Includes:
- Comparable
- Defined in:
- lib/redmineup/currency.rb,
lib/redmineup/currency/loader.rb,
lib/redmineup/currency/formatting.rb,
lib/redmineup/currency/heuristics.rb
Overview
Represents a specific currency unit.
Defined Under Namespace
Modules: Formatting, Heuristics, Loader Classes: MissingAttributeError, UnknownCurrency
Constant Summary
Constants included from Loader
Instance Attribute Summary collapse
-
#decimal_mark ⇒ String
(also: #separator)
readonly
The decimal mark, or character used to separate the whole unit from the subunit.
-
#disambiguate_symbol ⇒ String
readonly
Alternative currency used if symbol is ambiguous.
-
#html_entity ⇒ String
readonly
The html entity for the currency symbol.
-
#id ⇒ Symbol
readonly
The symbol used to identify the currency, usually THE lowercase
iso_code
attribute. -
#iso_code ⇒ String
readonly
The international 3-letter code as defined by the ISO 4217 standard.
-
#iso_numeric ⇒ String
readonly
The international 3-numeric code as defined by the ISO 4217 standard.
-
#name ⇒ String
readonly
The currency name.
-
#priority ⇒ Integer
readonly
A numerical value you can use to sort/group the currency list.
-
#smallest_denomination ⇒ Object
readonly
Returns the value of attribute smallest_denomination.
-
#subunit ⇒ String
readonly
The name of the fractional monetary unit.
-
#subunit_to_unit ⇒ Integer
readonly
The proportion between the unit and the subunit.
-
#symbol ⇒ String
readonly
The currency symbol (UTF-8 encoded).
-
#symbol_first ⇒ Boolean
readonly
Should the currency symbol precede the amount, or should it come after?.
-
#The ⇒ String
readonly
Character used to separate thousands grouping of the whole unit.
-
#thousands_separator ⇒ Object
(also: #delimiter)
readonly
Returns the value of attribute thousands_separator.
Class Method Summary collapse
- .add_admin_money_menu ⇒ Object
-
.all ⇒ Array
List the currencies imported and registered.
-
.decimal_places_cache ⇒ Object
Cache decimal places for subunit_to_unit values.
- .each ⇒ Object
-
.find(id) ⇒ Money::Currency
Lookup a currency with given
id
an returns aCurrency
instance on success,nil
otherwise. -
.find_by_iso_numeric(num) ⇒ Money::Currency
Lookup a currency with given
num
as an ISO 4217 numeric and returns anCurrency
instance on success,nil
otherwise. -
.register(curr) ⇒ Object
Register a new currency.
-
.stringified_keys ⇒ Set
We need a string-based validator before creating an unbounded number of symbols.
-
.table ⇒ Object
List of known currencies.
-
.unregister(curr) ⇒ Boolean
Unregister a currency.
-
.wrap(object) ⇒ Money::Currency
Wraps the object in a
Currency
unless it’s already aCurrency
object.
Instance Method Summary collapse
-
#<=>(other_currency) ⇒ -1, ...
Compares
self
withother_currency
against the value ofpriority
attribute. -
#==(other_currency) ⇒ Boolean
Compares
self
withother_currency
and returnstrue
if the are the same or if theirid
attributes match. -
#code ⇒ String
Returns currency symbol or iso code for currencies with no symbol.
-
#decimal_places ⇒ Integer
The number of decimal places needed.
- #eql? ⇒ Object
-
#exponent ⇒ Float
Returns the number of digits after the decimal separator.
-
#hash ⇒ Fixnum
Returns a Fixnum hash value based on the
id
attribute in order to use functions like & (intersection), group_by, etc. -
#initialize(id) ⇒ Money::Currency
constructor
Create a new
Currency
object. -
#inspect ⇒ String
Returns a human readable representation.
- #symbol_first? ⇒ Boolean
-
#to_currency ⇒ self
Conversation to
self
. -
#to_s ⇒ String
Returns a string representation corresponding to the upcase
id
attribute. -
#to_str ⇒ String
Returns a string representation corresponding to the upcase
id
attribute. -
#to_sym ⇒ Symbol
Returns a symbol representation corresponding to the upcase
id
attribute.
Methods included from Loader
Methods included from Heuristics
Methods included from Formatting
default_formatting_rules, define_i18n_method, format, included, localize_formatting_rules, regexp_format, symbol_position_from, symbol_value_from, translate_formatting_rules
Constructor Details
#initialize(id) ⇒ Money::Currency
Create a new Currency
object.
258 259 260 261 262 263 264 265 |
# File 'lib/redmineup/currency.rb', line 258 def initialize(id) id = id.to_s.downcase unless self.class.stringified_keys.include?(id) raise UnknownCurrency, "Unknown currency '#{id}'" end @id = id.to_sym initialize_data! end |
Instance Attribute Details
#decimal_mark ⇒ String (readonly) Also known as: separator
Returns The decimal mark, or character used to separate the whole unit from the subunit.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination |
#disambiguate_symbol ⇒ String (readonly)
Returns Alternative currency used if symbol is ambiguous.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination |
#html_entity ⇒ String (readonly)
Returns The html entity for the currency symbol.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination |
#id ⇒ Symbol (readonly)
Returns The symbol used to identify the currency, usually THE lowercase iso_code
attribute.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 def id @id end |
#iso_code ⇒ String (readonly)
Returns The international 3-letter code as defined by the ISO 4217 standard.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination |
#iso_numeric ⇒ String (readonly)
Returns The international 3-numeric code as defined by the ISO 4217 standard.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination |
#name ⇒ String (readonly)
Returns The currency name.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination |
#priority ⇒ Integer (readonly)
Returns A numerical value you can use to sort/group the currency list.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination |
#smallest_denomination ⇒ Object (readonly)
Returns the value of attribute smallest_denomination.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination |
#subunit ⇒ String (readonly)
Returns The name of the fractional monetary unit.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination |
#subunit_to_unit ⇒ Integer (readonly)
Returns The proportion between the unit and the subunit.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination |
#symbol ⇒ String (readonly)
Returns The currency symbol (UTF-8 encoded).
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination |
#symbol_first ⇒ Boolean (readonly)
Returns Should the currency symbol precede the amount, or should it come after?.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination |
#The ⇒ String (readonly)
Returns character used to separate thousands grouping of the whole unit.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination |
#thousands_separator ⇒ Object (readonly) Also known as: delimiter
Returns the value of attribute thousands_separator.
241 242 243 |
# File 'lib/redmineup/currency.rb', line 241 def thousands_separator @thousands_separator end |
Class Method Details
.add_admin_money_menu ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/redmineup/currency.rb', line 36 def return if Redmine::MenuManager.map(:admin_menu).exists?(:redmineup_money) require 'redmineup/hooks/views_layouts_hook' Redmine::MenuManager.map(:admin_menu).push(:redmineup_money, { controller: 'redmineup', action: 'settings', id: 'money' }, caption: :label_redmineup_money, html: { class: 'icon icon-redmineup-money' }) end |
.all ⇒ Array
List the currencies imported and registered
129 130 131 132 133 134 135 136 137 |
# File 'lib/redmineup/currency.rb', line 129 def all table.keys.map do |curr| c = Currency.new(curr) if c.priority.nil? raise MissingAttributeError.new(:all, c.id, :priority) end c end.sort_by(&:priority) end |
.decimal_places_cache ⇒ Object
Cache decimal places for subunit_to_unit values. Common ones pre-cached.
404 405 406 |
# File 'lib/redmineup/currency.rb', line 404 def self.decimal_places_cache @decimal_places_cache ||= {1 => 0, 10 => 1, 100 => 2, 1000 => 3} end |
.each ⇒ Object
193 194 195 |
# File 'lib/redmineup/currency.rb', line 193 def each all.each { |c| yield(c) } end |
.find(id) ⇒ Money::Currency
Lookup a currency with given id
an returns a Currency
instance on success, nil
otherwise.
retrieve the applicable attributes.
58 59 60 61 62 63 64 |
# File 'lib/redmineup/currency.rb', line 58 def find(id) return nil if id == "" id = id.to_s.downcase.to_sym new(id) rescue UnknownCurrency nil end |
.find_by_iso_numeric(num) ⇒ Money::Currency
Lookup a currency with given num
as an ISO 4217 numeric and returns an Currency
instance on success, nil
otherwise.
the right currency id.
77 78 79 80 81 82 83 |
# File 'lib/redmineup/currency.rb', line 77 def find_by_iso_numeric(num) num = num.to_s id, _ = self.table.find{|key, currency| currency[:iso_numeric] == num} new(id) rescue UnknownCurrency nil end |
.register(curr) ⇒ Object
Register a new currency
166 167 168 169 170 171 |
# File 'lib/redmineup/currency.rb', line 166 def register(curr) key = curr.fetch(:iso_code).downcase.to_sym table if !@table @table[key] = curr @stringified_keys = stringify_keys end |
.stringified_keys ⇒ Set
We need a string-based validator before creating an unbounded number of symbols. www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#11 github.com/RubyMoney/money/issues/132
145 146 147 |
# File 'lib/redmineup/currency.rb', line 145 def stringified_keys @stringified_keys ||= stringify_keys end |
.table ⇒ Object
List of known currencies.
monetary unit
The standard unit of value of a currency, as the dollar in the United States or the peso in Mexico. www.answers.com/topic/monetary-unit
fractional monetary unit, subunit
A monetary unit that is valued at a fraction (usually one hundredth) of the basic monetary unit www.answers.com/topic/fractional-monetary-unit-subunit
See en.wikipedia.org/wiki/List_of_circulating_currencies and search.cpan.org/~tnguyen/Locale-Currency-Format-1.28/Format.pm
119 120 121 |
# File 'lib/redmineup/currency.rb', line 119 def table @table ||= load_currencies end |
.unregister(curr) ⇒ Boolean
Unregister a currency.
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/redmineup/currency.rb', line 181 def unregister(curr) if curr.is_a?(Hash) key = curr.fetch(:iso_code).to_s.downcase.to_sym else key = curr.to_s.downcase.to_sym end existed = @table.delete(key) @stringified_keys = stringify_keys existed ? true : false end |
Instance Method Details
#<=>(other_currency) ⇒ -1, ...
Compares self
with other_currency
against the value of priority
attribute.
280 281 282 283 284 285 286 287 288 289 |
# File 'lib/redmineup/currency.rb', line 280 def <=>(other_currency) # <=> returns nil when one of the values is nil comparison = (self.priority <=> other_currency.priority || 0) rescue 0 if comparison == 0 self.id.to_s <=> other_currency.id.to_s else comparison end end |
#==(other_currency) ⇒ Boolean
Compares self
with other_currency
and returns true
if the are the same or if their id
attributes match.
303 304 305 |
# File 'lib/redmineup/currency.rb', line 303 def ==(other_currency) self.equal?(other_currency) || compare_ids(other_currency) end |
#code ⇒ String
Returns currency symbol or iso code for currencies with no symbol.
388 389 390 |
# File 'lib/redmineup/currency.rb', line 388 def code symbol || iso_code end |
#decimal_places ⇒ Integer
The number of decimal places needed.
411 412 413 |
# File 'lib/redmineup/currency.rb', line 411 def decimal_places cache[subunit_to_unit] ||= calculate_decimal_places(subunit_to_unit) end |
#eql? ⇒ Object
247 |
# File 'lib/redmineup/currency.rb', line 247 alias_method :eql?, :== |
#exponent ⇒ Float
Returns the number of digits after the decimal separator.
399 400 401 |
# File 'lib/redmineup/currency.rb', line 399 def exponent Math.log10(@subunit_to_unit) if @subunit_to_unit end |
#hash ⇒ Fixnum
Returns a Fixnum hash value based on the id
attribute in order to use functions like & (intersection), group_by, etc.
325 326 327 |
# File 'lib/redmineup/currency.rb', line 325 def hash id.hash end |
#inspect ⇒ String
Returns a human readable representation.
335 336 337 |
# File 'lib/redmineup/currency.rb', line 335 def inspect "#<#{self.class.name} id: #{id}, priority: #{priority}, symbol_first: #{symbol_first}, thousands_separator: #{thousands_separator}, html_entity: #{html_entity}, decimal_mark: #{decimal_mark}, name: #{name}, symbol: #{symbol}, subunit_to_unit: #{subunit_to_unit}, exponent: #{exponent}, iso_code: #{iso_code}, iso_numeric: #{iso_numeric}, subunit: #{subunit}, smallest_denomination: #{smallest_denomination}>" end |
#symbol_first? ⇒ Boolean
392 393 394 |
# File 'lib/redmineup/currency.rb', line 392 def symbol_first? !!@symbol_first end |
#to_currency ⇒ self
Conversation to self
.
381 382 383 |
# File 'lib/redmineup/currency.rb', line 381 def to_currency self end |
#to_s ⇒ String
Returns a string representation corresponding to the upcase id
attribute.
– DEV: id.to_s.upcase corresponds to iso_code but don’t use ISO_CODE for consistency.
350 351 352 |
# File 'lib/redmineup/currency.rb', line 350 def to_s id.to_s.upcase end |
#to_str ⇒ String
Returns a string representation corresponding to the upcase id
attribute. Useful in cases where only implicit conversions are made.
362 363 364 |
# File 'lib/redmineup/currency.rb', line 362 def to_str id.to_s.upcase end |
#to_sym ⇒ Symbol
Returns a symbol representation corresponding to the upcase id
attribute.
374 375 376 |
# File 'lib/redmineup/currency.rb', line 374 def to_sym id.to_s.upcase.to_sym end |