configy 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Gabe Varela
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.
data/README.rdoc ADDED
@@ -0,0 +1,49 @@
1
+ == Configy
2
+
3
+ == Description
4
+
5
+ It allows to have a file (config/config.yml) with application configuration parameters.
6
+ It should have a "common" section with all parameters along with default values and can also
7
+ contain a section for each of the rails environments (development, test, production, or
8
+ your custom one). The values from the current environment section will override the values in the
9
+ "common" section.
10
+
11
+ If some developer needs his own specific values for his working copy, he can simply create
12
+ a config/config.local.yml file and override any value there, again having a "common" section
13
+ and a section for each environment.
14
+
15
+ Nothing is mandatory (files, sections) you just have what you really need. The files are parsed with ERB,
16
+ so they can contain some Ruby. It also checks for file modifications so you don't have to restart the server to pick up new values on production.
17
+
18
+ == Usage
19
+
20
+ An example of a config file (config.yml):
21
+
22
+ common:
23
+ admin_email: admin@domain.com
24
+ xml_rpc_url: http://domain.com:8000/
25
+ media_path: <%= RAILS_ROOT %>/tmp/media
26
+
27
+ development:
28
+ xml_rpc_url: http://localhost:8000/
29
+
30
+ test:
31
+ xml_rpc_url: http://localhost:8008/
32
+
33
+ In an initializer
34
+ Configy.create(:config)
35
+
36
+ Then, in the application you can use the config parameters like this:
37
+
38
+ Config.xml_rpc_url
39
+
40
+ So it means that you've got a Config object which holds all the configuration parameters defined.
41
+ It doesn't allow to change the values in the application code, BTW.
42
+
43
+ == Authors
44
+ Gabe Varela
45
+
46
+ The Configy gem based on the AppConfig plugin which was evolved from the original plugin by Eugene Bolshakov, eugene.bolshakov@gmail.com, http://www.taknado.com
47
+
48
+ The plugin is based on the idea described here:
49
+ http://kpumuk.info/ruby-on-rails/flexible-application-configuration-in-ruby-on-rails/lang-pref/en/
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "configy"
8
+ gem.summary = %Q{simple application configuration}
9
+ gem.email = "gvarela@gmail.com"
10
+ gem.homepage = "http://github.com/gvarela/configy"
11
+ gem.authors = ["Gabe Varela"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "configy #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.4
data/configy.gemspec ADDED
@@ -0,0 +1,50 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{configy}
8
+ s.version = "0.0.4"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Gabe Varela"]
12
+ s.date = %q{2009-10-08}
13
+ s.email = %q{gvarela@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "configy.gemspec",
26
+ "lib/configy.rb",
27
+ "lib/configy/configuration.rb",
28
+ "test/configy_test.rb",
29
+ "test/test_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/gvarela/configy}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{simple application configuration}
36
+ s.test_files = [
37
+ "test/configy_test.rb",
38
+ "test/test_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
data/lib/configy.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'configy/configuration'
2
+
@@ -0,0 +1,123 @@
1
+ require 'erb'
2
+
3
+ module Configy
4
+ @@load_path = nil
5
+ @@section = nil
6
+
7
+ def self.load_path=(val)
8
+ @@load_path = val
9
+ end
10
+
11
+ def self.section=(val)
12
+ @@section = val
13
+ end
14
+
15
+ def self.load_path
16
+ if @@load_path
17
+ return @@load_path
18
+ elsif defined? RAILS_ROOT
19
+ return "#{RAILS_ROOT}/config"
20
+ elsif defined? RACK_ROOT
21
+ return "#{RACK_ROOT}/config"
22
+ else
23
+ return 'config'
24
+ end
25
+ end
26
+
27
+ def self.section
28
+ if @@section
29
+ return @@section
30
+ elsif defined? RAILS_ENV
31
+ return RAILS_ENV
32
+ elsif defined? RACK_ENV
33
+ return RACK_ENV
34
+ else
35
+ return 'development'
36
+ end
37
+ end
38
+
39
+ def self.camelize(phrase)
40
+ camelized = phrase.gsub(/^[a-z]|\s+[a-z]|_+[a-z]|-+[a-z]/i) { |a| a.upcase }
41
+ camelized.gsub!(/\s/, '')
42
+ camelized.gsub!(/_/, '')
43
+ camelized.gsub!(/-/, '')
44
+ return camelized
45
+ end
46
+
47
+
48
+ def self.create(file)
49
+ instance_eval <<-"end;"
50
+ module ::#{camelize(file.to_s)}
51
+ class << self
52
+ @app_config
53
+ @file_mtime
54
+ @local_file_mtime
55
+
56
+ def file_path
57
+ File.join(Configy.load_path, "#{file.to_s}.yml")
58
+ end
59
+
60
+ def local_file_path
61
+ File.join(Configy.load_path, "#{file.to_s}.local.yml")
62
+ end
63
+
64
+ def method_missing(param)
65
+ build_config if can_build_config?
66
+ @app_config.send(param)
67
+ end
68
+
69
+ def can_build_config?
70
+ @app_config.nil? ||
71
+ @file_mtime && @file_mtime < File.mtime(file_path) ||
72
+ @local_file_mtime && @local_file_mtime < File.mtime(local_file_path)
73
+ end
74
+
75
+ def build_config
76
+ @app_config = Configy::Configuration.new
77
+ @app_config.use_file!(file_path)
78
+ @file_mtime = File.mtime(file_path)
79
+
80
+ if File.exists?(local_file_path)
81
+ @app_config.use_file!(local_file_path)
82
+ @local_file_mtime = File.mtime(local_file_path)
83
+ end
84
+
85
+ @app_config.use_section!(Configy.section)
86
+ end
87
+ end
88
+ end
89
+ end;
90
+ end
91
+
92
+ class Configuration
93
+
94
+ def initialize(file = nil)
95
+ @sections = {}
96
+ @params = {}
97
+ use_file!(file) if file
98
+ end
99
+
100
+ def use_file!(file)
101
+ begin
102
+ hash = YAML::load(ERB.new(IO.read(file)).result)
103
+ @sections.merge!(hash) {|key, old_val, new_val| (old_val || new_val).merge new_val }
104
+ @params.merge!(@sections['common'])
105
+ rescue; end
106
+ end
107
+
108
+ def use_section!(section)
109
+ @params.merge!(@sections[section.to_s]) if @sections.key?(section.to_s)
110
+ end
111
+
112
+ def method_missing(param)
113
+ param = param.to_s
114
+ if @params.key?(param)
115
+ @params[param]
116
+ else
117
+ raise "Invalid Configy::Configuration Parameter " + param
118
+ end
119
+ end
120
+
121
+ end
122
+
123
+ end
@@ -0,0 +1,124 @@
1
+ require 'test_helper'
2
+
3
+ class ConfigyTest < Test::Unit::TestCase
4
+ def setup
5
+ @dir = File.dirname(__FILE__) + "/"
6
+ @files = []
7
+ end
8
+
9
+ def test_should_read_from_yml
10
+ test_config 'common' => {'a' => '1', 'b' => '2'} do |file, hash|
11
+ config = Configy::Configuration.new(file)
12
+ assert_equal_with_hash config, hash['common']
13
+ end
14
+ end
15
+
16
+ def test_should_ignore_non_existent_file
17
+ config = Configy::Configuration.new('nonexsistentfile')
18
+ assert_not_nil config
19
+ end
20
+
21
+ def test_should_igonre_non_existent_section
22
+ test_config 'common' => {'a' => '1', 'b' => '2' } do |file, hash|
23
+ config = Configy::Configuration.new(file)
24
+ config.use_section!('nonexistentsection')
25
+ assert_equal config.b, '2'
26
+ end
27
+ end
28
+
29
+ def test_should_parse_yaml_with_erb
30
+ test_config 'common' => {'a' => '1', 'b' => '<%= 2 + 2 %>' } do |file, hash|
31
+ config = Configy::Configuration.new(file)
32
+ assert_equal config.b, 4
33
+ end
34
+ end
35
+
36
+ def test_should_override_params_with_given_section
37
+ test_config 'common' => {'a' => '1', 'b' => '<%= 2 + 2 %>'},
38
+ 'special' => {'a' => 1, 'b' => 5 } do |file, hash|
39
+ config = Configy::Configuration.new(file)
40
+ config.use_section!('special')
41
+ assert_equal_with_hash config, hash['special']
42
+ end
43
+ test_config 'common' => {'a' => '1', 'b' => '<%= 2 + 2 %>'},
44
+ 'special' => {'b' => 5 } do |file, hash|
45
+ config = Configy::Configuration.new(file)
46
+ config.use_section!('special')
47
+ assert_equal_with_hash config, {'a' => '1', 'b' => 5}
48
+ end
49
+ end
50
+
51
+ def test_should_ovveride_params_with_another_file
52
+ test_config({'common' => {'a' => '1', 'b' => '<%= 2 + 2 %>'},
53
+ 'special' => {'b' => 5 }}, 'config') do |file1, hash1|
54
+ test_config({'common' => {'a' => '2'},
55
+ 'special' => {'b' => 8 }}, 'config.local') do |file2, hash2|
56
+ config = Configy::Configuration.new(file1)
57
+ assert_equal_with_hash config, {'a' => '1', 'b' => 4}
58
+ config.use_file!(file2)
59
+ assert_equal_with_hash config, {'a' => '2', 'b' => 4}
60
+ end
61
+ end
62
+ end
63
+
64
+ def test_should_ovveride_params_with_another_file_and_use_proper_section
65
+ test_config({'common' => {'a' => '1', 'b' => '<%= 2 + 2 %>', 'c' => 2},
66
+ 'special' => {'b' => 5, 'd' => 6 },
67
+ 'extra' => {'f' => 4, 'a' => 8}}, 'config') do |file1, hash1|
68
+ test_config({'common' => {'a' => '2'},
69
+ 'special' => {'b' => 8 }}, 'config.local') do |file2, hash2|
70
+ config = Configy::Configuration.new
71
+ config.use_file!(file1)
72
+ config.use_file!(file2)
73
+ config.use_section!('special')
74
+ assert_equal_with_hash config, {'a' => '2', 'b' => 8, 'c' => 2, 'd' => 6}
75
+ end
76
+ end
77
+ end
78
+
79
+ def test_camelize
80
+ assert_equal 'Config', Configy.camelize('config')
81
+ assert_equal 'SomeConfig', Configy.camelize('some_config')
82
+ assert_equal 'SomeOtherConfig', Configy.camelize('some-other_config')
83
+ end
84
+
85
+ def test_should_create_configuration_via_create
86
+ Configy.load_path = @dir
87
+ test_config( {'common' => {'a' => '1', 'b' => '2' }}, 'some_config' ) do |file, hash|
88
+ Configy.create('some_config')
89
+ assert_equal SomeConfig.b, '2'
90
+ end
91
+ end
92
+
93
+ def teardown
94
+ begin
95
+ FileUtils.rm @files
96
+ rescue
97
+ end
98
+ end
99
+
100
+ protected
101
+
102
+ def file_path(file)
103
+ @dir + file + '.yml'
104
+ end
105
+
106
+ def create_config_file(file, hash)
107
+ file = @dir + file + '.yml'
108
+ @files << file
109
+ File.open(file, 'w') do |f|
110
+ f.write hash.to_yaml
111
+ end
112
+ end
113
+
114
+ def assert_equal_with_hash(config, hash)
115
+ hash.each do |key, value|
116
+ assert_equal config.send(key), value
117
+ end
118
+ end
119
+
120
+ def test_config(hash, file = 'config', &blk)
121
+ create_config_file file, hash
122
+ blk.call(file_path(file), hash)
123
+ end
124
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'configy'
7
+
8
+ class Test::Unit::TestCase
9
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Gabe Varela
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-08 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: gvarela@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - configy.gemspec
33
+ - lib/configy.rb
34
+ - lib/configy/configuration.rb
35
+ - test/configy_test.rb
36
+ - test/test_helper.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/gvarela/configy
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: simple application configuration
65
+ test_files:
66
+ - test/configy_test.rb
67
+ - test/test_helper.rb