Skip to content

Instantly share code, notes, and snippets.

@durran
Created January 29, 2012 09:58
Show Gist options
  • Save durran/1698098 to your computer and use it in GitHub Desktop.
Save durran/1698098 to your computer and use it in GitHub Desktop.
Flushing out Mongoid sessions API to Moped.
# encoding: utf-8
require "mongoid/sessions/factory"
module Mongoid #:nodoc:
module Sessions
extend ActiveSupport::Concern
included do
class_attribute :storage_options
end
def collection
self.class.collection
end
# Get the name of the collection this model persists to. This will be
# either the pluralized class name or the option defined in the store_in
# macro.
#
# @example Get the collection name.
# Model.collection_name
#
# @return [ String ] The name of the collection.
#
# @since 3.0.0
def collection_name
self.class.collection_name
end
# Get the session for this model. This is determined in the following order:
#
# 1. Any custom configuration provided by the 'store_in' macro.
# 2. The 'default' session as provided in the mongoid.yml
#
# @example Get the session.
# model.mongo_session
#
# @return [ Moped::Session ] The default moped session.
#
# @since 3.0.0
def mongo_session
self.class.mongo_session
end
# Tell the next persistance operation to store in a specific collection,
# database or session.
#
# @example Save the current document to a different collection.
# model.with(collection: "secondary").save
#
# @example Save the current document to a different database.
# model.with(database: "secondary").save
#
# @example Save the current document to a different session.
# model.with(session: "replica_set").save
#
# @example Save with a combination of options.
# model.with(session: "sharded", database: "secondary").save
#
# @param [ Hash ] options The storage options.
#
# @option options [ String, Symbol ] :collection The collection name.
# @option options [ String, Symbol ] :database The database name.
# @option options [ String, Symbol ] :session The session name.
#
# @return [ Document ] The current document.
#
# @since 3.0.0
def with(options)
end
module ClassMethods
def collection
mongo_session[collection_name]
end
# Get the name of the collection this model persists to. This will be
# either the pluralized class name or the option defined in the store_in
# macro.
#
# @example Get the collection name.
# Model.collection_name
#
# @return [ String ] The name of the collection.
#
# @since 3.0.0
def collection_name
@collection_name ||= __collection_name__
end
# Get the session for this model. This is determined in the following order:
#
# 1. Any custom configuration provided by the 'store_in' macro.
# 2. The 'default' session as provided in the mongoid.yml
#
# @example Get the session.
# Model.mongo_session
#
# @return [ Moped::Session ] The default moped session.
#
# @since 3.0.0
def mongo_session
__session__.tap do |session|
database = __database__
session.use(database[:name])
end
end
# Give this model specific custom default storage options.
#
# @example Store this model by default in "artists"
# class Band
# include Mongoid::Document
# store_in collection: "artists"
# end
#
# @example Store this model by default in the sharded db.
# class Band
# include Mongoid::Document
# store_in database: "echo_shard"
# end
#
# @example Store this model by default in a different session.
# class Band
# include Mongoid::Document
# store_in session: "secondary"
# end
#
# @example Store this model with a combination of options.
# class Band
# include Mongoid::Document
# store_in collection: "artists", database: "secondary"
# end
#
# @param [ Hash ] options The storage options.
#
# @option options [ String, Symbol ] :collection The collection name.
# @option options [ String, Symbol ] :database The database name.
# @option options [ String, Symbol ] :session The session name.
#
# @return [ Class ] The model class.
#
# @since 3.0.0
def store_in(options)
# @todo: Durran: Validate the options.
self.storage_options = options
end
# Tell the next persistance operation to store in a specific collection,
# database or session.
#
# @example Create a document in a different collection.
# Model.with(collection: "secondary").create(name: "test")
#
# @example Create a document in a different database.
# Model.with(database: "secondary").create(name: "test")
#
# @example Create a document in a different session.
# Model.with(session: "secondary").create(name: "test")
#
# @example Create with a combination of options.
# Model.with(session: "sharded", database: "secondary").create
#
# @param [ Hash ] options The storage options.
#
# @option options [ String, Symbol ] :collection The collection name.
# @option options [ String, Symbol ] :database The database name.
# @option options [ String, Symbol ] :session The session name.
#
# @return [ Class ] The model class.
#
# @since 3.0.0
def with(options)
end
private
# Get the name of the collection this model persists to.
#
# @example Get the collection name.
# Model.__collection_name__
#
# @return [ String ] The name of the collection.
#
# @since 3.0.0
def __collection_name__
if storage_options && name = storage_options[:collection]
name.to_sym
else
self.name.collectionize.to_sym
end
end
# Get the database configuration.
#
# @example Get the database configuration.
# Model.__database__
#
# @return [ Hash ] The db config.
#
# @since 3.0.0
def __database__
# @todo: Durran: Raise error if name not defined.
if storage_options && name = storage_options[:database]
Mongoid.databases[name.to_sym]
else
Mongoid.databases[:default]
end
end
# Get the session for this class.
#
# @example Get the session.
# Model.__session__
#
# @return [ Moped::Session ] The moped session.
#
# @since 3.0.0
def __session__
if storage_options && name = storage_options[:session]
Threaded.sessions[name.to_sym] ||= Sessions::Factory.create(name).tap
else
Threaded.sessions[:default] ||= Sessions::Factory.default
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment