Skip to content

Commit

Permalink
Define =~ method on Timezone identifier
Browse files Browse the repository at this point in the history
It's useful to match against a timezones identifier using regex.
e.g. this rails issue: rails/rails#35973 this
commit introduces the =~ operator to the Timezone class so that clients
can match against its identifier by suppliying a regex.
  • Loading branch information
itsWill committed Apr 26, 2019
1 parent a1b28df commit be116b2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/tzinfo/timezone.rb
Expand Up @@ -1119,6 +1119,14 @@ def hash
identifier.hash
end

# @param regex [Regex] a `Regex` to match against the identifier of this
# {Timezone}.
# @return [Integer, nil] `Integer` offset of the match on the identifier,
# otherwise `nil` if there is no match.
def =~(regex)
regex =~ identifier
end

# Returns a serialized representation of this {Timezone}. This method is
# called when using `Marshal.dump` with an instance of {Timezone}.
#
Expand Down
6 changes: 6 additions & 0 deletions test/tc_timezone.rb
Expand Up @@ -1700,6 +1700,12 @@ def test_hash
assert_equal('America/New_York'.hash, TestTimezone.new('America/New_York').hash)
end

define_method("test_=~_operator_matches_against_identifier") do
tz = TestTimezone.new('Europe/London')
assert_equal(0, tz =~ /Europe\/London/)
assert_nil(tz =~ /America\/NewYork/)
end

def test_marshal_data
tz = Timezone.get('Europe/London')
marshalled_tz = Marshal.load(Marshal.dump(tz))
Expand Down

0 comments on commit be116b2

Please sign in to comment.