magiconf 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.markdown +125 -0
- data/Rakefile +1 -0
- data/lib/magiconf.rb +22 -0
- data/lib/magiconf/railtie.rb +8 -0
- data/lib/magiconf/version.rb +3 -0
- data/magiconf.gemspec +21 -0
- metadata +77 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3@magiconf --create
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Seth Vargo
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
Magiconf
|
2
|
+
========
|
3
|
+
Magiconf is a light-weight alternative to figaro that is designed to work directly with Rails. Easily store and access all your application's configuration data using a single yaml file. A great solution for storing passwords, dealing with heroku, and working in a team with different development environments.
|
4
|
+
|
5
|
+
Installation
|
6
|
+
------------
|
7
|
+
1. Add `magiconf` to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'magiconf'
|
10
|
+
|
11
|
+
Don't forget to run the `bundle` command to install.
|
12
|
+
|
13
|
+
2. Next, create `config/application.yml`:
|
14
|
+
|
15
|
+
touch config/application.yml
|
16
|
+
|
17
|
+
3. Add your configuration variables:
|
18
|
+
|
19
|
+
# config/application.yml
|
20
|
+
username: 'seth'
|
21
|
+
password: 'aj29slda'
|
22
|
+
development:
|
23
|
+
pusher_url: 'http://github.com/pusher'
|
24
|
+
production:
|
25
|
+
pusher_url: 'http://google.com/pusher'
|
26
|
+
|
27
|
+
Usage
|
28
|
+
-----
|
29
|
+
You now have access to all your configuration data in an easy constant: `YourApp::Config`. For example, if my Rails app was name `Envelope`, I could access my configuration with `Envelope::Config`. Let's look at a simple example:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
# Gemfile
|
33
|
+
gem 'magiconf'
|
34
|
+
gem 'rails'
|
35
|
+
```
|
36
|
+
|
37
|
+
```text
|
38
|
+
# config/application.yml
|
39
|
+
username: 'seth'
|
40
|
+
development:
|
41
|
+
password: 'test'
|
42
|
+
production:
|
43
|
+
password: 'super_secret_test'
|
44
|
+
```
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
# app/controllers/application_controller.rb
|
48
|
+
def authenticate
|
49
|
+
username = Envelope::Config.username
|
50
|
+
password = Envelope::Config.password
|
51
|
+
|
52
|
+
render :unathorized unless params[:username] == username && params[:password] == password
|
53
|
+
end
|
54
|
+
```
|
55
|
+
**Note: Magiconf will automatically merge the keys associated with your current Rails environment with the top-level ones.**
|
56
|
+
|
57
|
+
Nested Keys
|
58
|
+
-----------
|
59
|
+
Sometimes, it might make sense to nest your data. For example, a common pusher configuration might look like this:
|
60
|
+
|
61
|
+
```yaml
|
62
|
+
# config/application.yml
|
63
|
+
pusher:
|
64
|
+
app_id: 123
|
65
|
+
key: abcdef
|
66
|
+
secret: abcdefghijklmnop1234567890
|
67
|
+
```
|
68
|
+
|
69
|
+
The `.pusher` method would return this data, accessible in a hash:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
>> Envelope::Config.pusher
|
73
|
+
=> {"app_id"=>123, "key"=>"abcdef", "secret"=>"abcdefghijklmnop1234567890"}
|
74
|
+
```
|
75
|
+
|
76
|
+
**Notice that the keys are strings, not symbols.** This means you would do something like this in an initializer:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
# config/initializers/pusher.rb
|
80
|
+
Pusher.app_id = Envelope::Config.pusher['app_id'] # not :app_id
|
81
|
+
Pusher.key = Envelope::Config.pusher['key'] # not :key
|
82
|
+
Pusher.secret = Envelope::Config.pusher['secret'] # not :secret
|
83
|
+
```
|
84
|
+
|
85
|
+
Using in Application Configuration
|
86
|
+
----------------------------------
|
87
|
+
Imagine you want to define application configuration variables with Magiconf - maybe you want to define the default host for mail in development. You'd do something like this:
|
88
|
+
|
89
|
+
```yaml
|
90
|
+
# config/application.yml
|
91
|
+
development:
|
92
|
+
host: 'www.example.com'
|
93
|
+
```
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
# config/environments/development.rb
|
97
|
+
...
|
98
|
+
config.action_mailer.default_url_options = { host: Envelope::Config.host }
|
99
|
+
```
|
100
|
+
|
101
|
+
...And if you try to start up your server, you'll see:
|
102
|
+
|
103
|
+
```text
|
104
|
+
uninitialized constant MyApp::Config (NameError)
|
105
|
+
```
|
106
|
+
|
107
|
+
Huh? Well, it turns out that, even though Magiconf hooks into the Rails initialization process as early as possible, it's not soon enough. There's no way for me to get added anywhere earlier in the stack. Fortunately, there's an easy workaround.
|
108
|
+
|
109
|
+
Open up `config/application.rb` and add the following line to the bottom of the file (around line 72):
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
# config/application.rb
|
113
|
+
...
|
114
|
+
Magiconf.setup!
|
115
|
+
```
|
116
|
+
|
117
|
+
Restart your server and things will work like a charm!
|
118
|
+
|
119
|
+
Contributing
|
120
|
+
------------
|
121
|
+
1. Fork it
|
122
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
123
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
124
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
125
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/magiconf.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Magiconf
|
2
|
+
extend self
|
3
|
+
|
4
|
+
def setup!
|
5
|
+
# Get the namespace and create the module
|
6
|
+
namespace = Rails.application.class.parent_name.constantize
|
7
|
+
nodule = namespace.const_set('Config', Module.new)
|
8
|
+
|
9
|
+
# Open up the config file
|
10
|
+
config = YAML::load( ERB.new( File.read('config/application.yml') ).result )
|
11
|
+
config.merge!( config.fetch(Rails.env, {}) )
|
12
|
+
config.symbolize_keys!
|
13
|
+
|
14
|
+
config.keys.each do |key|
|
15
|
+
nodule.define_singleton_method key do
|
16
|
+
return config[key]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'magiconf/railtie' if defined?(Rails)
|
data/magiconf.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'magiconf/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'magiconf'
|
8
|
+
gem.version = Magiconf::VERSION
|
9
|
+
gem.author = 'Seth Vargo'
|
10
|
+
gem.email = 'sethvargo@gmail.com'
|
11
|
+
gem.description = %q{Magiconf is a tiny gem for managing a Rails application configuration file}
|
12
|
+
gem.summary = %q{Manage a single Rails application config file with Magiconf}
|
13
|
+
gem.homepage = ''
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ['lib']
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rake'
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: magiconf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Seth Vargo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Magiconf is a tiny gem for managing a Rails application configuration
|
31
|
+
file
|
32
|
+
email: sethvargo@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .rvmrc
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.markdown
|
42
|
+
- Rakefile
|
43
|
+
- lib/magiconf.rb
|
44
|
+
- lib/magiconf/railtie.rb
|
45
|
+
- lib/magiconf/version.rb
|
46
|
+
- magiconf.gemspec
|
47
|
+
homepage: ''
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
hash: -605009535946545267
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
hash: -605009535946545267
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.8.24
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Manage a single Rails application config file with Magiconf
|
77
|
+
test_files: []
|