configloader 0.1.0

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,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
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Vinicius Teles
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,66 @@
1
+ = configloader
2
+
3
+ If you need to create a custom config file for you Rails project, ConfigLoader will help you to load the contents of this file.
4
+
5
+ == How to install the gem
6
+
7
+ gem install configloader
8
+
9
+ == Usage
10
+
11
+ ConfigLoader.load(file_name, running_env, project_root)
12
+
13
+ Let's assume that you have the special file config/database.yml in your Rails project.
14
+ In this example, you are using CouchDB as your database. This file has the content below:
15
+
16
+ development:
17
+ server: localhost
18
+ port: 5984
19
+ database_name: addressbook_development
20
+
21
+ test:
22
+ server: localhost
23
+ port: 5984
24
+ database_name: addressbook_development
25
+
26
+ production:
27
+ server: production.server.com
28
+ port: 5984
29
+ database_name: addressbook_development
30
+
31
+ In order to access the database configuration for your current environment, you'd write, for instance:
32
+
33
+ db_config = ConfigLoader.load('database')
34
+ db_config['server'] # localhost
35
+ db_config[:server] # localhost
36
+ db_config.server # localhost
37
+
38
+ We're assuming that your current environment is development.
39
+
40
+ You can get the configuration of a specific running environment writing this:
41
+
42
+ db_config = ConfigLoader.load('database', 'production')
43
+ db_config['server'] # production.server.com
44
+ db_config[:server] # production.server.com
45
+ db_config.server # production.server.com
46
+
47
+ Finally, you can specify the project root too. If you don't, it will assume the project root is RAILS_ROOT.
48
+ To change it, write:
49
+
50
+
51
+ db_config = ConfigLoader.load('database', 'production')
52
+ db_config = ConfigLoader.load('database', 'test', '/home/user/my_special_project_root')
53
+
54
+ == Note on Patches/Pull Requests
55
+
56
+ * Fork the project.
57
+ * Make your feature addition or bug fix.
58
+ * Add specs for it. This is important so I don't break it in a
59
+ future version unintentionally.
60
+ * Commit, do not mess with rakefile, version, or history.
61
+ (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)
62
+ * Send me a pull request. Bonus points for topic branches.
63
+
64
+ == Copyright
65
+
66
+ Copyright (c) 2010 Vinicius Teles. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "configloader"
8
+ gem.summary = %Q{ConfigLoader makes it easy to load the values of a custom configuration file in a Rails project.}
9
+ gem.description = %Q{If you need to create a custom config file for you project, ConfigLoader will help you to load the contents of this file.}
10
+ gem.email = "vinicius@improveit.com.br"
11
+ gem.homepage = "http://github.com/viniciusteles/configloader"
12
+ gem.authors = ["Vinicius Teles"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+ task :spec => :check_dependencies
33
+
34
+ task :default => :spec
35
+
36
+ require 'rake/rdoctask'
37
+ Rake::RDocTask.new do |rdoc|
38
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
39
+
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "configloader #{version}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{configloader}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Vinicius Teles"]
12
+ s.date = %q{2010-05-17}
13
+ s.description = %q{If you need to create a custom config file for you project, ConfigLoader will help you to load the contents of this file.}
14
+ s.email = %q{vinicius@improveit.com.br}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "configloader.gemspec",
27
+ "lib/config_loader.rb",
28
+ "lib/config_loader/map.rb",
29
+ "lib/config_loader/missing_config_file_error.rb",
30
+ "lib/config_loader/missing_config_file_name_error.rb",
31
+ "lib/configloader.rb",
32
+ "spec/config_loader/map_spec.rb",
33
+ "spec/config_loader_spec.rb",
34
+ "spec/spec.opts",
35
+ "spec/spec_helper.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/viniciusteles/configloader}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.6}
41
+ s.summary = %q{ConfigLoader makes it easy to load the values of a custom configuration file in a Rails project.}
42
+ s.test_files = [
43
+ "spec/config_loader/map_spec.rb",
44
+ "spec/config_loader_spec.rb",
45
+ "spec/spec_helper.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
59
+ end
60
+ end
61
+
@@ -0,0 +1,48 @@
1
+ dependencies = %w(missing_config_file_name_error missing_config_file_error map)
2
+ dependencies.each { |dependency| require "config_loader/#{dependency}" }
3
+
4
+ module ConfigLoader
5
+
6
+ # Let's assume that you have the special file config/database.yml in your Rails project. In this example, you are using CouchDB as your database. This file has the content below:
7
+ #
8
+ # development:
9
+ # server: localhost
10
+ # port: 5984
11
+ # database_name: addressbook_development
12
+ #
13
+ # test:
14
+ # server: localhost
15
+ # port: 5984
16
+ # database_name: addressbook_development
17
+ #
18
+ # production:
19
+ # server: production.server.com
20
+ # port: 5984
21
+ # database_name: addressbook_development
22
+ #
23
+ # In order to access the database configuration for your current environment, you'd write, for instance:
24
+ #
25
+ # db_config = ConfigLoader.load('database')
26
+ # db_config['server'] # localhost
27
+ # db_config[:server] # localhost
28
+ # db_config.server # localhost
29
+ #
30
+ # We're assuming that your current environment is development.
31
+ #
32
+ # You can get the configuration of a specific running environment writing this:
33
+ #
34
+ # db_config = ConfigLoader.load('database', 'production')
35
+ # db_config['server'] # production.server.com
36
+ # db_config[:server] # production.server.com
37
+ # db_config.server # production.server.com
38
+ #
39
+ # Finally, you can specify the project root too. If you don't, it will assume the project root is RAILS_ROOT. To change it, write:
40
+ #
41
+ #
42
+ # db_config = ConfigLoader.load('database', 'production')
43
+ # db_config = ConfigLoader.load('database', 'test', '/home/user/my_special_project_root')
44
+ def self.load(file_name, running_env = Rails.env, project_root = RAILS_ROOT)
45
+ ConfigLoader::Map.new(file_name, running_env, project_root).populate
46
+ end
47
+
48
+ end
@@ -0,0 +1,36 @@
1
+ module ConfigLoader
2
+
3
+ class Map < Hash
4
+
5
+ attr_reader :file_name, :running_env, :project_root
6
+
7
+ def initialize(file_name, running_env, project_root)
8
+ raise MissingConfigFileNameError unless file_name
9
+ @file_name = "#{file_name}.yml"
10
+ @running_env = running_env
11
+ @project_root = project_root
12
+ end
13
+
14
+ def file_content
15
+ raise MissingConfigFileError unless File.exists?(full_file_name)
16
+ File.open(full_file_name) { |file| YAML::load(file) }
17
+ end
18
+
19
+ def full_file_name
20
+ "#{project_root}/config/#{file_name}"
21
+ end
22
+
23
+ def populate
24
+ file_content[@running_env].each do |key, value|
25
+ self[key] = value
26
+ self[key.to_sym] = value
27
+ end
28
+ end
29
+
30
+ def method_missing(method_name)
31
+ self[method_name]
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,11 @@
1
+ module ConfigLoader
2
+
3
+ class MissingConfigFileError < RuntimeError
4
+
5
+ def initialize
6
+ super("The config file couldn't be found.")
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,11 @@
1
+ module ConfigLoader
2
+
3
+ class MissingConfigFileNameError < ArgumentError
4
+
5
+ def initialize
6
+ super('You must provide a config file name.')
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1 @@
1
+ require 'config_loader'
@@ -0,0 +1,83 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ module ConfigLoader
4
+
5
+ describe "Map" do
6
+
7
+ before(:each) do
8
+ Rails.stub!(:env).and_return('development')
9
+ @config_loader = Map.new("database", 'development', '/home/user/project')
10
+ end
11
+
12
+ describe "initialize" do
13
+
14
+ it "should raise appropriate exception when no config file name is given" do
15
+ lambda { Map.new(nil, nil, nil) }.should raise_error(MissingConfigFileNameError)
16
+ end
17
+
18
+ it "should store the configuration file name" do
19
+ @config_loader.file_name.should == 'database.yml'
20
+ end
21
+
22
+ it "should store the given running environment" do
23
+ config_loader = Map.new("database", "production", '/home/user/project')
24
+ config_loader.running_env.should == 'production'
25
+ end
26
+
27
+ it "should store the given project root" do
28
+ config_loader = Map.new("database", "development", "/home/user/another_project")
29
+ config_loader.project_root.should == "/home/user/another_project"
30
+ end
31
+
32
+ end
33
+
34
+ describe ".full_file_name" do
35
+
36
+ it "should return the full path of the configuration file name" do
37
+ @config_loader.full_file_name.should == '/home/user/project/config/database.yml'
38
+ end
39
+
40
+ end
41
+
42
+ describe ".file_content" do
43
+
44
+ it "should load the contents of the YAML file" do
45
+ file_mock = mock('File')
46
+ File.should_receive(:exists?).with('/home/user/project/config/database.yml').and_return(true)
47
+ File.should_receive(:open).with('/home/user/project/config/database.yml').and_yield(file_mock)
48
+ YAML.should_receive(:load).with(file_mock).and_return({ 'development' => 'content'})
49
+ @config_loader.file_content.should == { 'development' => 'content'}
50
+ end
51
+
52
+ it "should raise appropriate exception when the config file is not found" do
53
+ File.should_receive(:exists?).with('/home/user/project/config/database.yml').and_return(false)
54
+ lambda { @config_loader.file_content }.should raise_error(MissingConfigFileError)
55
+ end
56
+
57
+ end
58
+
59
+ describe ".populate" do
60
+
61
+ it "should load config for a specific running environment" do
62
+ file_content = { 'development' =>
63
+ { 'server' => 'localhost',
64
+ 'port' => 5984,
65
+ 'name' => 'customers' } }
66
+ @config_loader.should_receive(:file_content).and_return(file_content)
67
+ @config_loader.populate
68
+ @config_loader['server'].should == 'localhost'
69
+ @config_loader[:server].should == 'localhost'
70
+ @config_loader.server.should == 'localhost'
71
+ @config_loader['port'].should == 5984
72
+ @config_loader[:port].should == 5984
73
+ @config_loader.port.should == 5984
74
+ @config_loader['name'].should == 'customers'
75
+ @config_loader[:name].should == 'customers'
76
+ @config_loader.name.should == 'customers'
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+
83
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ConfigLoader" do
4
+
5
+ describe ".load" do
6
+
7
+ def prepare_mocks(running_env, project_root)
8
+ Rails.stub!(:env).and_return(running_env)
9
+ map_mock = mock('ConfigLoader::Map')
10
+ map_mock.should_receive(:populate).and_return('result')
11
+ ConfigLoader::Map.should_receive(:new).with('database', running_env, project_root).and_return(map_mock)
12
+ end
13
+
14
+ it "should delegate to ConfigLoader::Map.populate" do
15
+ prepare_mocks('development', '/home/user/project')
16
+ ConfigLoader.load('database').should == 'result'
17
+ end
18
+
19
+ it "should delegate to ConfigLoader::Map.populate with the given running_env" do
20
+ prepare_mocks('production', '/home/user/project')
21
+ ConfigLoader.load('database', 'production').should == 'result'
22
+ end
23
+
24
+ it "should delegate to ConfigLoader::Map.populate with the given project_root" do
25
+ prepare_mocks('development', '/home/user/another_project')
26
+ ConfigLoader.load('database', 'development', '/home/user/another_project').should == 'result'
27
+ end
28
+
29
+ end
30
+
31
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'configloader'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ RAILS_ROOT = '/home/user/project'
8
+
9
+ class Rails
10
+ end
11
+
12
+
13
+ Spec::Runner.configure do |config|
14
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configloader
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Vinicius Teles
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-17 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: If you need to create a custom config file for you project, ConfigLoader will help you to load the contents of this file.
35
+ email: vinicius@improveit.com.br
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - configloader.gemspec
51
+ - lib/config_loader.rb
52
+ - lib/config_loader/map.rb
53
+ - lib/config_loader/missing_config_file_error.rb
54
+ - lib/config_loader/missing_config_file_name_error.rb
55
+ - lib/configloader.rb
56
+ - spec/config_loader/map_spec.rb
57
+ - spec/config_loader_spec.rb
58
+ - spec/spec.opts
59
+ - spec/spec_helper.rb
60
+ has_rdoc: true
61
+ homepage: http://github.com/viniciusteles/configloader
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --charset=UTF-8
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.6
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: ConfigLoader makes it easy to load the values of a custom configuration file in a Rails project.
90
+ test_files:
91
+ - spec/config_loader/map_spec.rb
92
+ - spec/config_loader_spec.rb
93
+ - spec/spec_helper.rb