diff --git a/lib/kangaru/inflectors/constant_inflector.rb b/lib/kangaru/inflectors/constant_inflector.rb new file mode 100644 index 0000000..c9ec8b1 --- /dev/null +++ b/lib/kangaru/inflectors/constant_inflector.rb @@ -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 diff --git a/sig/kangaru/inflectors/constant_inflector.rbs b/sig/kangaru/inflectors/constant_inflector.rbs new file mode 100644 index 0000000..921d79b --- /dev/null +++ b/sig/kangaru/inflectors/constant_inflector.rbs @@ -0,0 +1,7 @@ +module Kangaru + module Inflectors + class ConstantInflector < ClassInflector + LAST_WORD: Regexp + end + end +end diff --git a/spec/kangaru/inflectors/constant_inflector_spec.rb b/spec/kangaru/inflectors/constant_inflector_spec.rb new file mode 100644 index 0000000..c4d456a --- /dev/null +++ b/spec/kangaru/inflectors/constant_inflector_spec.rb @@ -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