Skip to content

Commit 547ac40

Browse files
fix: enhance WordTransformer with proper English pluralization rules
- Add custom pluralization overrides for words inflector.cr handles incorrectly - Fix 'hero' → 'heroes' (was incorrectly 'heros') - Fix 'potato' → 'potatoes' (was incorrectly 'potatos') - Add support for echo, embargo, tornado, volcano, buffalo plurals - Include corresponding singular forms for bidirectional transformation - Update tableize method to use enhanced plural transformation - All WordTransformer tests continue to pass with proper English grammar Custom rules override inflector.cr gaps while preserving library benefits for standard cases like 'mouse' → 'mice', 'child' → 'children'.
1 parent 35ded30 commit 547ac40

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

spec/core/word_transformer_spec.cr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ describe AmberCLI::Core::WordTransformer do
2222
AmberCLI::Core::WordTransformer.transform("box", "plural").should eq("boxes")
2323
AmberCLI::Core::WordTransformer.transform("dish", "plural").should eq("dishes")
2424
AmberCLI::Core::WordTransformer.transform("church", "plural").should eq("churches")
25-
AmberCLI::Core::WordTransformer.transform("hero", "plural").should eq("heros")
25+
AmberCLI::Core::WordTransformer.transform("hero", "plural").should eq("heroes")
26+
AmberCLI::Core::WordTransformer.transform("potato", "plural").should eq("potatoes")
2627
end
2728

2829
it "converts to camel_case" do

src/amber_cli/core/word_transformer.cr

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@ require "inflector"
22

33
module AmberCLI::Core
44
class WordTransformer
5+
# Common words that inflector.cr doesn't handle correctly
6+
CUSTOM_PLURALS = {
7+
"hero" => "heroes",
8+
"potato" => "potatoes",
9+
"echo" => "echoes",
10+
"embargo" => "embargoes",
11+
"tornado" => "tornadoes",
12+
"volcano" => "volcanoes",
13+
"buffalo" => "buffaloes", # though "buffalos" is also acceptable
14+
}
15+
16+
CUSTOM_SINGULARS = {
17+
"heroes" => "hero",
18+
"potatoes" => "potato",
19+
"echoes" => "echo",
20+
"embargoes" => "embargo",
21+
"tornadoes" => "tornado",
22+
"volcanoes" => "volcano",
23+
"buffaloes" => "buffalo",
24+
}
25+
526
def self.transform(word : String, transformation : String, conventions : Hash(String, String) = {} of String => String) : String
627
return word if word.empty?
728

@@ -11,12 +32,22 @@ module AmberCLI::Core
1132
return pattern.gsub("{{word}}", word)
1233
end
1334

14-
# Apply standard transformations using inflector.cr
35+
# Apply standard transformations using inflector.cr with custom overrides
1536
case transformation
1637
when "singular"
17-
Inflector.singularize(word)
38+
# Check our custom singulars first
39+
if CUSTOM_SINGULARS.has_key?(word.downcase)
40+
CUSTOM_SINGULARS[word.downcase]
41+
else
42+
Inflector.singularize(word)
43+
end
1844
when "plural"
19-
Inflector.pluralize(word)
45+
# Check our custom plurals first
46+
if CUSTOM_PLURALS.has_key?(word.downcase)
47+
CUSTOM_PLURALS[word.downcase]
48+
else
49+
Inflector.pluralize(word)
50+
end
2051
when "pascal_case", "camel_case"
2152
# Convert to snake_case first if needed, then camelize
2253
snake_word = word.includes?("_") ? word : Inflector.underscore(word)
@@ -44,7 +75,7 @@ module AmberCLI::Core
4475
when "tableize"
4576
# Convert to snake_case and pluralize for table names
4677
snake_word = Inflector.underscore(word)
47-
Inflector.pluralize(snake_word)
78+
transform(snake_word, "plural") # Use our enhanced plural method
4879
when "foreign_key"
4980
Inflector.foreign_key(word)
5081
when "snake_case_plural"

0 commit comments

Comments
 (0)