Skip to content

Instantly share code, notes, and snippets.

@shepmaster
Created December 28, 2012 23:41
Show Gist options
  • Select an option

  • Save shepmaster/4403172 to your computer and use it in GitHub Desktop.

Select an option

Save shepmaster/4403172 to your computer and use it in GitHub Desktop.
module X
def type(code, name)
instance_eval do
define_method "#{name}?" do |type|
type == code
end
end
end
end
class Foo
extend X
type 0, :integer
type 1, :string
end
describe Foo do
it "knows if it is an integer" do
subject.should be_integer(0)
end
it "knows if it is not an integer" do
subject.should_not be_integer(1)
end
it "knows if it is a string" do
subject.should be_string(1)
end
end
describe X do
class Bar
extend X
type 99, :cat
end
it "associates a name with a type code" do
Bar.new.should be_cat(99)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment