Skip to content

Commit

Permalink
Implement constant inflector
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Sep 16, 2023
1 parent fa2d7d2 commit 09f86e8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/kangaru/inflectors/constant_inflector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Kangaru
module Inflectors
class ConstantInflector < ClassInflector
LAST_WORD = /(::)?(?!.*::)(.*)$/

def inflect
super.gsub(LAST_WORD) do |last_word|
ScreamingSnakecaseInflector.inflect(last_word)
end
end
end
end
end
7 changes: 7 additions & 0 deletions sig/kangaru/inflectors/constant_inflector.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Kangaru
module Inflectors
class ConstantInflector < ClassInflector
LAST_WORD: Regexp
end
end
end
42 changes: 42 additions & 0 deletions spec/kangaru/inflectors/constant_inflector_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
RSpec.describe Kangaru::Inflectors::ConstantInflector do
subject(:inflector) { described_class.new(string) }

describe "#inflect" do
subject(:inflection) { inflector.inflect }

include_examples :runs_inflections, [
{ from: "foo_bar_baz", to: "FOO_BAR_BAZ" },
{ from: "foo_bar__baz", to: "FOO_BAR_BAZ" },
{ from: "foo__bar_baz", to: "FOO_BAR_BAZ" },
{ from: "foo__bar__baz", to: "FOO_BAR_BAZ" },

{ from: "foo-bar-baz", to: "FOO_BAR_BAZ" },
{ from: "foo--bar--baz", to: "FOO_BAR_BAZ" },
{ from: "foo-bar--baz", to: "FOO_BAR_BAZ" },
{ from: "foo--bar-baz", to: "FOO_BAR_BAZ" },

{ from: "fooBarBaz", to: "FOO_BAR_BAZ" },
{ from: "FooBarBaz", to: "FOO_BAR_BAZ" },
{ from: "FOOBARBAZ", to: "FOOBARBAZ" },
{ from: "FOO_BAR_BAZ", to: "FOO_BAR_BAZ" },

{ from: "Foo::Bar::Baz", to: "Foo::Bar::BAZ" },
{ from: "Foo::BarBaz", to: "Foo::BAR_BAZ" },
{ from: "FooBar::Baz", to: "FooBar::BAZ" },

{ from: "::FOOBARBAZ", to: "::FOOBARBAZ" },
{ from: "::Foo::Bar::Baz", to: "::Foo::Bar::BAZ" },
{ from: "::Foo::BarBaz", to: "::Foo::BAR_BAZ" },
{ from: "::FooBar::Baz", to: "::FooBar::BAZ" },
{ from: "::FooBarBaz", to: "::FOO_BAR_BAZ" },
{ from: "::FOO_BAR_BAZ", to: "::FOO_BAR_BAZ" },

{ from: "foo_bar_baz.rb", to: "FOO_BAR_BAZ" },
{ from: "foo/bar/baz.rb", to: "Foo::Bar::BAZ" },
{ from: "foo/barBaz.rb", to: "Foo::BAR_BAZ" },
{ from: "/foo_bar_baz.rb", to: "::FOO_BAR_BAZ" },
{ from: "/foo/bar/baz.rb", to: "::Foo::Bar::BAZ" },
{ from: "/foo/barBaz.rb", to: "::Foo::BAR_BAZ" }
]
end
end

0 comments on commit 09f86e8

Please sign in to comment.