Skip to content
Browse files

the necessities

  • Loading branch information...
0 parents commit 9d7f7fed2375a7a49cf59ab4d54898cdd4a5a7a2 @croaky committed Jan 13, 2010
Showing with 110 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +21 −0 LICENSE
  3. +46 −0 README.md
  4. +15 −0 Rakefile
  5. +26 −0 lib/sinatra/mongoid.rb
2 .gitignore
@@ -0,0 +1,2 @@
+*.swp
+pkg
21 LICENSE
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2010 Dan Croak
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
46 README.md
@@ -0,0 +1,46 @@
+Sinatra Mongoid
+===============
+
+A Mongoid extension for Sinatra.
+
+Usage
+-----
+
+Install the gem:
+
+ sudo gem install sinatra-mongoid
+
+Create your Sinatra app:
+
+ require 'rubygems'
+ require 'sinatra'
+ require 'sinatra/mongoid'
+
+ set :mongo_db, 'job-board'
+
+ configure :production do
+ set :mongo_host, 'db.mongohq.com'
+ end
+
+Configuration
+-------------
+
+All Sinatra options you can set are:
+
+ :mongo_host
+ :mongo_db
+ :mongo_user
+ :mongo_password
+
+Their default values are:
+
+ ENV['MONGO_HOST'] || 'localhost'
+ ENV['MONGO_DB']
+ ENV['MONGO_USER']
+ ENV['MONGO_PASSWORD']
+
+Resources
+---------
+
+* [Mongoid](http://mongoid.org)
+* [Sinatra](http://sinatrarb.com)
15 Rakefile
@@ -0,0 +1,15 @@
+require 'jeweler'
+
+Jeweler::Tasks.new do |gem|
+ gem.name = "sinatra-mongoid"
+ gem.summary = "A Mongoid extension for Sinatra."
+ gem.description = "Like a blind date for the nastiest Ruby Mongo ORM and web framework."
+ gem.email = "dcroak@thoughtbot.com"
+ gem.homepage = "http://github.com/dancroak/sinatra-mongoid"
+ gem.authors = ["Dan Croak"]
+
+ gem.add_dependency("sinatra")
+ gem.add_dependency("mongoid")
+end
+
+Jeweler::GemcutterTasks.new
26 lib/sinatra/mongoid.rb
@@ -0,0 +1,26 @@
+require 'sinatra/base'
+require 'mongoid'
+require 'mongo_ext'
+
+module Sinatra
+ module MongoidExtension
+ module DatabaseConnection
+ connection = Mongo::Connection.new(mongo_host)
+ Mongoid.database = connection.db(mongo_db)
+ if mongo_user
+ Mongoid.database.authenticate(mongo_user, mongo_password)
+ end
+ end
+
+ def self.registered(app)
+ app.set :mongo_host, ENV['MONGO_HOST'] || 'localhost'
+ app.set :mongo_db, ENV['MONGO_DB']
+ app.set :mongo_user, ENV['MONGO_USER']
+ app.set :mongo_password, ENV['MONGO_PASSWORD']
+
+ app.helpers MongoidExtension::DatabaseConnection
+ end
+ end
+
+ register MongoidExtension
+end

0 comments on commit 9d7f7fe

Please sign in to comment.
Something went wrong with that request. Please try again.