Skip to content

Add custom currency support via custom_currency_path#515

Merged
csgavino merged 1 commit intomainfrom
support-custom-currency
Mar 4, 2026
Merged

Add custom currency support via custom_currency_path#515
csgavino merged 1 commit intomainfrom
support-custom-currency

Conversation

@csgavino
Copy link
Contributor

Adds support for app-defined custom currencies (e.g. loyalty points) loaded from a user-provided YAML file. Follows the same pattern established in #401 for crypto currencies: config option gates the feature, Loader reads the file, data is frozen via deep_deduplicate!.

Changes

  • Added custom_currency_path config option (defaults to nil)
  • Added Loader.load_custom_currencies(path) using YAML.safe_load_file for security
  • Updated Currency and Currency::Loader to handle custom currencies as a third-tier fallback (ISO > crypto > custom)
  • Path-keyed caching (@@custom_currencies_cache[path]) for fiber safety
  • Added comprehensive test coverage for new functionality
  • Updated RBS type signatures

Breaking changes

None. The feature is opt-in and disabled by default, so existing code will continue to work without any changes.

Configuration System

Before

# No built-in custom currency support
Money.new(500, "CREDITS") # Raises UnknownCurrency

After

# Point to a YAML file with your custom currencies
Money.configure do |config|
  config.custom_currency_path = "/path/to/custom_currencies.yml"
end

Money.new(500, "CREDITS") # Works!

YAML format

credits:
  iso_code: "CREDITS"
  name: "Loyalty Points"
  symbol: "CR"
  disambiguate_symbol: "CR"
  subunit_to_unit: 1
  smallest_denomination: 1
  decimal_mark: "."

Design decisions

  • YAML.safe_load_file for user-provided paths (bundled YAML files still use YAML.load_file)
  • Custom currencies cannot shadow ISO or crypto currencies — lookup order is ISO > crypto > custom, so a custom "USD" entry is ignored
  • ArgumentError raised if the YAML file doesn't contain a Hash
  • Path-keyed cache instead of a single class variable, so different fibers with different custom_currency_path values don't share stale data

@csgavino csgavino marked this pull request as ready for review February 26, 2026 09:29
Copy link

@kajol-maheshwari kajol-maheshwari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add some documentation for configuration and the shape of custom currencies file

@csgavino
Copy link
Contributor Author

The PR description includes config examples and the YAML format — is there a specific place you'd like documentation added (README, inline comments, etc.)?

Copy link

@kajol-maheshwari kajol-maheshwari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@csgavino yes in the README, like we have for crypto currencies https://github.com/Shopify/money?tab=readme-ov-file#crypto-currencies

@csgavino csgavino force-pushed the support-custom-currency branch from 4c9a266 to c450535 Compare March 2, 2026 02:57
Allow apps to define custom currencies (e.g., loyalty points) by
pointing Money::Config to a YAML file. Lookup order: ISO → crypto →
custom, so custom currencies cannot shadow built-in ones.

Uses YAML.safe_load_file for user-provided paths and a path-keyed
cache to support per-Fiber config isolation.
@csgavino csgavino force-pushed the support-custom-currency branch from c450535 to a647f51 Compare March 3, 2026 00:38
@csgavino csgavino requested a review from robinbrandt March 3, 2026 00:42
@csgavino csgavino merged commit eb7e89b into main Mar 4, 2026
9 checks passed
@csgavino csgavino deleted the support-custom-currency branch March 4, 2026 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants