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

Make ActiveSupport::TimeZone.all independent of previous lookups #31176

Merged
merged 5 commits into from Nov 22, 2017

Conversation

cjlarose
Copy link
Contributor

@cjlarose cjlarose commented Nov 17, 2017

Summary

If you call ActiveSupport::TimeZone.all before you look up any ActiveSupport::TimeZones by name, then all calls to ActiveSupport::TimeZone.all for the lifetime of the program will return a collection of ActiveSupport::TimeZones corresponding to the 151 "meaningful" time zone identifiers described by ActiveSupport::TimeZone::MAPPING.

If, however, you look up a time zone by a name that is not a key of ActiveSupport::TimeZone::MAPPING before making a call to ActiveSupport::TimeZone.all, then the collection returned from all calls to that method will contain the time zone you looked up.

Abridged Timeline

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

sgrif
sgrif previously requested changes Nov 17, 2017
@@ -8,6 +8,17 @@
class TimeZoneTest < ActiveSupport::TestCase
include TimeZoneTestHelpers

def setup
ActiveSupport::TimeZone.instance_variable_set(:@lazy_zones_map, Concurrent::Map.new)
Copy link
Contributor

Choose a reason for hiding this comment

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

This setup is overly coupled to the internal details of the implementation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you have any suggestions for an alternative way to do this? The test I wrote depends on the caches being cleared before making the first call to ActiveSupport::TimeZone.all.

MAPPING.each_key { |place| self[place] } # load all the zones
@lazy_zones_map
end
@zones_map ||= Hash[MAPPING.keys.map { |place| [place, self[place]] }]
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be written as:

@zones_map ||= MAPPING.transform_values { |tz| self[tz] }

Copy link
Contributor Author

@cjlarose cjlarose Nov 20, 2017

Choose a reason for hiding this comment

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

Not quite. It seems important to use the keys of MAPPING to perform lookups using #[] because the argument to #[] ends up on the name attribute of the returned ActiveSupport::TimeZone instance.

> Hash[MAPPING.keys.map { |k| [k, self[k]] }]['UTC'].name
=> "UTC"
> MAPPING.transform_values { |v| self[v] }['UTC'].name
=> "Etc/UTC"

If there's a nice way to use Hash#transform_values in a way that I have access to the key, it would work, but I don't think that exists.

> MAPPING.transform_values { |_, k| self[k] }['UTC'].name
=> "UTC"

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry about that - you can use each_with_object to get both the key and the value, e.g.

@zones_map ||= MAPPING.each_with_object({}) { |(name, _), zones| zones[name] = self[name] }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. 69b96af

@@ -8,6 +8,12 @@
class TimeZoneTest < ActiveSupport::TestCase
include TimeZoneTestHelpers

def setup
# Clear memoized time zone data on TimeZone class
ActiveSupport.send(:remove_const, :TimeZone) if ActiveSupport.const_defined?(:TimeZone)
Copy link
Contributor

Choose a reason for hiding this comment

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

Still not happy with this - I'd rather we add a nodoc public clear method on AS::TimeZone to explicitly clear the cache and then use that in the test.

@pixeltrix pixeltrix dismissed sgrif’s stale review November 22, 2017 17:10

Requested changes were made

@pixeltrix pixeltrix merged commit 078421b into rails:master Nov 22, 2017
@cjlarose
Copy link
Contributor Author

Thanks @pixeltrix! Could you please also close #7245?

kamipo added a commit that referenced this pull request Nov 27, 2017
The def with blank `()` was newly added in #31176, but we have not used
the blank `()` style in most part of our code base.
So I've enabled `Style/DefWithParentheses` to prevent to newly added the
code.
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.

None yet

4 participants