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

Separate core_ext #88

Merged
merged 11 commits into from
May 10, 2024
Merged

Separate core_ext #88

merged 11 commits into from
May 10, 2024

Conversation

ColinDKelley
Copy link
Contributor

@ColinDKelley ColinDKelley commented May 7, 2024

NOTE
Best reviewed by skipping the first commit that moved humanize.rb to humanize/module.rb and ignoring whitespace: https://github.com/radar/humanize/pull/88/files/a84b371db91fe64c184215df1b147f7a70c8912d..500e185faeb990f4e801d07ec6ac20643249f540

Changes

The Humanize gem would formerly always duck punch itself into the Integer, Float, and BigDecimal classes.

This PR takes inspiration from how active_support does core extensions. The core extensions are now optional with this require:

require 'humanize/core_ext'

And that is required automatically if you

require 'humanize'

to match previous behavior. But new code has the option to set require: false in the Gemfile and just do

require 'humanize/module'

Then rather than doing

42.humanize(...)

it can instead use the pure functional form (which is always available):

Humanize.format(42, ...)

This is useful for code bases that want don't want to duck punch into Ruby core classes.

@@ -117,15 +118,3 @@ def initialize
end
end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The big change is moving this out to core_ext.rb.


module Humanize
SPACE = ' '.freeze
EMPTY = ''.freeze
Copy link
Contributor Author

@ColinDKelley ColinDKelley May 7, 2024

Choose a reason for hiding this comment

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

No need for this trick since # frozen_string_literal: true above. Other string literals benefit as well.

# Big numbers are big: http://wiki.answers.com/Q/What_number_is_after_vigintillion&src=ansTT

def humanize(locale: Humanize.config.default_locale,
class << self
def format(number,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that this changed from humanize with (where self was mixed in as the implicit number) to format with an explicit number arg.

Humanize.config.default_locale == locale
end

def process_decimals(number, locale_class, locale, parts, decimals_as, spacer)
Copy link
Contributor Author

@ColinDKelley ColinDKelley May 7, 2024

Choose a reason for hiding this comment

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

Note that this previously polluted the mixin namespace (it became a public method of Integer, Float, and BigDecimal). Now it's pushed down into the module...where it has an explicit number arg.

@@ -0,0 +1,20 @@
module Humanize
def humanize(locale: Humanize.config.default_locale,
decimals_as: Humanize.config.decimals_as)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the glue code that converts from implicit self arg to explicit self passed as the number arg.

@radar
Copy link
Owner

radar commented May 7, 2024

Hey, thanks for this! I think it's a great PR.

Would you mind looking into the build failures here?

when :tr
[Humanize::Tr, ' ']
when :jp
[Humanize::Jp, '']
Copy link
Owner

Choose a reason for hiding this comment

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

This is missing the recent zh-TW addition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@radar Oops, I didn't mean to make any changes to this locale list. I just looked over in master and didn't find any reference to zh. Can you point me to it?

And I'm looking at the build failures.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The build failures were rubocop suggestions. All addressed here: 0abcfc9

Copy link
Owner

Choose a reason for hiding this comment

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

@radar Oops, I didn't mean to make any changes to this locale list. I just looked over in master and didn't find any reference to zh. Can you point me to it?

And I'm looking at the build failures.

Getting ahead of myself. It's in #87.

Copy link
Owner

Choose a reason for hiding this comment

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

Ok, merged #87. So can we now get that in here? I think it's just the two lines for the when + the config.

Copy link
Contributor Author

@ColinDKelley ColinDKelley May 8, 2024

Choose a reason for hiding this comment

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

Ok, I've merged latest master containing that PR into this branch.

Copy link
Owner

@radar radar left a comment

Choose a reason for hiding this comment

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

Please restore zh-TW support in for_locale.

@ColinDKelley
Copy link
Contributor Author

@radar BTW I think the # frozen_string_literal: true magic comment can help across the board here. I won't clutter this PR with that, but once this is merged, I'll submit another PR that includes that magic comment and then drops .freeze after all the string constants.

@radar
Copy link
Owner

radar commented May 9, 2024

Fine by me regarding FSL.

Rubocop is being a grump. Would you mind fixing that up? I can't push to your branch due to permission restrictions.

@ColinDKelley
Copy link
Contributor Author

Rubocop is being a grump. Would you mind fixing that up? I can't push to your branch due to permission restrictions.

Ah, I'd made this change suppress that rubucop failure but lost it in the merge-up. Merged back here: a347bbb

Also, I found I'd missed this recursive call that was depending on the core Integer class being extended. I've switched it to functional (which will always work): 9339246

And finally, I updated the README to document the new functional usage: 32bd066

@radar radar merged commit 9a5846d into radar:master May 10, 2024
3 checks passed
@radar
Copy link
Owner

radar commented May 10, 2024

All checks out to me. Thanks very much! ❤️

@ColinDKelley ColinDKelley deleted the separate-core_ext branch May 10, 2024 23:27
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.

2 participants