Skip to content

Commit f192967

Browse files
committed
Refactor Linter::Release
Use regular expression for post URL only once, introduce some methods.
1 parent c7da39d commit f192967

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

lib/linter/release.rb

+29-5
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@ def initialize(data)
2020
# file: en/news/_posts/2019-12-25-ruby-2-7-0-released.md
2121
#
2222
def post_filename
23-
%r{\A/en/news/(?<yyyy>\d{4})/(?<mm>\d\d)/(?<dd>\d\d)/(?<name>[^/]*)/\Z} =~ post
24-
25-
"en/news/_posts/#{yyyy}-#{mm}-#{dd}-#{name}.md"
23+
"en/news/_posts/#{post_date_string}-#{post_title}.md"
2624
end
2725

2826
# Returns true if the post URL does not match the expected format:
2927
#
3028
# /en/news/yyyy/mm/dd/ruby-2-7-0-released/
3129
#
3230
def post_url_invalid?
33-
!%r{\A/en/news/\d{4}/\d\d/\d\d/[^/]*/\Z}.match?(post)
31+
!post_url_data
3432
end
3533

3634
# Returns true if the release date and the post date do not match.
@@ -50,11 +48,37 @@ def post_missing?
5048

5149
private
5250

51+
# Returns the MatchData for date and title parts of the post URL,
52+
# or +nil+ if the post URL does not match the expected format.
53+
#
54+
# The MatchData includes the named captures yyyy, mm, dd, and title.
55+
#
56+
# Example URL:
57+
#
58+
# /en/news/2019/12/25/ruby-2-7-0-released/
59+
#
60+
def post_url_data
61+
pattern = %r{\A/en/news/(?<yyyy>\d{4})/(?<mm>\d\d)/(?<dd>\d\d)/(?<title>[^/]*)/\z}
62+
63+
pattern.match(post)
64+
end
65+
5366
# The date corresponding to the given post URL.
5467
def post_date_string
55-
%r{\A/en/news/(?<yyyy>\d{4})/(?<mm>\d\d)/(?<dd>\d\d)/(?<name>[^/]*)/\Z} =~ post
68+
return unless post_url_data
69+
70+
yyyy = post_url_data[:yyyy]
71+
mm = post_url_data[:mm]
72+
dd = post_url_data[:dd]
5673

5774
"#{yyyy}-#{mm}-#{dd}"
5875
end
76+
77+
# The post title corresponding to the given post URL.
78+
def post_title
79+
return unless post_url_data
80+
81+
post_url_data[:title]
82+
end
5983
end
6084
end

0 commit comments

Comments
 (0)