Skip to content

Commit

Permalink
Merge pull request #1306 from Earlopain/pluralization-grammar-byte
Browse files Browse the repository at this point in the history
Make `Rails/PluralizationGrammar` aware of byte methods
  • Loading branch information
koic committed Jul 2, 2024
2 parents e160f23 + 2b8ba41 commit 316a78c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
1 change: 1 addition & 0 deletions changelog/fix_make_pluralization_grammar_aware_of_bytes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1306](https://github.com/rubocop/rubocop-rails/pull/1306): Make `Rails/PluralizationGrammar` aware of byte methods. ([@earlopain][])
44 changes: 29 additions & 15 deletions lib/rubocop/cop/rails/pluralization_grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,39 @@ module Rails
# # bad
# 3.day.ago
# 1.months.ago
# 5.megabyte
# 1.gigabyte
#
# # good
# 3.days.ago
# 1.month.ago
# 5.megabytes
# 1.gigabyte
class PluralizationGrammar < Base
extend AutoCorrector

SINGULAR_DURATION_METHODS = { second: :seconds,
minute: :minutes,
hour: :hours,
day: :days,
week: :weeks,
fortnight: :fortnights,
month: :months,
year: :years }.freeze

RESTRICT_ON_SEND = SINGULAR_DURATION_METHODS.keys + SINGULAR_DURATION_METHODS.values

PLURAL_DURATION_METHODS = SINGULAR_DURATION_METHODS.invert.freeze
SINGULAR_METHODS = {
second: :seconds,
minute: :minutes,
hour: :hours,
day: :days,
week: :weeks,
fortnight: :fortnights,
month: :months,
year: :years,
byte: :bytes,
kilobyte: :kilobytes,
megabyte: :megabytes,
gigabyte: :gigabytes,
terabyte: :terabytes,
petabyte: :petabytes,
exabyte: :exabytes,
zettabyte: :zettabytes
}.freeze

RESTRICT_ON_SEND = SINGULAR_METHODS.keys + SINGULAR_METHODS.values

PLURAL_METHODS = SINGULAR_METHODS.invert.freeze

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

Expand Down Expand Up @@ -86,15 +100,15 @@ def literal_number?(node)
end

def pluralize(method_name)
SINGULAR_DURATION_METHODS.fetch(method_name.to_sym).to_s
SINGULAR_METHODS.fetch(method_name.to_sym).to_s
end

def singularize(method_name)
PLURAL_DURATION_METHODS.fetch(method_name.to_sym).to_s
PLURAL_METHODS.fetch(method_name.to_sym).to_s
end

def duration_method?(method_name)
SINGULAR_DURATION_METHODS.key?(method_name) || PLURAL_DURATION_METHODS.key?(method_name)
SINGULAR_METHODS.key?(method_name) || PLURAL_METHODS.key?(method_name)
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rails/pluralization_grammar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,12 @@
it_behaves_like 'enforces pluralization grammar', 'fortnight'
it_behaves_like 'enforces pluralization grammar', 'month'
it_behaves_like 'enforces pluralization grammar', 'year'
it_behaves_like 'enforces pluralization grammar', 'byte'
it_behaves_like 'enforces pluralization grammar', 'kilobyte'
it_behaves_like 'enforces pluralization grammar', 'megabyte'
it_behaves_like 'enforces pluralization grammar', 'gigabyte'
it_behaves_like 'enforces pluralization grammar', 'terabyte'
it_behaves_like 'enforces pluralization grammar', 'petabyte'
it_behaves_like 'enforces pluralization grammar', 'exabyte'
it_behaves_like 'enforces pluralization grammar', 'zettabyte'
end

0 comments on commit 316a78c

Please sign in to comment.