namlook/mongokit — pull request #161 by bneron (2013-12-20 09:10:39+00:00)
Title: embedding an object in a document

this patch allow the user to register some SchemaDocument, the SD regitered will be restore from mongo as a SD not a dict.
this feature allow to emebed an object in a document as describe here https://groups.google.com/forum/#!topic/mongokit/uZ8Kp2QfnFQ.

how it works:

```python
from mongokit import SchemaDocument, Document, Connection, ObjectId
connection = Connection()

class Pet(SchemaDocument):
structure = {'name' : basestring }

#every Canary object will be restore from the mongoDb as Canary
@connection.register
class Canary(Pet):

def say_hi(self):
return "Z'ai cru voir un rominet"

@connection.register
class Cat(Pet):
structure = {
'catch': Pet
}

def say_hi(self):
return "Faprifti"

@connection.register
class TuxedoCat(Cat):
structure = { 'color' : basestring }

@connection.register
class OldTuxedoCat(TuxedoCat):
structure = { 'age' : int }

# the dog will be store in mongoDB as dict and restore as dict
class Dog(Pet):
structure = {
'catch': Pet
}

def say_hi(self):
return "ouaf ouaf"

@connection.register
class Mamie(Document):
__database__ = 'test'
__collection__ = 'mamie'
structure = {
'_type' : unicode,
'bird' : Canary,
'cat' : Cat,
'dog' : Dog,
'pets' : {unicode: Pet}
}

#remove every mamie in db
connection.Mamie.collection.remove({})

titi = Canary()
titi['name'] = 'Titi'

claude = Cat()
claude['name'] = 'Claude'

sylvestre = TuxedoCat()
sylvestre['name'] = 'Sylvestre'
sylvestre['catch'] = titi

hector = Dog()
hector['name'] = 'Hector'
hector['catch'] = sylvestre

mamie= connection.Mamie()

mamie['bird'] = titi
mamie['cat'] = sylvestre
mamie['dog'] = hector
mamie['pets'] = {u'tweety' : titi,
u'sylvester' : sylvestre,
u'other_cat': claude,
u'dog': hector}
mamie.save()

grand_ma = connection.Mamie.fetch_one({})

# the Cat SD is registered so Cat objects are restore as Cat
print "grand_ma['cat'] = ", grand_ma['cat']
print "grand_ma['cat'].say_hi() = ", grand_ma['cat'].say_hi()

# it work's also for SD embed in an other SD
print "grand_ma['cat']['catch'].say_hi() = ", grand_ma['cat']['catch'].say_hi()

print "grand_ma['dog'] = ", grand_ma['dog']
print type(grand_ma['dog'])

#it's works also for collection of pets
print "grand_ma['pets']['sylvester']:", grand_ma['pets']['sylvester'], " ", type(grand_ma['pets']['sylvester'])

```