brodock-config-file-loader 0.1.1

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.
@@ -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,75 @@
1
+ # config-file-loader
2
+
3
+ Inspired/ ripped off from ryan bate's great railscast
4
+
5
+ [http://railscasts.com/episodes/85-yaml-configuration-file]()
6
+
7
+ ## features
8
+
9
+ * configuration file is in yaml
10
+ * supports erb in yaml file
11
+ * assumes/adds yml extension
12
+ * assumes file is in RAILS_ROOT/config
13
+ * can override config file location per file (using an absolute or relative path) or globally
14
+ * allows custom override files, so for local developers or production per machine values
15
+ * different variable values for different "Rails" environments.
16
+ * works in Rails, but also in non rails-projects.
17
+
18
+ ## planned features
19
+
20
+ * monitoring files and updating upon file changes using fsevents.
21
+ * database based config values
22
+ * memcache config values
23
+ * ui to see and edit variables (will save to db/memcache - probably not file)
24
+
25
+ ## file format
26
+
27
+ supports aliases, erb, and any yaml construct.
28
+
29
+ defaults: &defaults
30
+ d1: v1
31
+ d2: v2
32
+ development:
33
+ <<: *defaults
34
+ attribute1: value1
35
+ attribute2:
36
+ attribute2a: value2a
37
+ attribute2b: value2b
38
+ attribute3: <%= ENV['USER'] %>
39
+ test:
40
+ <<: *defaults
41
+ attribute1: value1b
42
+ ...
43
+
44
+ override file:
45
+
46
+ development:
47
+ attribute2:
48
+ attribute2a: replaced-value2a
49
+
50
+ If you are nesting hashes within a file, remember that yaml will replace the whole hash.
51
+
52
+ But a separate override file will merge in values. So in development, only attribute2a will be replaced.
53
+
54
+ ## usage
55
+
56
+ APP_CONFIG = ConfigFileLoader.load(
57
+ 'app_config.yml',
58
+ 'app_config_local.yml',
59
+ '/opt/local/config/app_config_override.yml'
60
+ )
61
+
62
+
63
+ ## Note on Patches/Pull Requests
64
+
65
+ * Fork the project.
66
+ * Make your feature addition or bug fix.
67
+ * Add tests for it. This is important so I don't break it in a
68
+ future version unintentionally.
69
+ * Commit, do not mess with rakefile, version, or history.
70
+ (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)
71
+ * Send me a pull request. Bonus points for topic branches.
72
+
73
+ ## Copyright
74
+
75
+ 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 = "brodock-config-file-loader"
8
+ gem.summary = %Q{Load config files from disk}
9
+ gem.description = %Q{simple way to load erb yaml config files. based upon http://railscasts.com/episodes/85-yaml-configuration-file with OpenStruct support}
10
+ gem.email = "brodock@gmail.com"
11
+ gem.homepage = "http://github.com/brodock/config-file-loader"
12
+ gem.authors = ["kbrock", "brodock"]
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 = "brodock-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.1
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'config_file_loader'
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/config_file_loader'
@@ -0,0 +1,121 @@
1
+ require 'erb'
2
+ require 'yaml'
3
+ require 'ostruct'
4
+ class ConfigFileLoader
5
+
6
+ ## base directory for configuration
7
+ ## typically in rails root / config
8
+ def self.base
9
+ if defined?(@@base)
10
+ @@base
11
+ elsif defined?(Rails)
12
+ @@base||="#{Rails.root}/config/"
13
+ else
14
+ @@base||=File.expand_path(File.dirname(__FILE__)) + "/config/"
15
+ end
16
+ end
17
+
18
+ def self.base=(val)
19
+ @@base=val
20
+ end
21
+
22
+
23
+ def self.env
24
+ if defined?(@@env)
25
+ @@env
26
+ elsif defined?(RAILS_ENV)
27
+ RAILS_ENV
28
+ end
29
+ #else nil
30
+ end
31
+
32
+ def self.env=(val)
33
+ @@env=val
34
+ end
35
+
36
+ def self.load_no_symbolize_keys(*files)
37
+ load_files(*files)
38
+ end
39
+
40
+ def self.load_as_struct(*files)
41
+ OpenStruct.new(load_files(*files))
42
+ end
43
+
44
+ def self.load(*files, &block)
45
+ block=lambda() { |hash| self.deep_symbolize_keys(hash) } unless block_given?
46
+ load_files(*files,&block)
47
+ end
48
+
49
+ def self.load_files(*files, &block)
50
+ raw_hash = nil
51
+ files.each do |file_name|
52
+ second_hash = contents(file_name)
53
+ second_hash = yield second_hash if block_given?
54
+ raw_hash=deep_merge(raw_hash,second_hash)
55
+ end
56
+ raw_hash
57
+ end
58
+
59
+ #add .yml or rails config root if necessary
60
+ def self.fix_name(name)
61
+ return unless name.is_a?(String)
62
+ #if it is not relative ('./file') or absolute ('/a/b/file') - tack on config directory
63
+ name = "#{self.base}/#{name}" unless ['.','/','~'].include?(name[0,1])
64
+ name+=".yml" unless name.include?('yml') || name.include?('cfg')
65
+ name
66
+ end
67
+
68
+ def self.contents(file_name)
69
+ if file_name.nil?
70
+ nil
71
+ elsif (file_name.is_a?(Hash))
72
+ file_name
73
+ else
74
+ file_name=fix_name(file_name)
75
+ load_erb_yaml(file_name) if File.exist?(file_name)
76
+ end
77
+ end
78
+
79
+ def self.load_erb_yaml(file_name)
80
+ raw_config = ERB.new(File.read(file_name)).result
81
+ yaml_config = YAML.load(raw_config)
82
+ yaml_config = yaml_config[env] || yaml_config[env.to_s] if env
83
+ yaml_config
84
+ end
85
+
86
+ # def self.deep_merge(target, extra)
87
+ # (target && extra) ? target.deep_merge(extra) : (target || extra)
88
+ # end
89
+
90
+ # http://snippets.dzone.com/posts/show/4706
91
+ # http://www.francisfish.com/deep_merge_a_ruby_hash_the_joys_of_recursion.htm
92
+ # rails already defines this
93
+ # return target.deep_merge(extra) if target.respond_to?(:deep_merge)
94
+ def self.deep_merge(target, extra)
95
+ # return something if either is nil
96
+ return target || extra if target.nil? || extra.nil?
97
+
98
+ extra.each_key do |k1|
99
+ # if the key exists in both sides and they are both "hashes", merge
100
+ # could move this block to the first line, but may want special logic for lists
101
+ if target.key?(k1) && target[k1].respond_to?(:each_key) && extra[k1].respond_to?(:each_key)
102
+ target[k1] = deep_merge(target[k1],extra[k1])
103
+ else
104
+ target[k1] = extra[k1] #dup?
105
+ end
106
+ end
107
+ target
108
+ end
109
+
110
+ def self.deep_symbolize_keys(hash)
111
+ return hash unless hash.is_a?(Hash)
112
+
113
+ #hash.symbolize_keys
114
+ hash.inject({}) do |options, (key, value)|
115
+ value=deep_symbolize_keys(value) if value.is_a?(Hash)
116
+ options[(key.to_sym rescue key) || key] = value
117
+ options
118
+ end
119
+ #ret inject value
120
+ end
121
+ end
@@ -0,0 +1 @@
1
+ require 'config_file_loader'
@@ -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,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brodock-config-file-loader
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - kbrock
13
+ - brodock
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-20 00:00:00 -03: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
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 2
31
+ - 9
32
+ version: 1.2.9
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: simple way to load erb yaml config files. based upon http://railscasts.com/episodes/85-yaml-configuration-file with OpenStruct support
36
+ email: brodock@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.markdown
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - CHANGELOG
48
+ - LICENSE
49
+ - README.markdown
50
+ - Rakefile
51
+ - VERSION
52
+ - init.rb
53
+ - lib/config-file-loader.rb
54
+ - lib/config_file_loader.rb
55
+ - rails/init.rb
56
+ - spec/config/a.yml
57
+ - spec/config/b.yml
58
+ - spec/config/dev_prod.yml
59
+ - spec/config_file_loader_spec.rb
60
+ - spec/deep_merge_spec.rb
61
+ - spec/filename_spec.rb
62
+ - spec/spec.opts
63
+ - spec/spec_helper.rb
64
+ - spec/symbolize_keys_spec.rb
65
+ has_rdoc: true
66
+ homepage: http://github.com/brodock/config-file-loader
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --charset=UTF-8
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.3.6
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Load config files from disk
95
+ test_files:
96
+ - spec/config_file_loader_spec.rb
97
+ - spec/deep_merge_spec.rb
98
+ - spec/filename_spec.rb
99
+ - spec/spec_helper.rb
100
+ - spec/symbolize_keys_spec.rb