sinatra-mongoid-config 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +94 -22
- data/lib/sinatra/mongoid_config.rb +18 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -15,45 +15,117 @@ If you're using Bundler, just add `gem "sinatra-mongoid-config"` to your `Gemfil
|
|
15
15
|
Using the Extension
|
16
16
|
-------------------
|
17
17
|
|
18
|
-
|
18
|
+
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.
|
19
19
|
|
20
|
+
### "Classic"
|
21
|
+
|
22
|
+
require 'rubygems'
|
20
23
|
require 'sinatra'
|
21
24
|
require 'sinatra-mongoid-config'
|
22
25
|
|
23
|
-
register Sinatra::MongoidConfig
|
24
|
-
|
25
|
-
configure do
|
26
|
-
set :mongo_db, 'the_database_name'
|
27
|
-
end
|
28
|
-
|
29
26
|
get '/' do
|
30
27
|
'It works!'
|
31
28
|
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
29
|
|
35
|
-
###
|
30
|
+
### "Modular"
|
36
31
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
32
|
+
require 'rubygems'
|
33
|
+
require 'sinatra'
|
34
|
+
require 'sinatra-mongoid-config'
|
35
|
+
|
36
|
+
class MyApp < Sinatra::Base
|
37
|
+
|
38
|
+
register Sinatra::MongoidConfig
|
39
|
+
|
40
|
+
get '/' do
|
41
|
+
'It works!'
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
Options & Defaults
|
47
|
+
------------------
|
42
48
|
|
43
|
-
All
|
49
|
+
All options are set using Sinatra's standard `set` method. Remember that you can also change settings for each environment:
|
44
50
|
|
45
51
|
configure do
|
46
|
-
set :mongo_db, '
|
47
|
-
|
48
|
-
|
49
|
-
configure :test do
|
50
|
-
set :mongo_db, 'the_database_test'
|
52
|
+
set :mongo_db, 'the_database'
|
53
|
+
set :mongo_port, 123
|
51
54
|
end
|
52
55
|
|
53
56
|
configure :production do
|
54
|
-
set :mongo_db, '
|
57
|
+
set :mongo_db, 'the_other_database'
|
58
|
+
set :mongo_user, 'the_user'
|
59
|
+
set :mongo_password, 'the_password'
|
55
60
|
end
|
56
61
|
|
62
|
+
All configuration options have **sensible defaults listed below**, and depending on your situation, you may not have to set anything.
|
63
|
+
|
64
|
+
<table>
|
65
|
+
<thead>
|
66
|
+
<tr>
|
67
|
+
<th style="text-align: left">Option</th>
|
68
|
+
<th style="text-align: left">Default</th>
|
69
|
+
</tr>
|
70
|
+
</thead>
|
71
|
+
<tbody>
|
72
|
+
<tr>
|
73
|
+
<td><code>:mongo_host</code></td>
|
74
|
+
<td><code>ENV['MONGO_HOST'] || 'localhost'</code></td>
|
75
|
+
</tr>
|
76
|
+
<tr>
|
77
|
+
<td><code>:mongo_db</code></td>
|
78
|
+
<td><code>ENV['MONGO_DB'] || self.app_to_db_name(app)</code></td>
|
79
|
+
</tr>
|
80
|
+
<tr>
|
81
|
+
<td><code>:mongo_port</code></td>
|
82
|
+
<td><code>ENV['MONGO_PORT'] || Mongo::Connection::DEFAULT_PORT</code></td>
|
83
|
+
</tr>
|
84
|
+
<tr>
|
85
|
+
<td><code>:mongo_user</code></td>
|
86
|
+
<td><code>ENV['MONGO_USER']</code></td>
|
87
|
+
</tr>
|
88
|
+
<tr>
|
89
|
+
<td><code>:mongo_password</code></td>
|
90
|
+
<td><code>ENV['MONGO_PASSWORD']</code></td>
|
91
|
+
</tr>
|
92
|
+
</tbody>
|
93
|
+
</table>
|
94
|
+
|
95
|
+
Did you notice the call to `self.app_to_db_name`? That method attempts to be smart about what your database is named, based on **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:
|
96
|
+
|
97
|
+
<table>
|
98
|
+
<thead>
|
99
|
+
<tr>
|
100
|
+
<th style="text-align: left">App’s Class Name</th>
|
101
|
+
<th style="text-align: left">Environment</th>
|
102
|
+
<th style="text-align: left">Resulting Database Name</th>
|
103
|
+
</tr>
|
104
|
+
</thead>
|
105
|
+
<tbody>
|
106
|
+
<tr>
|
107
|
+
<td><code>Refresh</code></td>
|
108
|
+
<td><code>:development</code></td>
|
109
|
+
<td><code>refresh_development</code></td>
|
110
|
+
</tr>
|
111
|
+
<tr>
|
112
|
+
<td><code>RefreshChicago</code></td>
|
113
|
+
<td><code>:test</code></td>
|
114
|
+
<td><code>refresh_chicago_test</code></td>
|
115
|
+
</tr>
|
116
|
+
<tr>
|
117
|
+
<td><code>RefreshChicago::Jobs</code></td>
|
118
|
+
<td><code>:production</code></td>
|
119
|
+
<td><code>refresh_chicago_jobs_production</code></td>
|
120
|
+
</tr>
|
121
|
+
<tr>
|
122
|
+
<td><code>Sinatra::Application</code></td>
|
123
|
+
<td><code>:development</code></td>
|
124
|
+
<td><code>sinatra_application_development</code></td>
|
125
|
+
</tr>
|
126
|
+
</tbody>
|
127
|
+
</table>
|
128
|
+
|
57
129
|
Alternatives
|
58
130
|
------------
|
59
131
|
|
@@ -37,11 +37,28 @@ module Sinatra
|
|
37
37
|
Mongoid.configure.sinatra_app = app
|
38
38
|
|
39
39
|
app.set :mongo_host, ENV['MONGO_HOST'] || 'localhost'
|
40
|
-
app.set :mongo_db, ENV['MONGO_DB'] ||
|
40
|
+
app.set :mongo_db, ENV['MONGO_DB'] || self.app_to_db_name(app)
|
41
41
|
app.set :mongo_port, ENV['MONGO_PORT'] || Mongo::Connection::DEFAULT_PORT
|
42
42
|
app.set :mongo_user, ENV['MONGO_USER']
|
43
43
|
app.set :mongo_password, ENV['MONGO_PASSWORD']
|
44
44
|
end
|
45
45
|
|
46
|
+
private
|
47
|
+
|
48
|
+
# Inspired by the ActiveSupport Inflector's #underscore method
|
49
|
+
def self.app_to_db_name app
|
50
|
+
name = app.
|
51
|
+
to_s.
|
52
|
+
gsub(/::/, '_').
|
53
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
54
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
55
|
+
tr("-", "_").
|
56
|
+
downcase
|
57
|
+
|
58
|
+
"#{ name }_#{ app.environment }"
|
59
|
+
end
|
60
|
+
|
46
61
|
end
|
62
|
+
|
63
|
+
register MongoidConfig
|
47
64
|
end
|