Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying a delimiter pattern #981

Merged
merged 4 commits into from
Mar 11, 2021

Conversation

ferranpm
Copy link
Contributor

To add an option to put the thousands separators according to a regex.
Use the ones previously defined in Formatter#regexp_format if not specified
Move the logic to fetch this regex to FormatterRules

Both the name and the path for the option are taken from activesupport

Copy link
Member

@semmons99 semmons99 left a comment

Choose a reason for hiding this comment

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

please update the changelog and documentation to share this new option, then we will merge it. thank you

@ferranpm
Copy link
Contributor Author

Is it ok like this? I'm not sure I've done it right.

Thanks

Copy link
Member

@semmons99 semmons99 left a comment

Choose a reason for hiding this comment

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

small change needed to version, otherwise, excellent

CHANGELOG.md Outdated
@@ -1,5 +1,9 @@
# Changelog

## 6.14.2
Copy link
Member

Choose a reason for hiding this comment

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

you will want to bump to 6.15.0 since this is an additive change and not a bug fix.

@@ -1,3 +1,3 @@
class Money
VERSION = '6.14.1'
VERSION = '6.14.2'
Copy link
Member

Choose a reason for hiding this comment

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

6.15.0 please

@ferranpm
Copy link
Contributor Author

Fixed

Copy link
Member

@semmons99 semmons99 left a comment

Choose a reason for hiding this comment

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

🤘awesome. thank you

@semmons99 semmons99 merged commit 4ba42d1 into RubyMoney:main Mar 11, 2021
@ferranpm ferranpm deleted the add-delimiter_pattern-support branch March 11, 2021 19:48
# from http://blog.revathskumar.com/2014/11/regex-comma-seperated-indian-currency-format.html
/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/
else
I18n.t('number.currency.format.delimiter_pattern', default: nil) || /(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/
Copy link
Contributor

Choose a reason for hiding this comment

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

@ferranpm one note here — I18n does not have a default number.currency.format.delimiter_pattern key (have a look at the definitions Rails is using in https://github.com/svenfuchs/rails-i18n) and these lookups are actually not cheap. I think we should to remove it and just fallback to a RegExp straight away

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a shame because it integrates well with the activesupport key but I understand your concern. I've created this PR #985 in order to remove it.

Nonetheless, could we consider adding a global configuration for formatting then?
Maybe a faster way would be to allow configuring this when setting up the gem

Money.formatting = {
  fr: { decimal_mark: ',', delimiter_pattern: ... }
  ...
}

And then merging those options with the ones provided to the #format method?

Copy link
Contributor

Choose a reason for hiding this comment

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

The only time when the I18n lookup is expensive is when you don't have that key. Otherwise it gets cached and loaded from memory.

What you are suggesting does make sense. I wonder how many exception cases would we see there. Are there a lot of currencies you expect delimiter_pattern to be useful for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've only encountered one case for a weird delimiter_pattern.
But the idea is not only for this option but for all the formatting options.
It will make it possible to save users to pass custom options every time they need to format a Money object

Copy link
Contributor

Choose a reason for hiding this comment

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

Right I see. So this is more like an override for formatting options. We already do look up I18n's formatting setup, but being able to override it does sound like a useful feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants