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:
CoinCoin::ArithmeticBankable
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)
{ 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.
:round
Class Method Summary collapse
-
.currency_direction_for(currency_code) ⇒ Symbol
Returns the configured display direction for the given currency code.
-
.currency_directions ⇒ Hash{String => Symbol}
Per-currency display direction map.
-
.reset_configuration! ⇒ void
Resets all mutable configuration to defaults.
-
.table_name(name) ⇒ String
Resolves the fully-qualified table name for a given base name.
Instance Method Summary collapse
-
#deprecation_behavior ⇒ Symbol
Controls how deprecation notices are emitted.
-
#table_namespace ⇒ String?
Optional PostgreSQL schema name used to namespace the coins table.
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.
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_directions ⇒ Hash{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
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.
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.
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_behavior ⇒ Symbol
Controls how deprecation notices are emitted.
Allowed values:
:warn: Prints to STDERR viaKernel.warn(default):raise: RaisesDeprecation::DeprecationError:silence: Suppresses all notices
80 |
# File 'lib/whittaker_tech/midas.rb', line 80 mattr_accessor :deprecation_behavior, default: :warn |
#table_namespace ⇒ String?
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.
69 |
# File 'lib/whittaker_tech/midas.rb', line 69 mattr_accessor :table_namespace, default: nil |