Created
August 10, 2010 17:35
-
-
Save jjw/517645 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
| require 'mongo_mapper' | |
| include MongoMapper | |
| MongoMapper.database = "mm-test" | |
| $db = MongoMapper.database | |
| class EncryptedPassword | |
| def self.to_mongo(value) | |
| print "to_mongo(#{value.inspect})..."; | |
| return('') if value.nil? | |
| puts(value == 'plain text' ? 'OK' : "Error? #{caller[0..3]}") | |
| 'encrypted text' | |
| end | |
| def self.from_mongo(value) | |
| print "from_mongo(#{value.inspect})..."; | |
| return('') if value.nil? | |
| puts(value == 'encrypted text' ? 'OK' : "Error? #{caller[0..3]}") | |
| 'plain text' | |
| end | |
| end | |
| class LoginDetails | |
| include MongoMapper::EmbeddedDocument | |
| key :login, String | |
| key :password, EncryptedPassword | |
| end | |
| class User1 | |
| include MongoMapper::Document | |
| key :name, String | |
| key :login, LoginDetails | |
| end | |
| class User2 | |
| include MongoMapper::Document | |
| key :name, String | |
| key :password, EncryptedPassword | |
| end | |
| User1.collection.remove | |
| User2.collection.remove | |
| @l=LoginDetails.new; @l.password = 'plain text' | |
| puts "\nExpect errors here...\n\n" | |
| @u1=User1.create(:login => @l) | |
| puts "\nbut none here...\n\n" | |
| @u2=User2.create(:password => 'plain text') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment