Skip to content

Instantly share code, notes, and snippets.

@quad
Created March 12, 2015 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quad/b9b8ff26badf1986eb26 to your computer and use it in GitHub Desktop.
Save quad/b9b8ff26badf1986eb26 to your computer and use it in GitHub Desktop.
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