1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-12-19 15:01:12 +00:00

There can be only one... UNDEFINED

By ensuring that we only ever create a single UNDEFINED object from the
UndefinedClass, it behaves more like nil
This commit is contained in:
Holger Just 2017-09-07 21:18:01 +02:00
parent 32e755396b
commit 74242caf0b
2 changed files with 24 additions and 9 deletions

View File

@ -84,7 +84,12 @@ module Rackstash
alias inspect to_s alias inspect to_s
end end
UNDEFINED = UndefinedClass.new UNDEFINED = UndefinedClass.new.tap do |undefined|
class << undefined.class
undef_method :allocate
undef_method :new
end
end
EMPTY_STRING = ''.freeze EMPTY_STRING = ''.freeze
EMPTY_SET = Set.new.freeze EMPTY_SET = Set.new.freeze

View File

@ -51,7 +51,16 @@ describe Rackstash do
end end
end end
it 'defines UNDEFINED' do describe 'UNDEFINED' do
it 'defines the UndefinedClass' do
expect(Rackstash::UndefinedClass).to be_a Class
# No (further) ibjects can be created of this class
expect { Rackstash::UndefinedClass.new }.to raise_error NoMethodError
expect { Rackstash::UndefinedClass.allocate }.to raise_error NoMethodError
end
it 'defines the singleton object' do
expect(Rackstash::UNDEFINED).to be_instance_of Rackstash::UndefinedClass expect(Rackstash::UNDEFINED).to be_instance_of Rackstash::UndefinedClass
expect(Rackstash::UNDEFINED.to_s).to eql 'undefined' expect(Rackstash::UNDEFINED.to_s).to eql 'undefined'
@ -62,6 +71,7 @@ describe Rackstash do
expect(Rackstash::UNDEFINED).not_to eql true expect(Rackstash::UNDEFINED).not_to eql true
expect(Rackstash::UNDEFINED).not_to eql 42 expect(Rackstash::UNDEFINED).not_to eql 42
end end
end
describe '.severity_label' do describe '.severity_label' do
it 'returns the label for an integer severity' do it 'returns the label for an integer severity' do