Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Add Monogid to your Sinatra app with potentially zero-configuration. Lazily creates the database connection whenever needed.

branch: master
README.md

sinatra-mongoid-config

Add Monogid to your Sinatra app with potentially zero-configuration. Lazily create the database connection whenever needed.

Installation

It‘s available as a gem via RubyGems.org, so use standard procedure:

$ gem install sinatra-mongoid-config

If you're using Bundler, just add gem "sinatra-mongoid-config" to your Gemfile.

Using the Extension

This extension works fine with both “classic” Sinatra apps, as well as “modular” apps which inherit from Sinatra::Base. How you use the extension varies slightly between styles.

“Classic”

require 'rubygems'
require 'sinatra'
require 'sinatra-mongoid-config'

get '/' do
  'It works!'
end

“Modular”

require 'rubygems'
require 'sinatra'
require 'sinatra-mongoid-config'

class MyApp < Sinatra::Base

  register Sinatra::MongoidConfig

  get '/' do
    'It works!'
  end

end

Options & Defaults

All options are set using Sinatra’s standard set method. Remember that you can also change settings for each environment:

configure do
  set :mongo_db, 'the_database'
  set :mongo_port, 123
end

configure :production do
  set :mongo_db, 'the_other_database'
  set :mongo_user, 'the_user'
  set :mongo_password, 'the_password'
end

Defaults

All configuration options have sensible defaults listed below, and depending on your situation, you may not have to set anything.

Option Default
:mongo_host ENV['MONGO_HOST'] || 'localhost'
:mongo_db ENV['MONGO_DB'] || self.app_to_db_name(app)
:mongo_port ENV['MONGO_PORT'] || Mongo::Connection::DEFAULT_PORT
:mongo_user ENV['MONGO_USER']
:mongo_password ENV['MONGO_PASSWORD']

Default DB Naming Convention

Did you notice that the default for :mongo_db calls self.app_to_db_name? That method attempts to be smart about what your database is named by using the class name of your app and the current environment. The real benefit here is for those who are creating "modular" apps, as they are named something other than the Sinatra default. Here are some examples:

App’s Class Name Environment Resulting Database Name
Refresh :development 'refresh_development'
RefreshChicago :test 'refresh_chicago_test'
RefreshChicago::Jobs :production 'refresh_chicago_jobs_production'
Sinatra::Application :development 'sinatra_application_development'

Alternatives

Something went wrong with that request. Please try again.