Remove Mutant.singleton_subclass_instance
* Only one call side, implementation was inlined
This commit is contained in:
parent
ad948bd7f3
commit
f9d3a275ac
3 changed files with 2 additions and 63 deletions
|
@ -37,33 +37,6 @@ module Mutant
|
|||
def self.ci?
|
||||
ENV.key?('CI')
|
||||
end
|
||||
|
||||
# Define instance of subclassed superclass as constant
|
||||
#
|
||||
# @param [Class] superclass
|
||||
# @param [Symbol] name
|
||||
#
|
||||
# @return [self]
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
# rubocop:disable MethodLength
|
||||
#
|
||||
def self.singleton_subclass_instance(name, superclass, &block)
|
||||
klass = Class.new(superclass) do
|
||||
def inspect
|
||||
self.class.name
|
||||
end
|
||||
|
||||
define_singleton_method(:name) do
|
||||
"#{superclass.name}::#{name}".freeze
|
||||
end
|
||||
end
|
||||
klass.class_eval(&block)
|
||||
superclass.const_set(name, klass.new)
|
||||
self
|
||||
end
|
||||
|
||||
end # Mutant
|
||||
|
||||
require 'mutant/version'
|
||||
|
|
|
@ -15,7 +15,7 @@ module Mutant
|
|||
"\e[#{@code}m#{text}\e[0m"
|
||||
end
|
||||
|
||||
Mutant.singleton_subclass_instance('NONE', self) do
|
||||
NONE = Class.new(self) do
|
||||
|
||||
# Format null color
|
||||
#
|
||||
|
@ -41,7 +41,7 @@ module Mutant
|
|||
def initialize
|
||||
end
|
||||
|
||||
end
|
||||
end.new
|
||||
|
||||
RED = Color.new(31)
|
||||
GREEN = Color.new(32)
|
||||
|
|
|
@ -12,38 +12,4 @@ RSpec.describe Mutant do
|
|||
|
||||
it { should be(result) }
|
||||
end
|
||||
|
||||
describe '.singleton_subclass_instance' do
|
||||
subject { object.singleton_subclass_instance(name, superclass, &block) }
|
||||
|
||||
before { subject }
|
||||
|
||||
let(:name) { 'Test' }
|
||||
let(:block) { proc { def foo; end } }
|
||||
let(:superclass) { Class.new }
|
||||
|
||||
let(:generated) { superclass.const_get(:Test) }
|
||||
|
||||
it_should_behave_like 'a command method'
|
||||
|
||||
it 'sets expected name' do
|
||||
name = generated.class.name
|
||||
expect(name).to eql("::#{self.name}")
|
||||
expect(name).to be_frozen
|
||||
end
|
||||
|
||||
it 'stores instance of subclass' do
|
||||
expect(generated).to be_kind_of(superclass)
|
||||
end
|
||||
|
||||
it 'evaluates the context of proc inside subclass' do
|
||||
expect(generated).to respond_to(:foo)
|
||||
end
|
||||
|
||||
it 'generates nice #inspect' do
|
||||
inspect = generated.inspect
|
||||
expect(inspect).to eql("::#{self.name}")
|
||||
expect(inspect).to be_frozen
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue