Class: RVGP::Journal::Currency
- Inherits:
-
Object
- Object
- RVGP::Journal::Currency
- Defined in:
- lib/rvgp/journal/currency.rb
Overview
This abstraction offers a repository by which currencies can be defined, and by which their rules can be queried. An instance of this class, is an entry in the currency table, that we support. The default currencies that RVGP supports can be found in: iso-4217-currencies.json , and this file is typically loaded during rvgp initialization.
Here’s what an entry in that file, looks like: “‘
{
"Entity":"UNITED STATES",
"Currency":"US Dollar",
"Alphabetic Code":"USD",
"Numeric Code":"840",
"Minor unit":"2",
"Symbol":"$"
},
“‘
PR’s welcome, if you have additions to offer on this file.
This class is used in several places throughout the codebase, and provides a standard set of interfaces for working with this global repository of currency properties.
Defined Under Namespace
Classes: Error
Class Attribute Summary collapse
-
.currencies_config ⇒ Object
Returns the value of attribute currencies_config.
Instance Attribute Summary collapse
-
#alphabetic_code ⇒ String
readonly
The shorthand, three digit letter code for this currency (ie ‘USD’).
-
#currency ⇒ String
readonly
A colloquial name for this currency (ie. “US Dollar”).
-
#entity ⇒ String
readonly
The name of the institution to which this currency belongs.
-
#minor_unit ⇒ Integer
readonly
The default precision for this currency, as would be typically implied.
-
#numeric_code ⇒ Integer
readonly
The ISO 4217 code for this currency 840.
-
#symbol ⇒ String
readonly
The shorthand, (typically) one character symbol code for this currency (ie ‘$’).
Class Method Summary collapse
-
.from_code_or_symbol(str) ⇒ RVGP::Journal::Currency
Load and return a parsed RVGP::Journal::Currency, out of the provided iso-4217-currencies.json file.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Currency
constructor
Create a Currency commodity, from constituent parts.
-
#to_commodity(quantity) ⇒ RVGP::Journal::Commodity
Create a new commodity, from this currency, with the provided quantity.
-
#valid? ⇒ TrueClass, FalseClass
Indicates whether or not this instance contains all required fields.
Constructor Details
#initialize(opts = {}) ⇒ Currency
Create a Currency commodity, from constituent parts
55 56 57 58 59 60 61 62 63 |
# File 'lib/rvgp/journal/currency.rb', line 55 def initialize(opts = {}) @entity = opts[:entity] @currency = opts[:currency] @alphabetic_code = opts[:alphabetic_code] @numeric_code = opts[:numeric_code].to_i @minor_unit = opts[:minor_unit].to_i @symbol = opts[:symbol] raise Error, format('Unabled to parse config entry: "%s"', inspect) unless valid? end |
Class Attribute Details
.currencies_config ⇒ Object
Returns the value of attribute currencies_config.
97 98 99 |
# File 'lib/rvgp/journal/currency.rb', line 97 def currencies_config @currencies_config end |
Instance Attribute Details
#alphabetic_code ⇒ String (readonly)
The shorthand, three digit letter code for this currency (ie ‘USD’)
41 42 43 |
# File 'lib/rvgp/journal/currency.rb', line 41 def alphabetic_code @alphabetic_code end |
#currency ⇒ String (readonly)
A colloquial name for this currency (ie. “US Dollar”)
41 42 43 |
# File 'lib/rvgp/journal/currency.rb', line 41 def currency @currency end |
#entity ⇒ String (readonly)
The name of the institution to which this currency belongs. For some reason (I believe this is part of the ISO-4217 standard) this string is capitalized (ie, ‘UNITED STATES’)
41 42 43 |
# File 'lib/rvgp/journal/currency.rb', line 41 def entity @entity end |
#minor_unit ⇒ Integer (readonly)
The default precision for this currency, as would be typically implied. For the case of USD, this would be 2. Indicating two decimal digits for a default transcription of a USD amount.
41 42 43 |
# File 'lib/rvgp/journal/currency.rb', line 41 def minor_unit @minor_unit end |
#numeric_code ⇒ Integer (readonly)
The ISO 4217 code for this currency 840
41 42 43 |
# File 'lib/rvgp/journal/currency.rb', line 41 def numeric_code @numeric_code end |
#symbol ⇒ String (readonly)
The shorthand, (typically) one character symbol code for this currency (ie ‘$’)
41 42 43 |
# File 'lib/rvgp/journal/currency.rb', line 41 def symbol @symbol end |
Class Method Details
.from_code_or_symbol(str) ⇒ RVGP::Journal::Currency
Load and return a parsed RVGP::Journal::Currency, out of the provided iso-4217-currencies.json file.
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rvgp/journal/currency.rb', line 83 def self.from_code_or_symbol(str) @currencies ||= begin unless currencies_config && File.readable?(currencies_config) raise StandardError, 'Missing currency config file' end JSON.parse(File.read(currencies_config)).map do |c| new(c.transform_keys { |k| k.downcase.tr(' ', '_').to_sym }) end end @currencies.find { |c| (c.alphabetic_code && c.alphabetic_code == str) || (c.symbol && c.symbol == str) } end |
Instance Method Details
#to_commodity(quantity) ⇒ RVGP::Journal::Commodity
Create a new commodity, from this currency, with the provided quantity
74 75 76 |
# File 'lib/rvgp/journal/currency.rb', line 74 def to_commodity(quantity) RVGP::Journal::Commodity.new symbol || alphabetic_code, alphabetic_code, quantity, minor_unit end |
#valid? ⇒ TrueClass, FalseClass
Indicates whether or not this instance contains all required fields
67 68 69 |
# File 'lib/rvgp/journal/currency.rb', line 67 def valid? [entity, currency, alphabetic_code, numeric_code, minor_unit].all? end |