Skip to content

Commit

Permalink
Implement inflection patches
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Sep 16, 2023
1 parent 09f86e8 commit c44ebd9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/kangaru/patches/inflections.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Kangaru
module Patches
module Inflections
refine String do
def to_class_name
Inflectors::ClassInflector.inflect(self)
end

def to_constant_name
Inflectors::ConstantInflector.inflect(self)
end
end
end
end
end
11 changes: 11 additions & 0 deletions sig/kangaru/patches/inflections.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Kangaru
module Patches
module Inflections : String
class ::String
def to_class_name: -> String

def to_constant_name: -> String
end
end
end
end
40 changes: 40 additions & 0 deletions spec/kangaru/patches/inflections_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
RSpec.describe Kangaru::Patches::Inflections do
using described_class

describe String do
subject(:string) { "foo/bar/baz" }

shared_examples :delegates_to_inflector do |options|
let(:inflector_class) { options[:inflector] }

let(:inflector) { instance_double(inflector_class, inflect: "output") }

before do
allow(inflector_class).to receive(:new).and_return(inflector)
end

it "instantiates a #{options[:inflector]}" do
subject
expect(inflector_class).to have_received(:new).with(string).once
end

it "inflects and returns the output" do
expect(subject).to eq(inflector.inflect)
end
end

describe "#to_class_name" do
subject(:class_name) { string.to_class_name }

include_examples :delegates_to_inflector,
inflector: Kangaru::Inflectors::ClassInflector
end

describe "#to_constant_name" do
subject(:constant_name) { string.to_constant_name }

include_examples :delegates_to_inflector,
inflector: Kangaru::Inflectors::ConstantInflector
end
end
end

0 comments on commit c44ebd9

Please sign in to comment.