rails_config 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/TODO ADDED
@@ -0,0 +1,10 @@
1
+ - Add a Rails before_filter that triggers Settings.reload! for each development request
2
+ - Allow user to customize the name of the global setting object (Settings)
3
+ - Add generator `rails_config:install` to create the default setting files
4
+ - config/settings.yml
5
+ - config/settings/development.yml
6
+ - config/settings/test.yml
7
+ - config/settings/production.yml
8
+
9
+ - Remove OStruct and create a custom implementation (return proxy obj)
10
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
@@ -1,13 +1,15 @@
1
1
  if defined?(Rails::Railtie)
2
2
  module RailsConfig
3
3
  class Railtie < Rails::Railtie
4
- initializer :setup_rails_config do
5
- ::Settings = RailsConfig::SettingBuilder.load_files(
4
+
5
+ ActiveSupport.on_load :before_initialize, :yield => true do
6
+ ::Settings = RailsConfig.load_files(
6
7
  Rails.root.join("config", "settings.yml").to_s,
7
8
  Rails.root.join("config", "settings", "#{Rails.env}.yml").to_s,
8
9
  Rails.root.join("config", "environments", "#{Rails.env}.yml").to_s
9
10
  )
10
11
  end
12
+
11
13
  end
12
14
  end
13
15
  end
data/lib/rails_config.rb CHANGED
@@ -1,5 +1,65 @@
1
- require 'rails_config/vendor/deep_merge' unless defined?(DeepMerge)
2
1
  require 'pathname'
2
+ require 'ostruct'
3
+ require 'yaml'
4
+ require 'erb'
5
+
6
+ require 'rails_config/vendor/deep_merge' unless defined?(DeepMerge)
7
+
8
+ module RailsConfig
9
+
10
+ @@load_paths = []
11
+ def self.load_paths
12
+ @@load_paths
13
+ end
14
+
15
+ # Create a config object (OpenStruct) from a yaml file. If a second yaml file is given, then the sections of that file will overwrite the sections
16
+ # if the first file if they exist in the first file.
17
+ def self.load_files(*files)
18
+ config = OpenStruct.new
19
+
20
+ @@load_paths = [files].flatten.compact.uniq
21
+ # add singleton method to our Settings that reloads its settings from the load_paths
22
+ def config.reload!
23
+
24
+ conf = {}
25
+ RailsConfig.load_paths.to_a.each do |path|
26
+ file_conf = YAML.load(ERB.new(IO.read(path.to_s)).result) if path and File.exists?(path.to_s)
27
+ next unless file_conf
28
+
29
+ if conf.size > 0
30
+ DeepMerge.deep_merge!(file_conf, conf, :preserve_unmergeables => false)
31
+ else
32
+ conf = file_conf
33
+ end
34
+ end
35
+
36
+ # load all the new values into the openstruct
37
+ marshal_load(RailsConfig.convert(conf).marshal_dump)
38
+
39
+ return self
40
+ end
41
+
42
+ config.reload!
43
+ return config
44
+ end
45
+
46
+ # Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)
47
+ def self.convert(h) #:nodoc:
48
+ s = OpenStruct.new
49
+ h.each do |k, v|
50
+ s.new_ostruct_member(k)
51
+ if v.is_a?(Hash)
52
+ s.send( (k+'=').to_sym, convert(v))
53
+ elsif v.is_a?(Array)
54
+ converted_array = v.collect { |e| e.instance_of?(Hash) ? convert(e) : e }
55
+ s.send("#{k}=".to_sym, converted_array)
56
+ else
57
+ s.send("#{k}=".to_sym, v)
58
+ end
59
+ end
60
+ s
61
+ end
62
+ end
3
63
 
4
- require 'rails_config/setting_builder'
64
+ # add railtie
5
65
  require 'rails_config/railtie'
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+
3
+ describe RailsConfig do
4
+
5
+ it "should load a basic config file" do
6
+ config = RailsConfig.load_files(setting_path("settings.yml"))
7
+ config.size.should == 1
8
+ config.server.should == "google.com"
9
+ end
10
+
11
+ it "should load 2 basic config files" do
12
+ config = RailsConfig.load_files(setting_path("settings.yml"), setting_path("settings2.yml"))
13
+ config.size.should == 1
14
+ config.server.should == "google.com"
15
+ config.another.should == "something"
16
+ end
17
+
18
+ it "should load empty config for a missing file path" do
19
+ config = RailsConfig.load_files(setting_path("some_file_that_doesnt_exist.yml"))
20
+ config.should == OpenStruct.new
21
+ end
22
+
23
+ it "should load an empty config for multiple missing file paths" do
24
+ files = [setting_path("doesnt_exist1.yml"), setting_path("doesnt_exist2.yml")]
25
+ config = RailsConfig.load_files(files)
26
+ config.should == OpenStruct.new
27
+ end
28
+
29
+ it "should load empty config for an empty setting file" do
30
+ config = RailsConfig.load_files(setting_path("empty1.yml"))
31
+ config.should == OpenStruct.new
32
+ end
33
+
34
+ it "should load an empty config for multiple missing file paths" do
35
+ files = [setting_path("empty1.yml"), setting_path("empty2.yml")]
36
+ config = RailsConfig.load_files(files)
37
+ config.should == OpenStruct.new
38
+ end
39
+
40
+ it "should allow overrides" do
41
+ files = [setting_path("settings.yml"), setting_path("development.yml")]
42
+ config = RailsConfig.load_files(files)
43
+ config.server.should == "google.com"
44
+ config.size.should == 2
45
+ end
46
+
47
+ context "Nested Settings" do
48
+ let(:config) do
49
+ RailsConfig.load_files(setting_path("development.yml"))
50
+ end
51
+
52
+ it "should allow nested sections" do
53
+ config.section.size.should == 3
54
+ end
55
+
56
+ it "should allow configuration collections (arrays)" do
57
+ config.section.servers[0].name.should == "yahoo.com"
58
+ config.section.servers[1].name.should == "amazon.com"
59
+ end
60
+ end
61
+
62
+ context "Settings with ERB tags" do
63
+ let(:config) do
64
+ RailsConfig.load_files(setting_path("with_erb.yml"))
65
+ end
66
+
67
+ it "should evaluate ERB tags" do
68
+ config.computed.should == 6
69
+ end
70
+
71
+ it "should evaluated nested ERB tags" do
72
+ config.section.computed1.should == 1
73
+ config.section.computed2.should == 2
74
+ end
75
+ end
76
+
77
+ context "Deep Merging" do
78
+ let(:config) do
79
+ files = [setting_path("deep_merge/config1.yml"), setting_path("deep_merge/config2.yml")]
80
+ RailsConfig.load_files(files)
81
+ end
82
+
83
+ it "should merge hashes from multiple configs" do
84
+ config.inner.marshal_dump.keys.size.should == 3
85
+ config.inner2.inner2_inner.marshal_dump.keys.size.should == 3
86
+ end
87
+
88
+ it "should merge arrays from multiple configs" do
89
+ config.arraylist1.size.should == 6
90
+ config.arraylist2.inner.size.should == 6
91
+ end
92
+ end
93
+
94
+ context "Boolean Overrides" do
95
+ let(:config) do
96
+ files = [setting_path("bool_override/config1.yml"), setting_path("bool_override/config2.yml")]
97
+ RailsConfig.load_files(files)
98
+ end
99
+
100
+ it "should allow overriding of bool settings" do
101
+ config.override_bool.should == false
102
+ config.override_bool_opposite.should == true
103
+ end
104
+ end
105
+
106
+
107
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_config
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jacques Crocker
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-08 00:00:00 -07:00
18
+ date: 2010-08-10 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -45,16 +45,17 @@ extensions: []
45
45
  extra_rdoc_files:
46
46
  - LICENSE
47
47
  - README
48
+ - TODO
48
49
  files:
49
50
  - Gemfile
50
51
  - Gemfile.lock
51
52
  - LICENSE
52
53
  - README
53
54
  - Rakefile
55
+ - TODO
54
56
  - VERSION
55
57
  - lib/rails_config.rb
56
58
  - lib/rails_config/railtie.rb
57
- - lib/rails_config/setting_builder.rb
58
59
  - lib/rails_config/vendor/deep_merge.rb
59
60
  - spec/fixtures/bool_override/config1.yml
60
61
  - spec/fixtures/bool_override/config2.yml
@@ -68,7 +69,7 @@ files:
68
69
  - spec/fixtures/settings.yml
69
70
  - spec/fixtures/settings2.yml
70
71
  - spec/fixtures/with_erb.yml
71
- - spec/rails_config/setting_builder_spec.rb
72
+ - spec/rails_config_spec.rb
72
73
  - spec/spec_helper.rb
73
74
  has_rdoc: true
74
75
  homepage: http://github.com/railsjedi/rails_config
@@ -105,5 +106,5 @@ signing_key:
105
106
  specification_version: 3
106
107
  summary: provides an Settings for rails3 that reads config/settings.yml
107
108
  test_files:
108
- - spec/rails_config/setting_builder_spec.rb
109
+ - spec/rails_config_spec.rb
109
110
  - spec/spec_helper.rb
@@ -1,61 +0,0 @@
1
- require 'ostruct'
2
- require 'yaml'
3
- require 'erb'
4
-
5
- module RailsConfig
6
- module SettingBuilder
7
- @@load_paths = []
8
-
9
- # Create a config object (OpenStruct) from a yaml file. If a second yaml file is given, then the sections of that file will overwrite the sections
10
- # if the first file if they exist in the first file.
11
- def self.load_files(*files)
12
- config = OpenStruct.new
13
-
14
- @@load_paths = [files].flatten.compact.uniq
15
- # add singleton method to our Settings that reloads its settings from the load_paths
16
- def config.reload!
17
-
18
- conf = {}
19
- SettingBuilder.load_paths.to_a.each do |path|
20
- file_conf = YAML.load(ERB.new(IO.read(path.to_s)).result) if path and File.exists?(path.to_s)
21
- next unless file_conf
22
-
23
- if conf.size > 0
24
- DeepMerge.deep_merge!(file_conf, conf, :preserve_unmergeables => false)
25
- else
26
- conf = file_conf
27
- end
28
- end
29
-
30
- # load all the new values into the openstruct
31
- marshal_load(RailsConfig::SettingBuilder.convert(conf).marshal_dump)
32
-
33
- return self
34
- end
35
-
36
- config.reload!
37
- return config
38
- end
39
-
40
- def self.load_paths
41
- @@load_paths
42
- end
43
-
44
- # Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)
45
- def self.convert(h) #:nodoc:
46
- s = OpenStruct.new
47
- h.each do |k, v|
48
- s.new_ostruct_member(k)
49
- if v.is_a?(Hash)
50
- s.send( (k+'=').to_sym, convert(v))
51
- elsif v.is_a?(Array)
52
- converted_array = v.collect { |e| e.instance_of?(Hash) ? convert(e) : e }
53
- s.send("#{k}=".to_sym, converted_array)
54
- else
55
- s.send("#{k}=".to_sym, v)
56
- end
57
- end
58
- s
59
- end
60
- end
61
- end
@@ -1,109 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RailsConfig
4
- describe SettingBuilder do
5
-
6
- it "should load a basic config file" do
7
- config = SettingBuilder.load_files(setting_path("settings.yml"))
8
- config.size.should == 1
9
- config.server.should == "google.com"
10
- end
11
-
12
- it "should load 2 basic config files" do
13
- config = SettingBuilder.load_files(setting_path("settings.yml"), setting_path("settings2.yml"))
14
- config.size.should == 1
15
- config.server.should == "google.com"
16
- config.another.should == "something"
17
- end
18
-
19
- it "should load empty config for a missing file path" do
20
- config = SettingBuilder.load_files(setting_path("some_file_that_doesnt_exist.yml"))
21
- config.should == OpenStruct.new
22
- end
23
-
24
- it "should load an empty config for multiple missing file paths" do
25
- files = [setting_path("doesnt_exist1.yml"), setting_path("doesnt_exist2.yml")]
26
- config = SettingBuilder.load_files(files)
27
- config.should == OpenStruct.new
28
- end
29
-
30
- it "should load empty config for an empty setting file" do
31
- config = SettingBuilder.load_files(setting_path("empty1.yml"))
32
- config.should == OpenStruct.new
33
- end
34
-
35
- it "should load an empty config for multiple missing file paths" do
36
- files = [setting_path("empty1.yml"), setting_path("empty2.yml")]
37
- config = SettingBuilder.load_files(files)
38
- config.should == OpenStruct.new
39
- end
40
-
41
- it "should allow overrides" do
42
- files = [setting_path("settings.yml"), setting_path("development.yml")]
43
- config = SettingBuilder.load_files(files)
44
- config.server.should == "google.com"
45
- config.size.should == 2
46
- end
47
-
48
- context "Nested Settings" do
49
- let(:config) do
50
- SettingBuilder.load_files(setting_path("development.yml"))
51
- end
52
-
53
- it "should allow nested sections" do
54
- config.section.size.should == 3
55
- end
56
-
57
- it "should allow configuration collections (arrays)" do
58
- config.section.servers[0].name.should == "yahoo.com"
59
- config.section.servers[1].name.should == "amazon.com"
60
- end
61
- end
62
-
63
- context "Settings with ERB tags" do
64
- let(:config) do
65
- SettingBuilder.load_files(setting_path("with_erb.yml"))
66
- end
67
-
68
- it "should evaluate ERB tags" do
69
- config.computed.should == 6
70
- end
71
-
72
- it "should evaluated nested ERB tags" do
73
- config.section.computed1.should == 1
74
- config.section.computed2.should == 2
75
- end
76
- end
77
-
78
- context "Deep Merging" do
79
- let(:config) do
80
- files = [setting_path("deep_merge/config1.yml"), setting_path("deep_merge/config2.yml")]
81
- SettingBuilder.load_files(files)
82
- end
83
-
84
- it "should merge hashes from multiple configs" do
85
- config.inner.marshal_dump.keys.size.should == 3
86
- config.inner2.inner2_inner.marshal_dump.keys.size.should == 3
87
- end
88
-
89
- it "should merge arrays from multiple configs" do
90
- config.arraylist1.size.should == 6
91
- config.arraylist2.inner.size.should == 6
92
- end
93
- end
94
-
95
- context "Boolean Overrides" do
96
- let(:config) do
97
- files = [setting_path("bool_override/config1.yml"), setting_path("bool_override/config2.yml")]
98
- SettingBuilder.load_files(files)
99
- end
100
-
101
- it "should allow overriding of bool settings" do
102
- config.override_bool.should == false
103
- config.override_bool_opposite.should == true
104
- end
105
- end
106
-
107
-
108
- end
109
- end