sinatra-mongoid-config 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +63 -0
  2. data/lib/sinatra/mongoid_config.rb +47 -0
  3. metadata +89 -0
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ sinatra-mongoid-config
2
+ ======================
3
+
4
+ Easy configuration when using Mongoid within a Sinatra app. Lazily creates the database connection when called upon.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ It's available as a gem via RubyGems.org, so use standard procedure:
10
+
11
+ $ gem install sinatra-mongoid-config
12
+
13
+ If you're using Bundler, just add `gem "sinatra-mongoid-config"` to your `Gemfile`.
14
+
15
+ Using the Extension
16
+ -------------------
17
+
18
+ As with all Sinatra extensions, it's simply a matter of **requiring the library** and **registering the extension**. Other than that, you should just **set the database name**.
19
+
20
+ require 'sinatra'
21
+ require 'sinatra-mongoid-config'
22
+
23
+ register Sinatra::MongoidConfig
24
+
25
+ configure do
26
+ set :mongo_db, 'the_database_name'
27
+ end
28
+
29
+ get '/' do
30
+ 'It works!'
31
+ end
32
+
33
+ Note that this extension works fine both with classic-style Sinatra apps like the one above, as well as modular-style apps which inherit from `Sinatra::Base`.
34
+
35
+ ### Options
36
+
37
+ app.set :mongo_host, ENV['MONGO_HOST'] || 'localhost'
38
+ app.set :mongo_db, ENV['MONGO_DB'] || 'sinatra-mongoid'
39
+ app.set :mongo_port, ENV['MONGO_PORT'] || Mongo::Connection::DEFAULT_PORT
40
+ app.set :mongo_user, ENV['MONGO_USER']
41
+ app.set :mongo_password, ENV['MONGO_PASSWORD']
42
+
43
+ All configuration options have **sensible defaults listed above**, and in most cases, you can get away with just setting the `:mongo_db`. Remember that you can also change settings for each environment:
44
+
45
+ configure do
46
+ set :mongo_db, 'the_database_development'
47
+ end
48
+
49
+ configure :test do
50
+ set :mongo_db, 'the_database_test'
51
+ end
52
+
53
+ configure :production do
54
+ set :mongo_db, 'the_database_production'
55
+ end
56
+
57
+ Alternatives
58
+ ------------
59
+
60
+ * Dan Croak's [sinatra-mongoid](http://github.com/dancroak/sinatra-mongoid)
61
+ * Ethan Gunderson's [sinatra-mongo-config](http://github.com/ethangunderson/sinatra-mongo-config)
62
+
63
+
@@ -0,0 +1,47 @@
1
+ require 'sinatra/base'
2
+ require 'mongoid'
3
+
4
+
5
+
6
+ module Mongoid
7
+ class Config
8
+
9
+ # Sets a reference to the Sinatra app in which
10
+ # this extension is registered.
11
+ def sinatra_app= app
12
+ @sinatra_app = app unless @sinatra_app
13
+ end
14
+
15
+ # If the database connection doesn't already exist,
16
+ # attempt to connect using options from the Sinatra app.
17
+ def master_with_autoconnect
18
+ unless @master
19
+ self.master = Mongo::Connection.new(@sinatra_app.mongo_host, @sinatra_app.mongo_port).db(@sinatra_app.mongo_db)
20
+ @master.authenticate(@sinatra_app.mongo_user, @sinatra_app.mongo_password) if @sinatra_app.mongo_user
21
+ end
22
+ master_without_autoconnect
23
+ end
24
+
25
+ alias_method :master_without_autoconnect, :master
26
+ alias_method :master, :master_with_autoconnect
27
+
28
+ end
29
+ end
30
+
31
+
32
+
33
+ module Sinatra
34
+ module MongoidConfig
35
+
36
+ def self.registered app
37
+ Mongoid.configure.sinatra_app = app
38
+
39
+ app.set :mongo_host, ENV['MONGO_HOST'] || 'localhost'
40
+ app.set :mongo_db, ENV['MONGO_DB'] || 'sinatra-mongoid'
41
+ app.set :mongo_port, ENV['MONGO_PORT'] || Mongo::Connection::DEFAULT_PORT
42
+ app.set :mongo_user, ENV['MONGO_USER']
43
+ app.set :mongo_password, ENV['MONGO_PASSWORD']
44
+ end
45
+
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-mongoid-config
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Matt Puchlerz
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-19 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: sinatra
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ version: "1.0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: mongoid
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 2
42
+ - 0
43
+ - 0
44
+ version: 2.0.0
45
+ type: :runtime
46
+ version_requirements: *id002
47
+ description: Easy configuration when using Mongoid within a Sinatra app. Lazily creates the database connection when called upon.
48
+ email: matt@puchlerz.com
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ extra_rdoc_files: []
54
+
55
+ files:
56
+ - README.md
57
+ - lib/sinatra/mongoid_config.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/mattpuchlerz/sinatra-mongoid-config
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.6
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Mongoid config for Sinatra.
88
+ test_files: []
89
+