Skip to content

Commit

Permalink
Refactor constantiser into single static method
Browse files Browse the repository at this point in the history
  • Loading branch information
apexatoll committed Sep 20, 2023
1 parent f38a005 commit 9359414
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 46 deletions.
31 changes: 7 additions & 24 deletions lib/kangaru/inflectors/constantiser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,15 @@ module Inflectors
class Constantiser
using Patches::Inflections

attr_reader :string

def initialize(string)
@string = string
end

def constantise
if defined_as_class?
Object.const_get(string.to_class_name)
elsif defined_as_constant?
Object.const_get(string.to_constant_name)
end
end

def self.constantise(string)
new(string).constantise
end

private
as_class = string.to_class_name
as_constant = string.to_constant_name

def defined_as_class?
Object.const_defined?(string.to_class_name)
end

def defined_as_constant?
Object.const_defined?(string.to_constant_name)
if Object.const_defined?(as_class)
Object.const_get(as_class)
elsif Object.const_defined?(as_constant)
Object.const_get(as_constant)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kangaru/patches/constantise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Patches
module Constantise
refine String do
def constantise
Inflectors::Constantiser.new(self).constantise
Inflectors::Constantiser.constantise(self)
end
end
end
Expand Down
10 changes: 0 additions & 10 deletions sig/kangaru/inflectors/constantiser.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@ module Kangaru
class Constantiser
attr_reader string: String

def initialize: (String) -> void

def constantise: -> untyped

def self.constantise: (String) -> untyped

private

def defined_as_class?: -> bool

def defined_as_constant?: -> bool
end
end
end
15 changes: 4 additions & 11 deletions spec/kangaru/patches/constantise_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
RSpec.describe Kangaru::Patches::Constantise do
using described_class

let(:constantiser) { instance_double(constantiser_class, constantise: value) }

let(:constantiser_class) { Kangaru::Inflectors::Constantiser }
let(:constantiser) { Kangaru::Inflectors::Constantiser }

let(:value) { "value" }

before do
allow(constantiser_class).to receive(:new).and_return(constantiser)
allow(constantiser).to receive(:constantise).and_return(value)
end

describe String do
Expand All @@ -17,14 +15,9 @@
describe "#constantise" do
subject(:constant) { string.constantise }

it "instantiates a constantiser" do
constant
expect(constantiser_class).to have_received(:new).with(string).once
end

it "delegates to the constantiser" do
it "delegates to the Constantiser class" do
constant
expect(constantiser).to have_received(:constantise).once
expect(constantiser).to have_received(:constantise).with(string).once
end

it "returns the value from the constantiser" do
Expand Down

0 comments on commit 9359414

Please sign in to comment.