Skip to content

Instantly share code, notes, and snippets.

@jjw
Created August 10, 2010 17:35
Show Gist options
  • Select an option

  • Save jjw/517645 to your computer and use it in GitHub Desktop.

Select an option

Save jjw/517645 to your computer and use it in GitHub Desktop.
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