Ruby persistence layer using MongoDB as database.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Persistence is simple & stupid gem that follows data mapper pattern by introducing a mapper objects that allows you to load, save and find objects using MongoDB database.

The main idea behind Persistence is to use pure Ruby objects that can be persisted and loaded using mapper objects. Mappers acts as a separate layer from your domain objects and it should hopefully let you focus more on logic of you app and keep database out of the way.

Initializing Persistence module

+Persistence+ uses instance based approach to initialize the connection. Then you can register your own mappers:

persistence = 'persistence_database', collection: 'persistence_collection')
persistence.register_mapper PersonMapper

Using mappers to save objects

When mapper saves given objects, it converts object to resource hash (from instance variables). Then checks to see if it was already persisted or not and finally inserts or updates resource.

Persistence also automatically saves class of object in +_type+ field.

# Class without #to_resource method
class Person
  attr_accessor :id
  attr_accessor :name

p1 = = 'Jonny D' p1
# Resource to backend: { _id: '4e649938aabc604f08000001', _type: 'Person', name: 'Jonny D' }
p # => BSON::ObjectId('4e649938aabc604f08000001')

Using mappers to load persisted objects

Based on +_type+ attribute mapper materializes given object with attributes from backend.

class Book
  attr_accessor :id
  attr_accessor :author
  attr_accessor :title

# Resource stored in backend: { _id: '4e649938aabc604f08000001', _type: 'Book', author: 'Jonny D' }

book = PersonMapper.find
p # => BSON::ObjectId('4e649938aabc604f08000001')
p # => "Jonny D"


Copyright (c) 2011, 2012 Jiri Zajpt,