configutron 0.2.1 → 0.3.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.markdown +33 -5
- data/Rakefile +3 -1
- data/VERSION +1 -1
- data/lib/configutron.rb +14 -5
- data/spec/configutron_spec.rb +50 -33
- data/spec/{fake_rails_root/config/settings/development.yml → fake_yml_files.rb} +11 -3
- data/spec/spec_helper.rb +27 -1
- metadata +14 -3
data/README.markdown
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
configutron
|
2
2
|
===========
|
3
3
|
|
4
|
-
configutron is a rails gemplugin for simple, application wide
|
4
|
+
configutron is a rails gemplugin for simple, application wide settings. there are tons of projects like this; each does it differently, and this is how i like to do it. all the ones I tried seemed too heavy handed. i am publishing this as a convenience for myself to use in projects i work on.
|
5
5
|
|
6
6
|
usage
|
7
7
|
-----
|
@@ -12,16 +12,42 @@ use the gem in your rails project:
|
|
12
12
|
config.gem 'configutron'
|
13
13
|
end
|
14
14
|
|
15
|
-
|
15
|
+
you have two options for specifying settings.
|
16
|
+
|
17
|
+
1. one file (_config/settings.yml_)
|
18
|
+
2. one file per environment (_config/settings/environment\_name.yml_)
|
19
|
+
|
20
|
+
option 1 takes presedence; that is, if _config/settings.yml_ exists, it will be used, and the _config/settings_ directory will be ignored.
|
21
|
+
|
22
|
+
config/settings.yml
|
23
|
+
-------------------
|
24
|
+
this option uses one file for the application, with settings for each
|
25
|
+
environment nested under the environment's name, like database.yml.
|
26
|
+
|
27
|
+
add your variables to that file:
|
28
|
+
|
29
|
+
% echo "--- \ndevelopment: \n variable: value\n" > config/settings.yml
|
30
|
+
|
31
|
+
try it out:
|
32
|
+
|
33
|
+
% ./script/console
|
34
|
+
Loading development environment (Rails 2.3.5)
|
35
|
+
$ >> Configutron.variable
|
36
|
+
% => "value
|
37
|
+
|
38
|
+
config/settings/RAILS\_ENV.yml
|
39
|
+
------------------------------
|
40
|
+
|
41
|
+
create a folder *config/settings*. configutron will be available for each environment
|
42
|
+
that has a corresponding yaml file in that directory.
|
16
43
|
|
17
44
|
% mkdir config/settings
|
18
|
-
% touch config/settings/development.yml config/settings/test.yml config/settings/production.yml
|
19
45
|
|
20
46
|
add your variables to those files:
|
21
47
|
|
22
48
|
% echo "--- \nvariable: value\n" > config/settings/development.yml
|
23
49
|
|
24
|
-
|
50
|
+
try it out:
|
25
51
|
|
26
52
|
% ./script/console
|
27
53
|
Loading development environment (Rails 2.3.5)
|
@@ -29,6 +55,8 @@ make sure it works:
|
|
29
55
|
=> "value"
|
30
56
|
|
31
57
|
name it something easier to type:
|
58
|
+
---------------------------------
|
59
|
+
back in _environment.rb_
|
32
60
|
|
33
61
|
Rails::Initializer.run do |config|
|
34
62
|
config.gem 'configutron'
|
@@ -36,7 +64,7 @@ name it something easier to type:
|
|
36
64
|
|
37
65
|
Configutron.constant = 'App'
|
38
66
|
|
39
|
-
|
67
|
+
try it out:
|
40
68
|
|
41
69
|
% ./script/console
|
42
70
|
Loading development environment (Rails 2.3.5)
|
data/Rakefile
CHANGED
@@ -13,6 +13,8 @@ begin
|
|
13
13
|
gem.add_dependency "activesupport", ">= 0"
|
14
14
|
gem.add_runtime_dependency "activesupport", ">= 0"
|
15
15
|
gem.add_development_dependency "rspec", ">= 1.2.9"
|
16
|
+
gem.add_development_dependency "fakefs", ">= 0.2.1"
|
17
|
+
|
16
18
|
gem.files = [
|
17
19
|
".gitignore",
|
18
20
|
"LICENSE",
|
@@ -25,7 +27,7 @@ begin
|
|
25
27
|
"spec/configutron_spec.rb",
|
26
28
|
"spec/spec.opts",
|
27
29
|
"spec/spec_helper.rb",
|
28
|
-
"spec/
|
30
|
+
"spec/fake_yml_files.rb"
|
29
31
|
]
|
30
32
|
end
|
31
33
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/configutron.rb
CHANGED
@@ -13,12 +13,21 @@ module Configutron
|
|
13
13
|
def method_missing(key)
|
14
14
|
configutron[key].to_openstruct || nil
|
15
15
|
end
|
16
|
-
|
17
|
-
def settings_path
|
18
|
-
File.expand_path("#{RAILS_ROOT}/config/settings/#{RAILS_ENV}.yml")
|
19
|
-
end
|
20
16
|
|
21
17
|
def configutron
|
22
|
-
@configutron ||=
|
18
|
+
@configutron ||= setup
|
19
|
+
end
|
20
|
+
|
21
|
+
def setup
|
22
|
+
settings_path = File.expand_path("#{RAILS_ROOT}/config/settings.yml")
|
23
|
+
env_settings_path = File.expand_path("#{RAILS_ROOT}/config/settings/#{RAILS_ENV}.yml")
|
24
|
+
|
25
|
+
if File.exist?(settings_path)
|
26
|
+
@configutron = YAML.load_file(settings_path)[RAILS_ENV].symbolize_keys
|
27
|
+
elsif File.exists?(env_settings_path)
|
28
|
+
@configutron = YAML.load_file(env_settings_path).symbolize_keys
|
29
|
+
else
|
30
|
+
raise IOError, "Create either config/settings.yml or config/settings/RAILS_ENV.yml"
|
31
|
+
end
|
23
32
|
end
|
24
33
|
end
|
data/spec/configutron_spec.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
RAILS_ROOT =
|
3
|
-
RAILS_ENV
|
2
|
+
RAILS_ROOT = '/rails_root/'
|
3
|
+
RAILS_ENV = 'test'
|
4
4
|
|
5
5
|
describe Configutron do
|
6
|
-
|
7
|
-
|
8
|
-
end
|
6
|
+
include FakeFS::SpecHelpers
|
7
|
+
after(:each) { reset! }
|
9
8
|
|
10
9
|
describe "module methods" do
|
11
10
|
describe ".constant=" do
|
@@ -15,45 +14,63 @@ describe Configutron do
|
|
15
14
|
end
|
16
15
|
end
|
17
16
|
end
|
18
|
-
|
19
|
-
describe "settings file" do
|
20
|
-
before do
|
21
|
-
Configutron.should_receive(:settings_path).and_return(@fixture_path)
|
22
|
-
end
|
23
|
-
|
24
|
-
specify "is loaded from config/settings based on the RAILS_ENV" do
|
25
|
-
lambda { Configutron.some_variable }.should_not raise_error
|
26
|
-
end
|
27
|
-
end
|
28
17
|
|
29
|
-
describe "
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
18
|
+
describe "settings file" do
|
19
|
+
context 'one file per env' do
|
20
|
+
before(:each) { mock_test_yml }
|
21
|
+
|
22
|
+
specify 'is loaded from config/settings/#{RAILS_ENV}.yml' do
|
23
|
+
Configutron.file.should == 'test.yml'
|
24
|
+
end
|
36
25
|
end
|
37
26
|
|
38
|
-
|
39
|
-
|
27
|
+
context 'one file for the whole application' do
|
28
|
+
before(:each) { mock_settings_yml }
|
29
|
+
|
30
|
+
specify 'is loaded from config/settings.yml based on the RAILS_ENV' do
|
31
|
+
Configutron.file.should == 'settings.yml'
|
32
|
+
end
|
40
33
|
end
|
41
34
|
|
42
|
-
|
43
|
-
|
44
|
-
|
35
|
+
context 'that is missing' do
|
36
|
+
specify 'raises an IOError' do
|
37
|
+
lambda { Configutron.snoop_dogg }.should raise_error(IOError, "Create either config/settings.yml or config/settings/RAILS_ENV.yml")
|
38
|
+
end
|
45
39
|
end
|
46
40
|
end
|
47
|
-
|
48
|
-
describe "
|
49
|
-
|
50
|
-
|
41
|
+
|
42
|
+
describe "settings" do
|
43
|
+
before(:each) { mock_test_yml }
|
44
|
+
|
45
|
+
describe "non-nested settings" do
|
46
|
+
specify "strings can be accessed" do
|
47
|
+
Configutron.my_string.should == 'string'
|
48
|
+
end
|
49
|
+
|
50
|
+
specify "integers can be accessed" do
|
51
|
+
Configutron.my_integer.should == 1
|
52
|
+
end
|
53
|
+
|
54
|
+
specify "arrays can be accessed" do
|
55
|
+
Configutron.my_array.should == [1,2,3,4]
|
56
|
+
end
|
57
|
+
|
58
|
+
specify "hashes can be accesed" do
|
59
|
+
Configutron.my_hash.one.should == 1
|
60
|
+
Configutron.my_hash.two.should == 2
|
61
|
+
end
|
51
62
|
end
|
52
63
|
|
53
64
|
describe "nested settings" do
|
54
65
|
specify "can be accessed" do
|
55
|
-
Configutron.
|
66
|
+
Configutron.two.three.should == 3
|
56
67
|
end
|
57
|
-
|
68
|
+
|
69
|
+
describe "nested settings" do
|
70
|
+
specify "can be accessed" do
|
71
|
+
Configutron.four.five.six.should == 6
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
58
75
|
end
|
59
76
|
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
TEST_YML = <<-END_SETTINGS
|
2
|
+
file: test.yml
|
3
|
+
|
1
4
|
some_variable: some value
|
2
5
|
|
3
6
|
my_string: string
|
@@ -9,15 +12,20 @@ my_array:
|
|
9
12
|
- 2
|
10
13
|
- 3
|
11
14
|
- 4
|
12
|
-
|
15
|
+
|
13
16
|
my_hash:
|
14
17
|
one: 1
|
15
18
|
two: 2
|
16
|
-
|
19
|
+
|
17
20
|
two:
|
18
21
|
three: 3
|
19
22
|
|
20
23
|
four:
|
21
|
-
five:
|
24
|
+
five:
|
22
25
|
six: 6
|
26
|
+
END_SETTINGS
|
23
27
|
|
28
|
+
SETTINGS_YML = <<-END_SETTINGS
|
29
|
+
test:
|
30
|
+
file: settings.yml
|
31
|
+
END_SETTINGS
|
data/spec/spec_helper.rb
CHANGED
@@ -3,6 +3,32 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
3
3
|
require 'configutron'
|
4
4
|
require 'spec'
|
5
5
|
require 'spec/autorun'
|
6
|
+
require 'fake_yml_files'
|
6
7
|
|
7
8
|
require 'rubygems'
|
8
|
-
require 'active_support'
|
9
|
+
require 'active_support'
|
10
|
+
require 'fakefs/spec_helpers'
|
11
|
+
|
12
|
+
def mock_test_yml
|
13
|
+
path = '/rails_root/config/settings/test.yml'
|
14
|
+
|
15
|
+
FileUtils.mkdir_p('/rails_root/config/settings')
|
16
|
+
File.open(path, 'w') do |f|
|
17
|
+
f.puts TEST_YML
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def mock_settings_yml
|
22
|
+
FileUtils.mkdir_p('/rails_root/config')
|
23
|
+
File.open('/rails_root/config/settings.yml', 'w') do |f|
|
24
|
+
f.puts SETTINGS_YML
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def reset!
|
29
|
+
FileUtils.rm_rf('/rails_root')
|
30
|
+
|
31
|
+
Configutron.instance_eval do
|
32
|
+
@configutron = nil
|
33
|
+
end
|
34
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configutron
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Neal Clark
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-03-03 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -42,6 +42,16 @@ dependencies:
|
|
42
42
|
- !ruby/object:Gem::Version
|
43
43
|
version: 1.2.9
|
44
44
|
version:
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: fakefs
|
47
|
+
type: :development
|
48
|
+
version_requirement:
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.2.1
|
54
|
+
version:
|
45
55
|
description: simple app-wide settings for rails applications
|
46
56
|
email: nclark@thrownproject.com
|
47
57
|
executables: []
|
@@ -61,7 +71,7 @@ files:
|
|
61
71
|
- lib/core_ext.rb
|
62
72
|
- rails/init.rb
|
63
73
|
- spec/configutron_spec.rb
|
64
|
-
- spec/
|
74
|
+
- spec/fake_yml_files.rb
|
65
75
|
- spec/spec.opts
|
66
76
|
- spec/spec_helper.rb
|
67
77
|
has_rdoc: true
|
@@ -94,4 +104,5 @@ specification_version: 3
|
|
94
104
|
summary: simple app-wide settings for rails apps
|
95
105
|
test_files:
|
96
106
|
- spec/configutron_spec.rb
|
107
|
+
- spec/fake_yml_files.rb
|
97
108
|
- spec/spec_helper.rb
|