Class: RVGP::Journal::Currency

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Currency

Create a Currency commodity, from constituent parts

Parameters:

  • opts (Hash) (defaults to: {})

    The parts of this complex commodity

Options Hash (opts):

Raises:



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_configObject

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_codeString (readonly)

The shorthand, three digit letter code for this currency (ie ‘USD’)

Returns:

  • (String)

    the current value of alphabetic_code



41
42
43
# File 'lib/rvgp/journal/currency.rb', line 41

def alphabetic_code
  @alphabetic_code
end

#currencyString (readonly)

A colloquial name for this currency (ie. “US Dollar”)

Returns:

  • (String)

    the current value of currency



41
42
43
# File 'lib/rvgp/journal/currency.rb', line 41

def currency
  @currency
end

#entityString (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’)

Returns:

  • (String)

    the current value of entity



41
42
43
# File 'lib/rvgp/journal/currency.rb', line 41

def entity
  @entity
end

#minor_unitInteger (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.

Returns:

  • (Integer)

    the current value of minor_unit



41
42
43
# File 'lib/rvgp/journal/currency.rb', line 41

def minor_unit
  @minor_unit
end

#numeric_codeInteger (readonly)

The ISO 4217 code for this currency 840

Returns:

  • (Integer)

    the current value of numeric_code



41
42
43
# File 'lib/rvgp/journal/currency.rb', line 41

def numeric_code
  @numeric_code
end

#symbolString (readonly)

The shorthand, (typically) one character symbol code for this currency (ie ‘$’)

Returns:

  • (String)

    the current value of symbol



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.

Parameters:

  • str (String)

    Either a three digit :alphabetic_code, or a single digit :symbol

Returns:



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

Parameters:

  • quantity (Integer)

    The quantity component, of the newly created commodity

Returns:



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

Returns:

  • (TrueClass, FalseClass)

    whether or not we’re valid



67
68
69
# File 'lib/rvgp/journal/currency.rb', line 67

def valid?
  [entity, currency, alphabetic_code, numeric_code, minor_unit].all?
end