Created
July 6, 2010 12:43
-
-
Save solnic/465337 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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