config-file-loader 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
@@ -0,0 +1,2 @@
1
+ 0.1.0
2
+ Initial Version
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 kbrock
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ = config-file-loader
2
+
3
+ http://railscasts.com/episodes/85-yaml-configuration-file
4
+
5
+ * assumes yaml files (which can contain erb) with the extension yml
6
+ * assumes yaml file is in config (unless an absolute path is given)
7
+
8
+ == file format
9
+
10
+ # development:
11
+ # attribute1: value1
12
+ # attribute2:
13
+ # attribute2a: value2a
14
+ # attribute2b: value2b
15
+ # attribute3: <%= ENV['USER'] %>
16
+ # test:
17
+ # attribute1: value1b
18
+ # ...
19
+
20
+ == usage
21
+
22
+ APP_CONFIG = ConfigFileLoader.load(
23
+ 'app_config.yml',
24
+ 'app_config_local.yml',
25
+ '/opt/local/config/app_config_override.yml'
26
+ )
27
+
28
+
29
+ == Note on Patches/Pull Requests
30
+
31
+ * Fork the project.
32
+ * Make your feature addition or bug fix.
33
+ * Add tests for it. This is important so I don't break it in a
34
+ future version unintentionally.
35
+ * Commit, do not mess with rakefile, version, or history.
36
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
37
+ * Send me a pull request. Bonus points for topic branches.
38
+
39
+ == Copyright
40
+
41
+ Copyright (c) 2010 kbrock. See LICENSE for details.
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "config-file-loader"
8
+ gem.summary = %Q{Load config files from disk}
9
+ gem.description = %Q{simple way to load erb yaml config files}
10
+ gem.email = "keenan@thebrocks.net"
11
+ gem.homepage = "http://github.com/kbrock/config-file-loader"
12
+ gem.authors = ["kbrock"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "config-file-loader #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,116 @@
1
+ require 'erb'
2
+ require 'yaml'
3
+ class ConfigFileLoader
4
+
5
+ ## base directory for configuration
6
+ ## typically in rails root / config
7
+ def self.base
8
+ if defined?(@@base)
9
+ @@base
10
+ elsif defined?(Rails)
11
+ @@base||="#{Rails.root}/config/"
12
+ else
13
+ @@base||=File.expand_path(File.dirname(__FILE__)) + "/config/"
14
+ end
15
+ end
16
+
17
+ def self.base=(val)
18
+ @@base=val
19
+ end
20
+
21
+
22
+ def self.env
23
+ if defined?(@@env)
24
+ @@env
25
+ elsif defined?(RAILS_ENV)
26
+ RAILS_ENV
27
+ end
28
+ #else nil
29
+ end
30
+
31
+ def self.env=(val)
32
+ @@env=val
33
+ end
34
+
35
+ def self.load_no_symbolize_keys(*files)
36
+ load_files(*files)
37
+ end
38
+
39
+ def self.load(*files, &block)
40
+ block=lambda() { |hash| self.deep_symbolize_keys(hash) } unless block_given?
41
+ load_files(*files,&block)
42
+ end
43
+
44
+ def self.load_files(*files, &block)
45
+ raw_hash = nil
46
+ files.each do |file_name|
47
+ second_hash = contents(file_name)
48
+ second_hash = yield second_hash if block_given?
49
+ raw_hash=deep_merge(raw_hash,second_hash)
50
+ end
51
+ raw_hash
52
+ end
53
+
54
+ #add .yml or rails config root if necessary
55
+ def self.fix_name(name)
56
+ return unless name.is_a?(String)
57
+ #if it is not relative ('./file') or absolute ('/a/b/file') - tack on config directory
58
+ name = "#{self.base}/#{name}" unless ['.','/'].include?(name[0,1])
59
+ name+=".yml" unless name.include?('yml') || name.include?('cfg')
60
+ name
61
+ end
62
+
63
+ def self.contents(file_name)
64
+ if file_name.nil?
65
+ nil
66
+ elsif (file_name.is_a?(Hash))
67
+ file_name
68
+ else
69
+ file_name=fix_name(file_name)
70
+ load_erb_yaml(file_name) if File.exist?(file_name)
71
+ end
72
+ end
73
+
74
+ def self.load_erb_yaml(file_name)
75
+ raw_config = ERB.new(File.read(file_name)).result
76
+ yaml_config = YAML.load(raw_config)
77
+ yaml_config = yaml_config[env] || yaml_config[env.to_s] if env
78
+ yaml_config
79
+ end
80
+
81
+ # def self.deep_merge(target, extra)
82
+ # (target && extra) ? target.deep_merge(extra) : (target || extra)
83
+ # end
84
+
85
+ # http://snippets.dzone.com/posts/show/4706
86
+ # http://www.francisfish.com/deep_merge_a_ruby_hash_the_joys_of_recursion.htm
87
+ # rails already defines this
88
+ # return target.deep_merge(extra) if target.respond_to?(:deep_merge)
89
+ def self.deep_merge(target, extra)
90
+ # return something if either is nil
91
+ return target || extra if target.nil? || extra.nil?
92
+
93
+ extra.each_key do |k1|
94
+ # if the key exists in both sides and they are both "hashes", merge
95
+ # could move this block to the first line, but may want special logic for lists
96
+ if target.key?(k1) && target[k1].respond_to?(:each_key) && extra[k1].respond_to?(:each_key)
97
+ target[k1] = deep_merge(target[k1],extra[k1])
98
+ else
99
+ target[k1] = extra[k1] #dup?
100
+ end
101
+ end
102
+ target
103
+ end
104
+
105
+ def self.deep_symbolize_keys(hash)
106
+ return hash unless hash.is_a?(Hash)
107
+
108
+ #hash.symbolize_keys
109
+ hash.inject({}) do |options, (key, value)|
110
+ value=deep_symbolize_keys(value) if value.is_a?(Hash)
111
+ options[(key.to_sym rescue key) || key] = value
112
+ options
113
+ end
114
+ #ret inject value
115
+ end
116
+ end
@@ -0,0 +1,9 @@
1
+ ---
2
+ a:
3
+ aa:
4
+ aaa: 111
5
+ c:
6
+ aa: 11
7
+ cc:
8
+ ccc: 111
9
+ aaa: 111
@@ -0,0 +1,9 @@
1
+ ---
2
+ b:
3
+ bb:
4
+ bbb: 222
5
+ c:
6
+ bb: 22
7
+ cc:
8
+ ccc: 222
9
+ bbb: 222
@@ -0,0 +1,6 @@
1
+ development:
2
+ fun: yes
3
+ uptime_percentage: 5
4
+ production:
5
+ fun: no
6
+ uptime_percentage: 99.9
@@ -0,0 +1,68 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ #require '/spec_helper'
3
+
4
+ describe ConfigFileLoader do
5
+ context "load" do
6
+ it "should handle no files" do
7
+ l().should == nil
8
+ end
9
+
10
+ it "should load empty hash" do
11
+ l({}).should == {}
12
+ end
13
+
14
+ it "should load 1 hash with symbold" do
15
+ l({:a => 1}).should == {:a => 1}
16
+ end
17
+
18
+ it "should load 1 hash symbolize" do
19
+ l({"a" => 1}).should == {:a => 1}
20
+ end
21
+
22
+ it "should load 1 hash not symbolize" do
23
+ ConfigFileLoader.load_no_symbolize_keys({"a" => 1}).should == {"a" => 1}
24
+ end
25
+
26
+ it "should load hash using custom hash 'fixing' function" do
27
+ ret=l({"c" => 1}, {:c => 2}) do |hash|
28
+ hash.inject({}) do |options, (key, value)|
29
+ options[key.to_s.upcase]=value
30
+ options
31
+ end
32
+ end
33
+ ret.should == {"C" => 2}
34
+ end
35
+
36
+ it "should handle file not found" do
37
+ l({},'bogus.yml').should == {}
38
+ end
39
+
40
+ it "should load 1 file" do
41
+ l('a').should ==
42
+ {:a=>{:aa=>{:aaa=>111}}, :c=>{:cc=>{:aaa=>111, :ccc=>111}, :aa=>11}}
43
+ end
44
+
45
+ it "should load 2 files and merge" do
46
+ l('a','b').should ==
47
+ {:b=>{:bb=>{:bbb=>222}}, :c=>{:aa=>11, :bb=>22, :cc=>{:ccc=>222, :bbb=>222, :aaa=>111}}, :a=>{:aa=>{:aaa=>111}}}
48
+ end
49
+
50
+ context "dev_prod" do
51
+ it "should load dev properties" do
52
+ ConfigFileLoader.env='development'
53
+ l('dev_prod').should == {:fun => true, :uptime_percentage => 5}
54
+ ConfigFileLoader.env=nil #back to testing default
55
+ end
56
+
57
+ it "should load prod properties" do
58
+ ConfigFileLoader.env='production'
59
+ l('dev_prod').should == {:fun => false, :uptime_percentage => 99.9}
60
+ ConfigFileLoader.env=nil #back to testing default
61
+ end
62
+ end
63
+ end
64
+
65
+ def l(*files,&block)
66
+ ConfigFileLoader.load(*files,&block)
67
+ end
68
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ #require '/spec_helper'
3
+
4
+ describe ConfigFileLoader do
5
+ context "deep merge" do
6
+ it "should merge with no target" do
7
+ dm({},{}).should == {}
8
+ end
9
+
10
+ it "should merge with no target 2" do
11
+ dm(nil,nil).should == nil
12
+ end
13
+
14
+ it "should merge with source" do
15
+ dm({:a=>1},{}).should == {:a=>1}
16
+ end
17
+
18
+ it "should merge with source 2" do
19
+ dm({:a=>1},nil).should == {:a=>1}
20
+ end
21
+
22
+ it "should merge with target" do
23
+ dm({},{:b=>1}).should == {:b=>1}
24
+ end
25
+
26
+ it "should merge with target 2" do
27
+ dm(nil,{:b=>1}).should == {:b=>1}
28
+ end
29
+
30
+ it "should take override over source" do
31
+ dm({:c => 1},{:c=>2}).should == {:c=>2}
32
+ end
33
+
34
+ context "in a hash" do
35
+ it "should merge with source" do
36
+ dm({:c =>{:aa=>1}},{}).should == {:c => {:aa=>1}}
37
+ end
38
+
39
+ it "should merge with target" do
40
+ dm({},{:c=>{:bb=>1}}).should == {:c => {:bb=>1}}
41
+ end
42
+
43
+ it "should merge both" do
44
+ dm({:c=>{:aa => 1}},{:c =>{:bb => 2}}).should == {:c=>{:aa=>1, :bb => 2}}
45
+ end
46
+
47
+ it "should take override over source" do
48
+ dm({:c => {:cc=>1}},{:c=>{:cc=>2}}).should == {:c=>{:cc=>2}}
49
+ end
50
+ end
51
+
52
+ context "half hash" do
53
+ it "should replace single value with hash" do
54
+ dm({:c => :a},{:c => {:b => 1}}).should == {:c => {:b => 1}}
55
+ end
56
+
57
+ it "should replace hash with single value" do
58
+ dm({:c => {:a => 1}}, {:c => :b}).should == {:c => :b}
59
+ end
60
+ end
61
+ end
62
+
63
+ def dm(a,b)
64
+ ConfigFileLoader.deep_merge(a,b)
65
+ end
66
+ end
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ #require '/spec_helper'
3
+
4
+ describe ConfigFileLoader do
5
+ context "fix file name" do
6
+ it "should handle fienames with a slash and yml" do
7
+ fn("/a.yml").should == "/a.yml"
8
+ end
9
+
10
+ it "should handle fienames with a slash and cfg" do
11
+ fn("/a.cfg").should == "/a.cfg"
12
+ end
13
+
14
+ it "should add yml to the filename" do
15
+ fn("/a").should == "/a.yml"
16
+ end
17
+
18
+ it "should add directory to filename" do
19
+ rslt=fn("a.yml")
20
+ rslt.index('/').should == 0 #starts with slash
21
+ rslt.index('a.yml').should == rslt.length - 5
22
+ rslt.index('/config/').should_not be_nil
23
+ end
24
+
25
+ it "should add directory and yml to filename" do
26
+ rslt=fn("a")
27
+ rslt.index('/').should == 0 #starts with slash
28
+ rslt.index('a.yml').should == rslt.length - 5
29
+ end
30
+
31
+ it "should leave directory alone if it starts with a ." do
32
+ fn("./abc.yml").should == "./abc.yml"
33
+ end
34
+ end
35
+
36
+ def fn(a)
37
+ ConfigFileLoader.fix_name(a)
38
+ end
39
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'config_file_loader'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+ ConfigFileLoader.base = File.join(File.dirname(__FILE__),'config/')
9
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ #require '/spec_helper'
3
+
4
+ describe ConfigFileLoader do
5
+ context "symbolize keys" do
6
+ it "should handle nil" do
7
+ sk(nil).should == nil
8
+ end
9
+
10
+ it "should handle string key" do
11
+ sk({"a" => 1}).should == {:a => 1}
12
+ end
13
+ it "should handle symbol key" do
14
+ sk({:a => 1}).should == {:a => 1}
15
+ end
16
+ it "should handle nested strings" do
17
+ sk({"a" => {"aa" => 1}}).should == {:a => {:aa => 1}}
18
+ end
19
+ end
20
+
21
+ def sk(a)
22
+ ConfigFileLoader.deep_symbolize_keys(a)
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: config-file-loader
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - kbrock
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-21 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: simple way to load erb yaml config files
38
+ email: keenan@thebrocks.net
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - .document
48
+ - .gitignore
49
+ - CHANGELOG
50
+ - LICENSE
51
+ - README.rdoc
52
+ - Rakefile
53
+ - VERSION
54
+ - lib/config_file_loader.rb
55
+ - spec/config/a.yml
56
+ - spec/config/b.yml
57
+ - spec/config/dev_prod.yml
58
+ - spec/config_file_loader_spec.rb
59
+ - spec/deep_merge_spec.rb
60
+ - spec/filename_spec.rb
61
+ - spec/spec.opts
62
+ - spec/spec_helper.rb
63
+ - spec/symbolize_keys_spec.rb
64
+ has_rdoc: true
65
+ homepage: http://github.com/kbrock/config-file-loader
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options:
70
+ - --charset=UTF-8
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.7
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Load config files from disk
98
+ test_files:
99
+ - spec/config_file_loader_spec.rb
100
+ - spec/deep_merge_spec.rb
101
+ - spec/filename_spec.rb
102
+ - spec/spec_helper.rb
103
+ - spec/symbolize_keys_spec.rb