node_config 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rbenv-version +1 -0
- data/Gemfile +3 -0
- data/README.md +161 -0
- data/Rakefile +8 -0
- data/lib/generators/node_config_generator.rb +28 -0
- data/lib/generators/templates/config/initializers/node_config.rb.erb +1 -0
- data/lib/generators/templates/config/node_config.rb.erb +7 -0
- data/lib/node_config.rb +8 -0
- data/lib/node_config/railtie.rb +4 -0
- data/lib/node_config/version.rb +3 -0
- data/node_config.gemspec +29 -0
- data/test/fixtures/node_config.yml +2 -0
- data/test/node_config_generator_test.rb +51 -0
- data/test/node_config_test.rb +22 -0
- data/test/test_helper.rb +1 -0
- metadata +122 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/.rbenv-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3-p0
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
# NodeConfig
|
2
|
+
|
3
|
+
Simple "node" configuration solution for Rails applications.
|
4
|
+
|
5
|
+
## Prelude
|
6
|
+
|
7
|
+
"node" is a single machine, which your Rails application is running on.
|
8
|
+
So "node" configuration is the configuration for that specific machine. For example:
|
9
|
+
|
10
|
+
* Your development laptop is also a "node" and needs a local configuration for _development_.
|
11
|
+
* Your server is also a "node", but it needs a different configuration for _production_.
|
12
|
+
|
13
|
+
## Purpose
|
14
|
+
|
15
|
+
We need to maintain lots of configuration keys specific for a "node", like AWS access keys,
|
16
|
+
Google-Analytics keys and much more...
|
17
|
+
|
18
|
+
Also:
|
19
|
+
|
20
|
+
* We want all of this keys to be defined in a _single_ _file_
|
21
|
+
* We want all of this keys to be _loaded_ _automatically_ on Rails startup
|
22
|
+
* We want all of this keys to be available inside Rails application
|
23
|
+
* We want that a missing key results in a _descriptive_ _error_
|
24
|
+
* We want this keys to be loaded into a process, which doesn't load the whole application
|
25
|
+
|
26
|
+
This is what `NodeConfig` tries to simplify.
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
# Gemfile
|
32
|
+
gem("node_config")
|
33
|
+
```
|
34
|
+
|
35
|
+
```bash
|
36
|
+
$ bundle install # Install the gem
|
37
|
+
$ rails generate node_config # Generate configuration files
|
38
|
+
```
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
Put your node configuration into generated `RAILS_ROOT/config/node_config.yml`.
|
43
|
+
|
44
|
+
|
45
|
+
### Within a loaded Rails application
|
46
|
+
|
47
|
+
Your node configuration is available in Rails application config.
|
48
|
+
This is done by the generated initializer `config/initializers/node_config.rb`.
|
49
|
+
|
50
|
+
```yaml
|
51
|
+
# config/node_config.yml
|
52
|
+
foo:
|
53
|
+
bar: :baz
|
54
|
+
```
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
# rails console
|
58
|
+
Rails.application.config.node_config.foo #=> {"bar" => :baz}
|
59
|
+
Rails.application.config.node_config.foo.bar #=> :baz
|
60
|
+
```
|
61
|
+
|
62
|
+
In a Rake task:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
# Rakefile
|
66
|
+
task :my_task do
|
67
|
+
puts(Rails.application.config.node_config.foo.bar) #=> baz
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
### Using without loading Rails
|
72
|
+
|
73
|
+
Outside the Rails you can access the node configuration through the top
|
74
|
+
level module named as your Rails application:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
PetShop.node_config.foo.bar #=> :baz
|
78
|
+
```
|
79
|
+
|
80
|
+
This is done by loading the generated `RAILS_ROOT/config/node_config.rb`.
|
81
|
+
|
82
|
+
If you have a Rake task for which the node configuration should be available
|
83
|
+
_without_ loading whole Rails, use the generated shortcut task.
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
# lib/tasks/my_task_which_doesnt_need_rails.rake
|
87
|
+
load File.expand_path("../node_config.rake", __FILE__)
|
88
|
+
|
89
|
+
task :my_task_which_doesnt_need_rails => :node_config do
|
90
|
+
puts(PetShop.node_config.foo.bar) #=> baz
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
Or as command line in your `Procfile` (https://github.com/ddollar/foreman)
|
95
|
+
for a worker (https://github.com/defunkt/resque):
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
# Rakefile
|
99
|
+
load File.expand_path("../lib/tasks/node_config.rake", __FILE__)
|
100
|
+
```
|
101
|
+
|
102
|
+
```
|
103
|
+
worker: bundle exec rake node_config resque:work
|
104
|
+
```
|
105
|
+
|
106
|
+
If you have a script for which the node configuration should be available
|
107
|
+
_without_ loading whole Rails, be aware that the process _has_ to be bundled.
|
108
|
+
Otherwise it may not find the gem.
|
109
|
+
|
110
|
+
For example in your backup script (https://github.com/meskyanichi/backup):
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
# config/backup.rb
|
114
|
+
require File.expand_path("../node_config", __FILE__)
|
115
|
+
|
116
|
+
PetShop.node_config.foo.bar #=> :baz
|
117
|
+
```
|
118
|
+
|
119
|
+
Same when deploying with Capistrano (https://github.com/capistrano/capistrano):
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
# config/deploy.rb
|
123
|
+
require File.expand_path("../node_config", __FILE__)
|
124
|
+
|
125
|
+
PetShop.node_config.foo.bar #=> :baz
|
126
|
+
```
|
127
|
+
|
128
|
+
Or even in an external Ruby script:
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
# foo.rb
|
132
|
+
require "/srv/www/pet_shop/current/config/node_config"
|
133
|
+
|
134
|
+
PetShop.node_config.foo.bar #=> :baz
|
135
|
+
```
|
136
|
+
|
137
|
+
If you have a process, which isn't bundled, then require the gem manually:
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
# foo.rb
|
141
|
+
require "rubygems"
|
142
|
+
require "node_config"
|
143
|
+
|
144
|
+
require "/srv/www/pet_shop/current/config/node_config"
|
145
|
+
|
146
|
+
PetShop.node_config.foo.bar #=> :baz
|
147
|
+
```
|
148
|
+
|
149
|
+
## Errors
|
150
|
+
|
151
|
+
Under the hood `NodeConfig` uses `HashWithStructAccess`,
|
152
|
+
so a missing key will result in appropriate `NoMethodError`
|
153
|
+
(see https://github.com/kostia/hash_with_struct_access for details).
|
154
|
+
|
155
|
+
|
156
|
+
## Using with Git
|
157
|
+
|
158
|
+
Of course you have instruct Git to ignore `config/node.yml`.
|
159
|
+
|
160
|
+
But it's a good practice to maintain a configuration template with the list of required keys,
|
161
|
+
for example in `config/node.yml.template`.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
|
3
|
+
class NodeConfigGenerator < Rails::Generators::Base
|
4
|
+
desc("Install NodeConfig")
|
5
|
+
source_root(File.expand_path("../templates", __FILE__))
|
6
|
+
|
7
|
+
def generate_initializer
|
8
|
+
@both_app_modules = Rails.application.class.name
|
9
|
+
template("config/initializers/node_config.rb.erb", "config/initializers/node_config.rb")
|
10
|
+
end
|
11
|
+
|
12
|
+
def generate_loader
|
13
|
+
@top_level_app_module = Rails.application.class.name.split("::").first
|
14
|
+
template("config/node_config.rb.erb", "config/node_config.rb")
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_rakefile
|
18
|
+
rakefile("node_config.rake") do
|
19
|
+
%Q{task :node_config do
|
20
|
+
load File.expand_path("../../../config/node_config.rb", __FILE__)
|
21
|
+
end}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_configuration
|
26
|
+
create_file("config/node_config.yml")
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= @both_app_modules %>.config.node_config = NodeConfig.load_file(File.expand_path("../../node_config.yml", __FILE__))
|
data/lib/node_config.rb
ADDED
data/node_config.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.expand_path("../lib/node_config/version", __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = "node_config"
|
7
|
+
gem.version = NodeConfig::VERSION
|
8
|
+
gem.description = %q{Simple "node" configuration solution for Rails applications}
|
9
|
+
gem.summary = %q{
|
10
|
+
Simple "node" configuration solution for Rails applications.
|
11
|
+
|
12
|
+
We need to maintain lots of configuration keys specific for a "node", like AWS access keys,
|
13
|
+
Google-Analytics keys and much more.
|
14
|
+
|
15
|
+
This is what this gem tries to simplify.
|
16
|
+
}
|
17
|
+
gem.homepage = "https://github.com/kostia/node_config"
|
18
|
+
gem.authors = ["Kostiantyn Kahanskyi"]
|
19
|
+
gem.email = %w(kostiantyn.kahanskyi@googlemail.com)
|
20
|
+
gem.files = `git ls-files`.split("\n")
|
21
|
+
gem.test_files = `git ls-files -- test/*`.split("\n")
|
22
|
+
gem.require_paths = %w(lib)
|
23
|
+
gem.required_ruby_version = ">= 1.9.2"
|
24
|
+
gem.add_dependency("hash_with_struct_access")
|
25
|
+
gem.add_dependency("rails", ">= 3.2.1")
|
26
|
+
gem.add_development_dependency("minitest")
|
27
|
+
gem.add_development_dependency("rake")
|
28
|
+
gem.add_development_dependency("turn")
|
29
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "./test/test_helper"
|
2
|
+
|
3
|
+
require "generators/node_config_generator"
|
4
|
+
|
5
|
+
module PetShop
|
6
|
+
class Application
|
7
|
+
def self.config
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class NodeConfigGeneratorTest < Rails::Generators::TestCase
|
13
|
+
tests(NodeConfigGenerator)
|
14
|
+
destination("/tmp")
|
15
|
+
setup(:prepare_destination)
|
16
|
+
|
17
|
+
# Stub application module
|
18
|
+
def Rails.application
|
19
|
+
::PetShop::Application.new
|
20
|
+
end
|
21
|
+
|
22
|
+
test "should create YAML config file" do
|
23
|
+
run_generator
|
24
|
+
assert_file("config/node_config.yml")
|
25
|
+
end
|
26
|
+
|
27
|
+
test "should create rakefile" do
|
28
|
+
run_generator
|
29
|
+
assert_file("lib/tasks/node_config.rake") do |rakefile|
|
30
|
+
rakefile.squeeze(" ").must_equal(%Q{task :node_config do
|
31
|
+
load File.expand_path("../../../config/node_config.rb", __FILE__)
|
32
|
+
end}.squeeze(" "))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
test "should generate loader" do
|
37
|
+
run_generator
|
38
|
+
assert_file("config/node_config.rb") do |loader|
|
39
|
+
loader.must_match(/^require "node_config"/)
|
40
|
+
eval(loader)
|
41
|
+
Proc.new { PetShop.node_config }.must_raise(Errno::ENOENT)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
test "should generate initializer" do
|
46
|
+
run_generator
|
47
|
+
assert_file("config/initializers/node_config.rb") do |initializer|
|
48
|
+
Proc.new { eval(initializer) }.must_raise(Errno::ENOENT)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "./test/test_helper"
|
2
|
+
|
3
|
+
require "node_config"
|
4
|
+
|
5
|
+
describe NodeConfig do
|
6
|
+
describe "#load_config" do
|
7
|
+
before do
|
8
|
+
@config_path = File.expand_path("../fixtures/node_config.yml", __FILE__)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should load and parse YAML file with given path" do
|
12
|
+
node_config = NodeConfig.load_file(@config_path)
|
13
|
+
node_config.must_equal({"foo" => {"bar" => :baz}})
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should create a hash with struct access from loaded data" do
|
17
|
+
node_config = NodeConfig.load_file(@config_path)
|
18
|
+
node_config.must_be_kind_of(HashWithStructAccess)
|
19
|
+
node_config.foo.bar.must_equal(:baz)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "turn/autorun"
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: node_config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kostiantyn Kahanskyi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: hash_with_struct_access
|
16
|
+
requirement: &70327627737620 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70327627737620
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rails
|
27
|
+
requirement: &70327627735420 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.2.1
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70327627735420
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: minitest
|
38
|
+
requirement: &70327627747880 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70327627747880
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: &70327627745120 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70327627745120
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: turn
|
60
|
+
requirement: &70327627742600 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70327627742600
|
69
|
+
description: Simple "node" configuration solution for Rails applications
|
70
|
+
email:
|
71
|
+
- kostiantyn.kahanskyi@googlemail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rbenv-version
|
78
|
+
- Gemfile
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/generators/node_config_generator.rb
|
82
|
+
- lib/generators/templates/config/initializers/node_config.rb.erb
|
83
|
+
- lib/generators/templates/config/node_config.rb.erb
|
84
|
+
- lib/node_config.rb
|
85
|
+
- lib/node_config/railtie.rb
|
86
|
+
- lib/node_config/version.rb
|
87
|
+
- node_config.gemspec
|
88
|
+
- test/fixtures/node_config.yml
|
89
|
+
- test/node_config_generator_test.rb
|
90
|
+
- test/node_config_test.rb
|
91
|
+
- test/test_helper.rb
|
92
|
+
homepage: https://github.com/kostia/node_config
|
93
|
+
licenses: []
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.9.2
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 1.8.17
|
113
|
+
signing_key:
|
114
|
+
specification_version: 3
|
115
|
+
summary: Simple "node" configuration solution for Rails applications. We need to
|
116
|
+
maintain lots of configuration keys specific for a "node", like AWS access keys,
|
117
|
+
Google-Analytics keys and much more. This is what this gem tries to simplify.
|
118
|
+
test_files:
|
119
|
+
- test/fixtures/node_config.yml
|
120
|
+
- test/node_config_generator_test.rb
|
121
|
+
- test/node_config_test.rb
|
122
|
+
- test/test_helper.rb
|