rails_config 0.1.0 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,5 +1,6 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
+ gem "activesupport", ">= 3.0.0.rc"
3
4
  gem "rspec", ">= 2.0.0.beta.19"
4
5
  gem 'autotest'
5
6
  gem "growl-glue"
data/Gemfile.lock CHANGED
@@ -1,6 +1,7 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ activesupport (3.0.0.rc)
4
5
  autotest (4.3.2)
5
6
  columnize (0.3.1)
6
7
  diff-lcs (1.1.2)
@@ -24,6 +25,7 @@ PLATFORMS
24
25
  ruby
25
26
 
26
27
  DEPENDENCIES
28
+ activesupport (>= 3.0.0.rc)
27
29
  autotest
28
30
  growl-glue
29
31
  rspec (>= 2.0.0.beta.19)
data/README.md ADDED
@@ -0,0 +1,111 @@
1
+ ## Summary
2
+
3
+ RailsConfig helps you easily manage environment specific Rails settings in an easy and usable manner
4
+
5
+ ## Features
6
+
7
+ * simple YAML config files
8
+ * config files support ERB
9
+ * config files support inheritance
10
+ * access config information via convenient object member notation
11
+
12
+ ## Compatibility
13
+
14
+ * Rails 3.0
15
+
16
+ For older versions of Rails and other Ruby apps, use [AppConfig](http://github.com/fredwu/app_config).
17
+
18
+ ## Installing on Rails 3
19
+
20
+ Add this to your `Gemfile`:
21
+
22
+ gem "rails_config"
23
+
24
+ ## Customizing RailsConfig
25
+
26
+ You may customize the behavior of RailsConfig by generating an initializer file:
27
+
28
+ rails g rails_config
29
+
30
+ This will generate `config/initializers/rails_config.rb` with a set of default settings.
31
+
32
+ ## Accessing the Settings object
33
+
34
+ After installing this plugin, the Settings object will be global available. Entries are accessed via object member notation:
35
+
36
+ Settings.my_config_entry
37
+
38
+ Nested entries are supported:
39
+
40
+ Settings.my_section.some_entry
41
+
42
+ If you have set a different constant name for the object in the initializer file, use that instead.
43
+
44
+ ## Common config file
45
+
46
+ Config entries are compiled from
47
+
48
+ config/settings.yml
49
+ config/settings/#{environment}.yml
50
+ config/environments/#{environment}.yml
51
+
52
+ settings defined in files that are lower in the list override settings higher
53
+
54
+ ### Reloading config files
55
+
56
+ You can reload the Settings from file at any time by running Settings.reload!
57
+
58
+ ### Environment specific config files
59
+
60
+ You can have environment specific config files. Environment specific config entries take precedence over common config entries.
61
+
62
+ Example development environment config file:
63
+
64
+ #{Rails.root}/config/environments/development.yml
65
+
66
+ Example production environment config file:
67
+
68
+ #{Rails.root}/config/environments/production.yml
69
+
70
+ ## Embedded Ruby (ERB)
71
+
72
+ Embedded Ruby is allowed in the configuration files. See examples below.
73
+
74
+ ## Accessing Configuration Settings
75
+
76
+ Consider the two following config files.
77
+
78
+ # #{Rails.root}/config/settings.yml:
79
+ size: 1
80
+ server: google.com
81
+
82
+ # #{Rails.root}/config/environments/development.yml:
83
+ size: 2
84
+ computed: <%= 1 + 2 + 3 %>
85
+ section:
86
+ size: 3
87
+ servers: [ {name: yahoo.com}, {name: amazon.com} ]
88
+
89
+ Notice that the environment specific config entries overwrite the common entries.
90
+
91
+ Settings.size # => 2
92
+ Settings.server # => google.com
93
+
94
+ Notice the embedded Ruby.
95
+
96
+ Settings.computed # => 6
97
+
98
+ Notice that object member notation is maintained even in nested entries.
99
+
100
+ Settings.section.size # => 3
101
+
102
+ Notice array notation and object member notation is maintained.
103
+
104
+ Settings.section.servers[0].name # => yahoo.com
105
+ Settings.section.servers[1].name # => amazon.com
106
+
107
+ ## Authors
108
+
109
+ * [Jacques Crocker](http://github.com/railsjedi)
110
+ * [Fred Wu](http://github.com/fredwu)
111
+ * Inherited from [AppConfig](http://github.com/cjbottaro/app_config) by [Christopher J. Bottaro](http://github.com/cjbottaro)
data/Rakefile CHANGED
@@ -5,13 +5,13 @@ begin
5
5
  Jeweler::Tasks.new do |s|
6
6
  s.name = "rails_config"
7
7
  s.summary = "provides an Settings for rails3 that reads config/settings.yml"
8
- s.email = "railsjedi@gmail.com"
9
8
  s.homepage = "http://github.com/railsjedi/rails_config"
10
9
  s.description = "Provides an easy to use Application Configuration object"
11
10
  s.authors = ["Jacques Crocker", "Fred Wu"]
11
+ s.email = ["railsjedi@gmail.com", "ifredwu@gmail.com"]
12
12
  s.files = FileList["[A-Z]*", "{bin,generators,lib,spec}/**/*"]
13
13
 
14
- s.add_dependency 'active_support', ">=3.0.0.rc"
14
+ s.add_dependency 'activesupport', ">=3.0.0.rc"
15
15
  s.add_development_dependency 'rspec', ">=2.0.0.beta.19"
16
16
 
17
17
  end
data/TODO CHANGED
@@ -1,5 +1,3 @@
1
- - Remove OStruct and use ActiveSupport::OrderedOptions instead (adds dependency to active_support 3.0)
2
-
3
1
  - Add generator `rails_config:install` to create the default setting files
4
2
  - config/settings.yml
5
3
  - config/settings/development.yml
@@ -8,7 +6,9 @@
8
6
 
9
7
  - Add generator `rails_config:override` that creates an config/rails_config.rb initializer and disables the default initialzer
10
8
 
11
-
12
9
  DONE:
13
10
  - Add a Rails before_filter that triggers Settings.reload! for each development request
14
11
  - Allow user to customize the name of the global setting object (Settings)
12
+ - Abstract OpenStruct to RailsConfig::Options
13
+ - Add ability to define multiple sources (Start with YAMLSource)
14
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.3
data/lib/rails_config.rb CHANGED
@@ -1,18 +1,17 @@
1
1
  require 'active_support/core_ext/module/attribute_accessors'
2
- require 'pathname'
3
- require 'ostruct'
4
- require 'yaml'
5
- require 'erb'
2
+ require 'rails_config/options'
3
+
4
+ require "rails_config/sources/yaml_source"
6
5
 
7
6
  require 'rails_config/vendor/deep_merge' unless defined?(DeepMerge)
8
7
 
9
8
  module RailsConfig
10
9
  # ensures the setup only gets run once
11
10
  @@_ran_once = false
12
-
11
+
13
12
  mattr_accessor :const_name
14
13
  @@const_name = "Settings"
15
-
14
+
16
15
  def self.setup
17
16
  yield self if @@_ran_once == false
18
17
  @@_ran_once = true
@@ -23,53 +22,19 @@ module RailsConfig
23
22
  @@load_paths
24
23
  end
25
24
 
26
- # 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
25
+ # Create a populated Options instance from a yaml file. If a second yaml file is given, then the sections of that file will overwrite the sections
27
26
  # if the first file if they exist in the first file.
28
27
  def self.load_files(*files)
29
- config = OpenStruct.new
30
-
31
- @@load_paths = [files].flatten.compact.uniq
32
- # add singleton method to our Settings that reloads its settings from the load_paths
33
- def config.reload!
34
-
35
- conf = {}
36
- RailsConfig.load_paths.to_a.each do |path|
37
- file_conf = YAML.load(ERB.new(IO.read(path.to_s)).result) if path and File.exists?(path.to_s)
38
- next unless file_conf
28
+ config = Options.new
39
29
 
40
- if conf.size > 0
41
- DeepMerge.deep_merge!(file_conf, conf, :preserve_unmergeables => false)
42
- else
43
- conf = file_conf
44
- end
45
- end
46
-
47
- # load all the new values into the openstruct
48
- marshal_load(RailsConfig.convert(conf).marshal_dump)
49
-
50
- return self
30
+ # add yaml sources
31
+ [files].flatten.compact.uniq.each do |file|
32
+ config.add_source!(Sources::YAMLSource.new(file))
51
33
  end
52
-
53
- config.reload!
34
+ config.load!
54
35
  return config
55
36
  end
56
37
 
57
- # Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)
58
- def self.convert(h) #:nodoc:
59
- s = OpenStruct.new
60
- h.each do |k, v|
61
- s.new_ostruct_member(k)
62
- if v.is_a?(Hash)
63
- s.send( (k+'=').to_sym, convert(v))
64
- elsif v.is_a?(Array)
65
- converted_array = v.collect { |e| e.instance_of?(Hash) ? convert(e) : e }
66
- s.send("#{k}=".to_sym, converted_array)
67
- else
68
- s.send("#{k}=".to_sym, v)
69
- end
70
- end
71
- s
72
- end
73
38
  end
74
39
 
75
40
  # add railtie
@@ -0,0 +1,61 @@
1
+ require 'ostruct'
2
+ module RailsConfig
3
+ class Options < OpenStruct
4
+
5
+ def empty?
6
+ marshal_dump.empty?
7
+ end
8
+
9
+ def add_source!(source)
10
+ @config_sources ||= []
11
+ @config_sources << source
12
+ end
13
+
14
+ def load!
15
+ reload!
16
+ end
17
+
18
+ # look through all our sources and rebuild the configuration
19
+ def reload!
20
+ conf = {}
21
+ @config_sources.each do |source|
22
+ source_conf = source.load
23
+
24
+ if conf.empty?
25
+ conf = source_conf
26
+ else
27
+ DeepMerge.deep_merge!(source_conf, conf, :preserve_unmergeables => false)
28
+ end
29
+ end
30
+
31
+ # swap out the contents of the OStruct with a hash (need to recursively convert)
32
+ marshal_load(__convert(conf).marshal_dump)
33
+
34
+ return self
35
+ end
36
+
37
+ alias :load! :reload!
38
+
39
+ protected
40
+
41
+ # Recursively converts Hashes to Options (including Hashes inside Arrays)
42
+ def __convert(h) #:nodoc:
43
+ s = self.class.new
44
+ unless h.is_a?(Hash)
45
+ require 'ruby-debug';debugger
46
+ end
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
@@ -0,0 +1,24 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+
4
+ module RailsConfig
5
+ module Sources
6
+ class YAMLSource
7
+
8
+ attr_accessor :path
9
+
10
+ def initialize(path)
11
+ @path = path
12
+ end
13
+
14
+ # returns a config hash from the YML file
15
+ def load
16
+ if @path and File.exists?(@path.to_s)
17
+ result = YAML.load(ERB.new(IO.read(@path.to_s)).result)
18
+ end
19
+ result || {}
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -17,24 +17,24 @@ describe RailsConfig do
17
17
 
18
18
  it "should load empty config for a missing file path" do
19
19
  config = RailsConfig.load_files(setting_path("some_file_that_doesnt_exist.yml"))
20
- config.should == OpenStruct.new
20
+ config.should be_empty
21
21
  end
22
22
 
23
23
  it "should load an empty config for multiple missing file paths" do
24
24
  files = [setting_path("doesnt_exist1.yml"), setting_path("doesnt_exist2.yml")]
25
25
  config = RailsConfig.load_files(files)
26
- config.should == OpenStruct.new
26
+ config.should be_empty
27
27
  end
28
28
 
29
29
  it "should load empty config for an empty setting file" do
30
30
  config = RailsConfig.load_files(setting_path("empty1.yml"))
31
- config.should == OpenStruct.new
31
+ config.should be_empty
32
32
  end
33
33
 
34
34
  it "should load an empty config for multiple missing file paths" do
35
35
  files = [setting_path("empty1.yml"), setting_path("empty2.yml")]
36
36
  config = RailsConfig.load_files(files)
37
- config.should == OpenStruct.new
37
+ config.should be_empty
38
38
  end
39
39
 
40
40
  it "should allow overrides" do
@@ -107,7 +107,7 @@ describe RailsConfig do
107
107
  it "should have the default settings constant as 'Settings'" do
108
108
  RailsConfig.const_name.should == "Settings"
109
109
  end
110
-
110
+
111
111
  it "should be able to assign a different settings constant" do
112
112
  RailsConfig.setup{ |config| config.const_name = "Settings2" }
113
113
  RailsConfig.const_name.should == "Settings2"
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ module RailsConfig::Sources
4
+ describe YAMLSource do
5
+
6
+ it "should take a path as initializer" do
7
+ source = YAMLSource.new "somepath"
8
+ source.path.should == "somepath"
9
+ end
10
+
11
+ context "basic yml file" do
12
+ let(:source) do
13
+ YAMLSource.new setting_path("development.yml")
14
+ end
15
+
16
+ it "should properly read the settings" do
17
+ results = source.load
18
+ results["size"].should == 2
19
+ end
20
+
21
+ it "should properly read nested settings" do
22
+ results = source.load
23
+ results["section"]["size"].should == 3
24
+ results["section"]["servers"].should be_an(Array)
25
+ results["section"]["servers"].size.should == 2
26
+ end
27
+ end
28
+
29
+ context "yml file with erb tags" do
30
+ let(:source) do
31
+ YAMLSource.new setting_path("with_erb.yml")
32
+ end
33
+
34
+ it "should properly evaluate the erb" do
35
+ results = source.load
36
+ results["computed"].should == 6
37
+ end
38
+
39
+ it "should properly evaluate the nested erb settings" do
40
+ results = source.load
41
+ results["section"]["computed1"].should == 1
42
+ results["section"]["computed2"].should == 2
43
+ end
44
+ end
45
+
46
+ context "missing yml file" do
47
+ let(:source) do
48
+ YAMLSource.new "somewhere_that_doesnt_exist.yml"
49
+ end
50
+
51
+ it "should return an empty hash" do
52
+ results = source.load
53
+ results.should == {}
54
+ end
55
+ end
56
+
57
+ context "blank yml file" do
58
+ let(:source) do
59
+ YAMLSource.new setting_path("empty1.yml")
60
+ end
61
+
62
+ it "should return an empty hash" do
63
+ results = source.load
64
+ results.should == {}
65
+ end
66
+ end
67
+
68
+ end
69
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rails_config'
2
+ require 'pathname'
2
3
  require "bundler"
3
4
  Bundler.setup
4
5
 
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: 27
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jacques Crocker
@@ -20,7 +20,7 @@ date: 2010-08-10 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
23
- name: active_support
23
+ name: activesupport
24
24
  prerelease: false
25
25
  requirement: &id001 !ruby/object:Gem::Requirement
26
26
  none: false
@@ -55,27 +55,31 @@ dependencies:
55
55
  type: :development
56
56
  version_requirements: *id002
57
57
  description: Provides an easy to use Application Configuration object
58
- email: railsjedi@gmail.com
58
+ email:
59
+ - railsjedi@gmail.com
60
+ - ifredwu@gmail.com
59
61
  executables: []
60
62
 
61
63
  extensions: []
62
64
 
63
65
  extra_rdoc_files:
64
66
  - LICENSE
65
- - README
67
+ - README.md
66
68
  - TODO
67
69
  files:
68
70
  - Gemfile
69
71
  - Gemfile.lock
70
72
  - LICENSE
71
- - README
73
+ - README.md
72
74
  - Rakefile
73
75
  - TODO
74
76
  - VERSION
75
77
  - lib/generators/rails_config_generator.rb
76
78
  - lib/generators/templates/rails_config.rb
77
79
  - lib/rails_config.rb
80
+ - lib/rails_config/options.rb
78
81
  - lib/rails_config/railtie.rb
82
+ - lib/rails_config/sources/yaml_source.rb
79
83
  - lib/rails_config/vendor/deep_merge.rb
80
84
  - spec/fixtures/bool_override/config1.yml
81
85
  - spec/fixtures/bool_override/config2.yml
@@ -90,6 +94,7 @@ files:
90
94
  - spec/fixtures/settings2.yml
91
95
  - spec/fixtures/with_erb.yml
92
96
  - spec/rails_config_spec.rb
97
+ - spec/sources/yaml_source_spec.rb
93
98
  - spec/spec_helper.rb
94
99
  has_rdoc: true
95
100
  homepage: http://github.com/railsjedi/rails_config
@@ -127,4 +132,5 @@ specification_version: 3
127
132
  summary: provides an Settings for rails3 that reads config/settings.yml
128
133
  test_files:
129
134
  - spec/rails_config_spec.rb
135
+ - spec/sources/yaml_source_spec.rb
130
136
  - spec/spec_helper.rb
data/README DELETED
@@ -1,72 +0,0 @@
1
- == Summary
2
- RailsConfig helps you easily manage environment specific Rails settings in an easy and usable manner
3
-
4
- == Author
5
- Jacques Crocker
6
- Modified from Original Project (AppConfig by Christopher J. Bottaro)
7
-
8
- === Compatibility
9
- Rails 3.0
10
-
11
- === Installing on Rails 3
12
-
13
- add this to your Gemfile
14
-
15
- gem "rails_config"
16
-
17
- === Accessing the Settings object
18
- After installing this plugin, the Settings object will be global available. Entries are accessed via object member notation:
19
- Settings.my_config_entry
20
- Nested entries are supported:
21
- Settings.my_section.some_entry
22
-
23
- === Common config file
24
- Config entries are compiled from
25
- config/settings.yml
26
- config/settings/#{environment}.yml
27
- config/environments/#{environment}.yml
28
-
29
- settings defined in files that are lower in the list override settings higher
30
-
31
- === Reloading config files
32
- You can reload the Settings from file at any time by running Settings.reload!
33
-
34
- === Environment specific config files
35
- You can have environment specific config files. Environment specific config entries take precedence over common config entries.
36
-
37
- Example development environment config file:
38
- #{Rails.root}/config/environments/development.yml
39
-
40
- Example production environment config file:
41
- #{Rails.root}/config/environments/production.yml
42
-
43
- === Embedded Ruby (ERB)
44
- Embedded Ruby is allowed in the configuration files. See examples below.
45
-
46
- === Accessing Configuration Settings
47
- Consider the two following config files.
48
-
49
- #{Rails.root}/config/settings.yml:
50
- size: 1
51
- server: google.com
52
-
53
- #{Rails.root}/config/environments/development.yml:
54
- size: 2
55
- computed: <%= 1 + 2 + 3 %>
56
- section:
57
- size: 3
58
- servers: [ {name: yahoo.com}, {name: amazon.com} ]
59
-
60
- Notice that the environment specific config entries overwrite the common entries.
61
- Settings.size -> 2
62
- Settings.server -> google.com
63
-
64
- Notice the embedded Ruby.
65
- Settings.computed -> 6
66
-
67
- Notice that object member notation is maintained even in nested entries.
68
- Settings.section.size -> 3
69
-
70
- Notice array notation and object member notation is maintained.
71
- Settings.section.servers[0].name -> yahoo.com
72
- Settings.section.servers[1].name -> amazon.com