Documentation: http://alexeypetrushin.github.com/mongodb_model
Object Model for MongoDB (callbacks, validations, mass-assignment, finders, ...).
- The same API for pure driver and Models.
- Minimum extra abstractions, trying to keep things as close to the MongoDB semantic as possible.
- Schema-less, dynamic (with ability to specify types for mass-assignment).
- Models can be saved to any collection, dynamically.
- Full support for composite / embedded objects (validations, callbacks, ...).
- Scope, default_scope
- Doesn't try to mimic ActiveRecord, MongoDB is differrent and the Object Model designed to get most of it.
- Works with multiple connections and databases.
- Associations.
- Very small, see [code stats][code_stats].
Other ODM usually try to cover simple but non-standard API of MongoDB behind complex ORM-like abstractions. This tool exposes simplicity and power of MongoDB and leverages its differences.
require 'mongo/model'
Mongo::Model.default_database_name = :default_test
Mongo::Model.default_database.clear
class Unit
inherit Mongo::Model
collection :units
attr_accessor :name, :status, :stats
def inspect; name end
end
class Unit::Stats
inherit Mongo::Model
attr_accessor :attack, :life, :shield
end
zeratul = Unit.new name: 'Zeratul', status: 'alive'
zeratul.stats = Unit::Stats.new attack: 85, life: 300, shield: 100
tassadar = Unit.new name: 'Tassadar', status: 'dead'
tassadar.stats = Unit::Stats.new attack: 0, life: 80, shield: 300
p zeratul.save
p tassadar.save
tassadar.stats.attack = 20
p tassadar.save
p Unit.first(name: 'Zeratul')
p Unit.all(name: 'Zeratul')
Unit.all name: 'Zeratul' do |unit|
p unit
end
p Unit.by_name('Zeratul')
p Unit.first_by_name('Zeratul')
p Unit.all_by_name('Zeratul')
Installation
gem install mongodb_model
License
Copyright (c) Alexey Petrushin, http://petrush.in, released under the MIT license.