Skip to content

Commit

Permalink
Fix camelize turning 'dr-who' into 'Dr-who' rather than DrWho
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtt committed Feb 19, 2024
1 parent 61c6917 commit 1ca26ba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/bulldoze_renamer/string_inflector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class StringDoesNotInflectToItselfError < ArgumentError; end
extend Forwardable

def self.active_support_inflections
[:underscore, :camelize]
[:underscore]
end

def self.inflections
active_support_inflections + [:dasherize, :upcase, :js_camelize]
active_support_inflections + [:camelize, :dasherize, :upcase, :js_camelize]
end

def_delegators ActiveSupport::Inflector, *StringInflector.active_support_inflections
Expand All @@ -26,7 +26,13 @@ def upcase(w)
underscore(w).upcase
end

def camelize(w)
# Wrapping AR camelize because it would return 'dr-who' as 'Dr-who', we want 'DrWho'
ActiveSupport::Inflector.camelize(underscore(w))
end

def dasherize(w)
# Wrapping AR dasherize because it would return 'DrWho' as 'DrWho', we want 'dr-who'
ActiveSupport::Inflector.dasherize(underscore(w))
end

Expand Down
15 changes: 15 additions & 0 deletions spec/bulldoze_renamer/string_inflector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@
expect(BulldozeRenamer::StringInflector.new('dr_who', 'roger_rabbit').mappings).to eq(expected_mappings)
end

it "has all mappings for dasherized strings" do
expected_mappings = {
current_inflection: :dasherize,
target_inflection: :dasherize,
inflections: {
underscore: { current: "dr_who", target: "roger_rabbit" },
camelize: { current: "DrWho", target: "RogerRabbit" },
dasherize: { current: "dr-who", target: "roger-rabbit" },
upcase: { current: "DR_WHO", target: "ROGER_RABBIT" },
js_camelize: {current: "drWho", target: "rogerRabbit"}
}
}
expect(BulldozeRenamer::StringInflector.new('dr-who', 'roger-rabbit').mappings).to eq(expected_mappings)
end

it "omits duplicate mappings for simple strings" do
expected_mappings = {
current_inflection: :camelize,
Expand Down

0 comments on commit 1ca26ba

Please sign in to comment.