Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A rough sketch to how I'd register subtypes.
class Thing < Struct.new(:color)
@@things = {}
def self.parse description
type_name, color = description.split
@@things[type_name].new color
end
def self.type_code code
@@things[code] = self
end
end
class Chair < Thing
type_code 'C'
end
class Phone < Thing
type_code 'P'
end
describe Chair do
context 'a parsed chair' do
subject { Thing.parse 'C brown' }
it { is_expected.to be_a(Thing) }
it { is_expected.to be_a(Chair) }
it { expect(subject.color).to eq('brown') }
end
context 'a parsed phone' do
subject { Thing.parse 'P yellow' }
it { is_expected.to be_a(Thing) }
it { is_expected.to be_a(Phone) }
it { expect(subject.color).to eq('yellow') }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment