Module: WhittakerTech::Midas

Defined in:
lib/whittaker_tech/midas.rb,
lib/whittaker_tech/midas/version.rb

Overview

WhittakerTech::Midas is a Rails engine for multi-currency monetary value management. It replaces scattered *_cents and *_currency columns with a single polymorphic Coin model backed by a centralized midas_coins table.

Configuration

Table namespace (PostgreSQL schema)

By default all coins are stored in midas_coins. To place the table inside a PostgreSQL schema set table_namespace in an initializer:

WhittakerTech::Midas.table_namespace = 'finance'
# => table becomes finance.coins

Bidirectional text

Currency display direction defaults to LTR for every currency code. Override individual currencies as needed:

WhittakerTech::Midas.currency_directions['ILS'] = :rtl
WhittakerTech::Midas.currency_directions['AED'] = :rtl

See also:

  • Coin
  • Coin::Arithmetic
  • Bankable

Since:

  • 0.1.0

Defined Under Namespace

Modules: ApplicationHelper, Bankable, Deprecation, FormHelper, Paramable Classes: ApplicationController, ApplicationJob, ApplicationMailer, ApplicationRecord, Coin, Engine, Exchange

Constant Summary collapse

VERSION =
'0.4.0'.freeze
ROUNDING_POLICIES =

Rounding strategies available for division operations.

Each value is a lambda that accepts a Float and returns a rounded value.

Rounding keys:

  • :round: Round half-up (standard commercial rounding)
  • :ceil: Always round up (ceiling)
  • :floor: Always round down (floor / truncate)
  • :bankers: Round half-to-even (banker's rounding)

Returns:

  • (Hash{Symbol => Proc})

Since:

  • 0.1.0

{
  round: ->(v) { v.round },
  ceil: ->(v) { v.ceil },
  floor: ->(v) { v.floor },
  bankers: ->(v) { v.round(0, half: :even) }
}.freeze
DEFAULT_ROUNDING_POLICY =

The rounding policy applied when no explicit policy is supplied.

Returns:

  • (Symbol)

Since:

  • 0.1.0

:round

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.currency_direction_for(currency_code) ⇒ Symbol

Returns the configured display direction for the given currency code.

Falls back to :ltr for any currency not explicitly configured.

Parameters:

  • currency_code (String)

    ISO 4217 currency code, e.g. "USD"

Returns:

  • (Symbol)

    :ltr or :rtl

Since:

  • 0.1.0



113
114
115
# File 'lib/whittaker_tech/midas.rb', line 113

def self.currency_direction_for(currency_code)
  currency_directions[currency_code.to_s.upcase]
end

.currency_directionsHash{String => Symbol}

Per-currency display direction map. Defaults all currencies to :ltr.

Modify to configure RTL currencies in your initializer:

WhittakerTech::Midas.currency_directions['ILS'] = :rtl

Returns:

  • (Hash{String => Symbol})

    maps uppercased ISO currency code to :ltr or :rtl

Since:

  • 0.1.0



103
104
105
# File 'lib/whittaker_tech/midas.rb', line 103

def self.currency_directions
  @currency_directions ||= Hash.new(:ltr)
end

.reset_configuration!void

This method returns an undefined value.

Resets all mutable configuration to defaults.

Intended for use in test suite after blocks when specs mutate engine-level configuration.

Since:

  • 0.1.0



123
124
125
126
127
# File 'lib/whittaker_tech/midas.rb', line 123

def self.reset_configuration!
  self.table_namespace      = nil
  self.deprecation_behavior = :warn
  @currency_directions      = nil
end

.table_name(name) ⇒ String

Resolves the fully-qualified table name for a given base name.

Parameters:

  • name (String)

    base table name, e.g. "coins"

Returns:

  • (String)

    the namespaced table name

Raises:

  • (RuntimeError)

    if table_namespace is set and the adapter is not PostgreSQL

Since:

  • 0.1.0



87
88
89
90
91
92
93
94
# File 'lib/whittaker_tech/midas.rb', line 87

def self.table_name(name)
  return "midas_#{name}" if table_namespace.blank?

  adapter = ActiveRecord::Base.connection.adapter_name
  raise "WhittakerTech::Midas.table_namespace requires PostgreSQL (got #{adapter})" unless adapter == 'PostgreSQL'

  "#{table_namespace}.#{name}"
end

Instance Method Details

#deprecation_behaviorSymbol

Controls how deprecation notices are emitted.

Allowed values:

  • :warn: Prints to STDERR via Kernel.warn (default)
  • :raise: Raises Deprecation::DeprecationError
  • :silence: Suppresses all notices

Returns:

  • (Symbol)

Since:

  • 0.1.0



80
# File 'lib/whittaker_tech/midas.rb', line 80

mattr_accessor :deprecation_behavior, default: :warn

#table_namespaceString?

Optional PostgreSQL schema name used to namespace the coins table.

When nil (default) the table is created as midas_coins. When set, the table becomes <namespace>.coins and requires a PostgreSQL adapter.

Returns:

  • (String, nil)

Since:

  • 0.1.0



69
# File 'lib/whittaker_tech/midas.rb', line 69

mattr_accessor :table_namespace, default: nil