Skip to content

Instantly share code, notes, and snippets.

@teamon
Created August 2, 2008 19:47
Show Gist options
  • Save teamon/3766 to your computer and use it in GitHub Desktop.
Save teamon/3766 to your computer and use it in GitHub Desktop.
# auto_validate.rb
require "dm-types" # not sure of that...
if property.type.ancestors.include?(Types::Enum)
validates_within property.name, options_with_message({:set => property.type.flag_map.values}, property, :within)
end
# auto -validate_spec
describe 'for Enum properties' do
require 'dm-types' # not sure if it is ok to require dm-types here
before :all do
class ColorBoat
include DataMapper::Resource
property :id, Integer, :serial => true
property :color, Enum[:red, :yellow, :blue]
end
end
before do
@boat = ColorBoat.new
end
it "should accept red color" do
@boat.color = :red
@boat.should be_valid
end
it "should not accept green color" do
@boat.color = :green
@boat.should_not be_valid
@boat.errors.on(:color).should_not be_empty
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment