Skip to content

Commit 316a78c

Browse files
authored
Merge pull request #1306 from Earlopain/pluralization-grammar-byte
Make `Rails/PluralizationGrammar` aware of byte methods
2 parents e160f23 + 2b8ba41 commit 316a78c

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1306](https://github.com/rubocop/rubocop-rails/pull/1306): Make `Rails/PluralizationGrammar` aware of byte methods. ([@earlopain][])

lib/rubocop/cop/rails/pluralization_grammar.rb

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,39 @@ module Rails
1010
# # bad
1111
# 3.day.ago
1212
# 1.months.ago
13+
# 5.megabyte
14+
# 1.gigabyte
1315
#
1416
# # good
1517
# 3.days.ago
1618
# 1.month.ago
19+
# 5.megabytes
20+
# 1.gigabyte
1721
class PluralizationGrammar < Base
1822
extend AutoCorrector
1923

20-
SINGULAR_DURATION_METHODS = { second: :seconds,
21-
minute: :minutes,
22-
hour: :hours,
23-
day: :days,
24-
week: :weeks,
25-
fortnight: :fortnights,
26-
month: :months,
27-
year: :years }.freeze
28-
29-
RESTRICT_ON_SEND = SINGULAR_DURATION_METHODS.keys + SINGULAR_DURATION_METHODS.values
30-
31-
PLURAL_DURATION_METHODS = SINGULAR_DURATION_METHODS.invert.freeze
24+
SINGULAR_METHODS = {
25+
second: :seconds,
26+
minute: :minutes,
27+
hour: :hours,
28+
day: :days,
29+
week: :weeks,
30+
fortnight: :fortnights,
31+
month: :months,
32+
year: :years,
33+
byte: :bytes,
34+
kilobyte: :kilobytes,
35+
megabyte: :megabytes,
36+
gigabyte: :gigabytes,
37+
terabyte: :terabytes,
38+
petabyte: :petabytes,
39+
exabyte: :exabytes,
40+
zettabyte: :zettabytes
41+
}.freeze
42+
43+
RESTRICT_ON_SEND = SINGULAR_METHODS.keys + SINGULAR_METHODS.values
44+
45+
PLURAL_METHODS = SINGULAR_METHODS.invert.freeze
3246

3347
MSG = 'Prefer `%<number>s.%<correct>s`.'
3448

@@ -86,15 +100,15 @@ def literal_number?(node)
86100
end
87101

88102
def pluralize(method_name)
89-
SINGULAR_DURATION_METHODS.fetch(method_name.to_sym).to_s
103+
SINGULAR_METHODS.fetch(method_name.to_sym).to_s
90104
end
91105

92106
def singularize(method_name)
93-
PLURAL_DURATION_METHODS.fetch(method_name.to_sym).to_s
107+
PLURAL_METHODS.fetch(method_name.to_sym).to_s
94108
end
95109

96110
def duration_method?(method_name)
97-
SINGULAR_DURATION_METHODS.key?(method_name) || PLURAL_DURATION_METHODS.key?(method_name)
111+
SINGULAR_METHODS.key?(method_name) || PLURAL_METHODS.key?(method_name)
98112
end
99113
end
100114
end

spec/rubocop/cop/rails/pluralization_grammar_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,12 @@
8383
it_behaves_like 'enforces pluralization grammar', 'fortnight'
8484
it_behaves_like 'enforces pluralization grammar', 'month'
8585
it_behaves_like 'enforces pluralization grammar', 'year'
86+
it_behaves_like 'enforces pluralization grammar', 'byte'
87+
it_behaves_like 'enforces pluralization grammar', 'kilobyte'
88+
it_behaves_like 'enforces pluralization grammar', 'megabyte'
89+
it_behaves_like 'enforces pluralization grammar', 'gigabyte'
90+
it_behaves_like 'enforces pluralization grammar', 'terabyte'
91+
it_behaves_like 'enforces pluralization grammar', 'petabyte'
92+
it_behaves_like 'enforces pluralization grammar', 'exabyte'
93+
it_behaves_like 'enforces pluralization grammar', 'zettabyte'
8694
end

0 commit comments

Comments
 (0)