Skip to content

API Reference

Full YARD-generated API documentation is available at api/WhittakerTech/Midas.md — generated by bundle exec yard doc --format markdown.

To browse it locally:

bundle exec yard server
# => http://localhost:8808

To regenerate after source changes:

bundle exec yard doc

WhittakerTech::Midas (module)

Constants

Constant Value
ROUNDING_POLICIES { round:, ceil:, floor:, bankers: } — lambdas
DEFAULT_ROUNDING_POLICY :round

Module methods

Method Returns Notes
table_name(name) String Resolves fully-qualified table name
table_namespace= Sets PostgreSQL schema namespace
currency_directions Hash Per-currency direction map (:ltr / :rtl)
currency_direction_for(code) Symbol Returns :ltr or :rtl for a currency code

Coin

Class methods

Method Returns Notes
Coin.value(currency_minor, currency_code) Coin Lowest-level factory (requires Integer)
Coin.zero(currency_code) Coin Zero-valued Coin
Coin.parse(value, currency_code: nil) Coin Coerces Money / Numeric / String

Instance methods — accessors

Method Returns Notes
amount Money Projection (memoized). Not the source of truth.
amount=(value) Accepts Money or Numeric (minor units)
currency_code String ISO 4217 code (3 letters, uppercase)
currency_minor Integer Raw minor units (cents, pence, etc.)
minor Integer Alias for currency_minor
fractional Integer Alias for currency_minor
currency String Alias for currency_code
decimals Integer Decimal places defined by the currency
scale Integer 10 ** decimals
major BigDecimal Major-unit value. Not payable money.

Instance methods — behaviour

Method Returns Notes
format(to: nil) String Delegates to Money#format
present(pattern, **opts) String Token-based formatting; see Presenter
allocate(per:, rounding_policy: :round) Allocation Returns per-unit value object

Coin::Arithmetic

Mixed into Coin. All operations return new frozen Coins.

Method Returns Raises
==(other) Boolean
eql?(other) Boolean
hash Integer
+(other) Coin TypeError, ArgumentError (currency mismatch)
-(other) Coin TypeError, ArgumentError
*(integer) Coin TypeError (non-Integer)
%(integer) Coin TypeError, ZeroDivisionError
negate Coin
-@ Coin
divide(divisor, rounding_policy: :round) Coin TypeError, ZeroDivisionError
divide_round(divisor) Coin Generated shorthand
divide_ceil(divisor) Coin Generated shorthand
divide_floor(divisor) Coin Generated shorthand
divide_bankers(divisor) Coin Generated shorthand

Coin::Allocation

Method Returns Notes
coin Coin The source Coin (frozen)
divisor / per Numeric Number of units
rounding_policy Symbol e.g. :round, :ceil
value Coin Per-unit Coin
price(qty: 1) Coin Total for N units
currency_code String Delegated from coin
decimals Integer Delegated from coin
scale Integer Delegated from coin
zero? Boolean Delegated from coin
positive? Boolean Delegated from coin
negative? Boolean Delegated from coin

Coin::Presenter

TOKEN_MAP

{ '%' => :token_percent, 't' => :token_total, 'm' => :token_minor,
  'M' => :token_major,   'c' => :token_currency_code,
  's' => :token_currency_symbol, 'n' => :token_number_only,
  'u' => :token_units,   'p' => :token_per_exact, '~' => :token_approx }

Class methods

Method Returns Notes
format(coin, pattern, **) String Main entry point
build_context(coin, **opts) Context Assembles rendering struct
scan(pattern, ctx) String Tokenises and expands pattern

Options for present / format

Option Type Default Token
approx: Boolean false %~
units: String nil (renders empty) %u
per_exact: String nil (renders empty) %p
currency_dir: Symbol From Midas.currency_direction_for all

Coin::Parser

Method Returns Raises
parse(value, currency_code: nil) Coin TypeError, ArgumentError

Input coercion rules:

Input Behaviour
Coin Returned unchanged
Money Wraps money.cents + money.currency.iso_code
Numeric Major units; scaled to minor. Requires currency_code.
String Strips non-numeric chars. Requires currency_code or embedded symbol.

Coin::Bidi

Method / Constant Notes
LRI = "\u2066" Left-to-Right Isolate mark
RLI = "\u2067" Right-to-Left Isolate mark
PDI = "\u2069" Pop Directional Isolate mark (closes span)
bidi_isolate(text, dir:) Wraps text; dir: is :ltr, :rtl, or nil
bidi_isolate_number(text) Shorthand for LTR isolation of numeric strings
bidi_currency_dir(currency_code) Reads from Midas.currency_directions

Bankable

Class methods (in ActiveRecord model)

Method Notes
has_coin(name, dependent: :destroy) Declares a single monetary attribute
has_coins(*names, dependent: :destroy) Declares multiple at once

Generated instance methods (for has_coin :price)

Method Returns Notes
price Coin? Returns the persisted Coin or nil
price_coin Coin? The has_one association
price_amount Money? coin.amount or nil
price_format String? Formatted string or nil
set_price(amount:, currency_code:) Coin Creates or updates the Coin
midas_coins Relation All coins on this resource

set_* amount coercion

Input type Treated as
Money Minor units from money.cents
Integer Already minor units
Float / Numeric Major units × 10 ** decimals_for(currency)

FormHelper

Method Returns Notes
midas_currency_field(form, attribute, currency_code:, **opts) String (HTML) Renders display + hidden fields

Options:

Option Type Notes
currency_code: String Required. ISO 4217 code.
label: String Field label text
decimals: Integer Decimal places override (default: 2)
input_html: Hash Passed to the visible display input
wrapper_html: Hash Passed to the wrapper <div>