Skip to content

Instantly share code, notes, and snippets.

@solnic
Created July 6, 2010 12:43
Show Gist options
  • Select an option

  • Save solnic/465337 to your computer and use it in GitHub Desktop.

Select an option

Save solnic/465337 to your computer and use it in GitHub Desktop.
module DataMapper
class Property
class YNBoolean < Boolean
primitive ::String # otherwise adapters won't be able to correctly store the value
def length
@length ||= 1
end
def valid?(value, negated = false)
dumped_value = dump(value)
ret = (dumped_value == 'Y' || dumped_value == 'N') || (dumped_value.nil? && !required?)
negated ? !ret : ret
end
def dump(value)
value ? 'Y' : 'N'
end
def load(value)
typecast(value)
end
def typecast(value)
if value == 'Y'
true
elsif value == 'N'
false
else
super
end
end
end
end
end
describe DataMapper::Property::YNBoolean do
supported_by :all do
before :all do
class ::Post
include DataMapper::Resource
property :id, Serial
property :published, YNBoolean
auto_migrate!
end
end
it 'should work with Boolean' do
post = ::Post.new :published => true
post.save.should be(true)
post.reload.published.should be(true)
end
it 'should work with Y' do
post = ::Post.new :published => 'Y'
post.save.should be(true)
post.reload.published.should be(true)
end
it 'should work with N' do
post = ::Post.new :published => 'N'
post.save.should be(true)
post.reload.published.should be(false)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment