configloader 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -44,12 +44,19 @@ If you need to create a custom config file for you Rails project, ConfigLoader w
44
44
  db_config['port'] # 5984
45
45
  db_config['database_name'] # addressbook_production
46
46
 
47
- Finally, you can specify the project root too. If you don't, it will assume the project root is Rails.root.
47
+ You can specify the project root too. If you don't, it will assume the project root is Rails.root.
48
48
  To change it, write:
49
49
 
50
50
 
51
51
  db_config = ConfigLoader.load('database', 'production')
52
52
  db_config = ConfigLoader.load('database', 'test', '/home/user/my_special_project_root')
53
+
54
+ Dinamic keys are also accepted. They're parsed using ERB. So, you're free to write a config file like this:
55
+
56
+ test:
57
+ server: <%= `hostname` %>
58
+ port: 5984
59
+ database_name: addressbook_test
53
60
 
54
61
  == Note on Patches/Pull Requests
55
62
 
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ begin
5
5
  require 'jeweler'
6
6
  Jeweler::Tasks.new do |gem|
7
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.}
8
+ gem.summary = %Q{ConfigLoader makes it easy to load the values of a custom configuration file in a Rails project or in a regular Ruby project.}
9
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
10
  gem.email = "vinicius@improveit.com.br"
11
11
  gem.homepage = "http://github.com/viniciusteles/configloader"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
data/configloader.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{configloader}
8
- s.version = "0.2.2"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Vinicius Teles"]
12
- s.date = %q{2010-09-28}
12
+ s.date = %q{2011-02-07}
13
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
14
  s.email = %q{vinicius@improveit.com.br}
15
15
  s.extra_rdoc_files = [
@@ -32,8 +32,9 @@ Gem::Specification.new do |s|
32
32
  "lib/config_loader/missing_config_file_error.rb",
33
33
  "lib/config_loader/missing_config_file_name_error.rb",
34
34
  "lib/configloader.rb",
35
- "spec/config_loader/map_spec.rb",
35
+ "spec/alternative_root_dir/config/database.yml",
36
36
  "spec/config_loader_spec.rb",
37
+ "spec/root_dir/config/database.yml",
37
38
  "spec/spec.opts",
38
39
  "spec/spec_helper.rb"
39
40
  ]
@@ -41,10 +42,9 @@ Gem::Specification.new do |s|
41
42
  s.rdoc_options = ["--charset=UTF-8"]
42
43
  s.require_paths = ["lib"]
43
44
  s.rubygems_version = %q{1.3.7}
44
- s.summary = %q{ConfigLoader makes it easy to load the values of a custom configuration file in a Rails project.}
45
+ s.summary = %q{ConfigLoader makes it easy to load the values of a custom configuration file in a Rails project or in a regular Ruby project.}
45
46
  s.test_files = [
46
- "spec/config_loader/map_spec.rb",
47
- "spec/config_loader_spec.rb",
47
+ "spec/config_loader_spec.rb",
48
48
  "spec/spec_helper.rb"
49
49
  ]
50
50
 
data/lib/config_loader.rb CHANGED
@@ -2,7 +2,6 @@ dependencies = %w(missing_config_file_name_error missing_config_file_error map)
2
2
  dependencies.each { |dependency| require "config_loader/#{dependency}" }
3
3
 
4
4
  module ConfigLoader
5
-
6
5
  # 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
6
  #
8
7
  # development:
@@ -36,13 +35,19 @@ module ConfigLoader
36
35
  # db_config['port'] # 5984
37
36
  # db_config['database_name'] # addressbook_production
38
37
  #
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:
38
+ # 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
39
  #
41
40
  #
42
41
  # db_config = ConfigLoader.load('database', 'production')
43
42
  # db_config = ConfigLoader.load('database', 'test', '/home/user/my_special_project_root')
43
+ #
44
+ # Dinamic keys are also accepted. They're parsed using ERB. So, you're free to write a config file like this:
45
+ #
46
+ # test:
47
+ # server: <%= `hostname` %>
48
+ # port: 5984
49
+ # database_name: addressbook_test
44
50
  def self.load(file_name, running_env = Rails.env, project_root = Rails.root.to_s)
45
51
  ConfigLoader::Map.new(file_name, running_env, project_root).load
46
52
  end
47
-
48
53
  end
@@ -1,9 +1,8 @@
1
+ require 'erb'
1
2
  require 'yaml'
2
3
 
3
4
  module ConfigLoader
4
-
5
5
  class Map
6
-
7
6
  attr_reader :file_name, :running_env, :project_root
8
7
 
9
8
  def initialize(file_name, running_env, project_root)
@@ -14,8 +13,8 @@ module ConfigLoader
14
13
  end
15
14
 
16
15
  def file_content
17
- raise MissingConfigFileError unless File.exists?(full_file_name)
18
- File.open(full_file_name) { |file| YAML::load(file) }
16
+ raise MissingConfigFileError.new(full_file_name) unless File.exists?(full_file_name)
17
+ YAML.load(ERB.new(File.read(full_file_name)).result)
19
18
  end
20
19
 
21
20
  def full_file_name
@@ -29,7 +28,5 @@ module ConfigLoader
29
28
  def method_missing(method_name)
30
29
  self[method_name]
31
30
  end
32
-
33
31
  end
34
-
35
- end
32
+ end
@@ -1,11 +1,7 @@
1
1
  module ConfigLoader
2
-
3
2
  class MissingConfigFileError < RuntimeError
4
-
5
- def initialize
6
- super("The config file couldn't be found.")
3
+ def initialize(filename)
4
+ super("The config file couldn't be found: #{filename}")
7
5
  end
8
-
9
6
  end
10
-
11
7
  end
@@ -1,11 +1,7 @@
1
1
  module ConfigLoader
2
-
3
2
  class MissingConfigFileNameError < ArgumentError
4
-
5
3
  def initialize
6
4
  super('You must provide a config file name.')
7
5
  end
8
-
9
6
  end
10
-
11
7
  end
@@ -0,0 +1,9 @@
1
+ development:
2
+ server: localhost
3
+ port: 5984
4
+ name: alternative_customers_development
5
+
6
+ test:
7
+ server: localhost
8
+ post: 5985
9
+ name: alternative_customers_test
@@ -1,35 +1,39 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
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
- Rails.stub!(:root).and_return('/home/user/project')
10
- ConfigLoader::Map.should_receive(:new).with('database', running_env, project_root).and_return(@map_mock)
11
- @map_mock.should_receive(:load).and_return('config')
4
+ before(:each) do
5
+ @root_dir = File.expand_path(File.join(File.dirname(__FILE__), "..", "spec", "root_dir"))
6
+ Rails.stub!(:root).and_return(@root_dir)
7
+ Rails.stub!(:env).and_return('development')
8
+ end
9
+
10
+ describe ".load" do
11
+ it "raises exception when no config file name is given" do
12
+ lambda { ConfigLoader.load(nil) }.should raise_error(ConfigLoader::MissingConfigFileNameError)
12
13
  end
13
14
 
14
- before(:each) do
15
- @map_mock = mock('ConfigLoader::Map')
15
+ it "raises exception when the config file can't be found" do
16
+ lambda { ConfigLoader.load('database', 'development', 'invalid_location') }.should raise_error(ConfigLoader::MissingConfigFileError)
16
17
  end
17
18
 
18
- it "should delegate to ConfigLoader::Map.populate" do
19
- prepare_mocks('development', '/home/user/project')
20
- ConfigLoader.load('database').should == 'config'
19
+ it "loads keys for Rails.env from the config file located in Rails.root/config" do
20
+ ConfigLoader.load('database')['name'].should == 'customers_development'
21
21
  end
22
22
 
23
- it "should delegate to ConfigLoader::Map.populate with the given running_env" do
24
- prepare_mocks('production', '/home/user/project')
25
- ConfigLoader.load('database', 'production').should == 'config'
23
+ it "loads keys for the given environament from the config file located in Rails.root/config" do
24
+ ConfigLoader.load('database', 'test')['name'].should == 'customers_test'
26
25
  end
27
26
 
28
- it "should delegate to ConfigLoader::Map.populate with the given project_root" do
29
- prepare_mocks('development', '/home/user/another_project')
30
- ConfigLoader.load('database', 'development', '/home/user/another_project').should == 'config'
27
+ it "loads keys for the given environmanent from the config file located under the given directory/config" do
28
+ alternative_root_dir = File.expand_path(File.join(File.dirname(__FILE__), "..", "spec", "alternative_root_dir"))
29
+ ConfigLoader.load('database', 'development', alternative_root_dir)['name'].should == 'alternative_customers_development'
31
30
  end
32
31
 
32
+ it "loads dinamic keys" do
33
+ # KEY:
34
+ # name: <%= "CUSTOMERS".downcase %>_test
35
+ config = ConfigLoader.load('database', 'test')
36
+ config['name'].should == 'customers_test'
37
+ end
33
38
  end
34
-
35
39
  end
@@ -0,0 +1,9 @@
1
+ development:
2
+ server: localhost
3
+ port: 5984
4
+ name: customers_development
5
+
6
+ test:
7
+ server: localhost
8
+ post: 5985
9
+ name: <%= "CUSTOMERS".downcase %>_test
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,3 @@ require 'spec/autorun'
6
6
 
7
7
  class Rails
8
8
  end
9
-
10
- Spec::Runner.configure do |config|
11
- end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 2
10
- version: 0.2.2
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Vinicius Teles
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-28 00:00:00 -03:00
18
+ date: 2011-02-07 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -59,8 +59,9 @@ files:
59
59
  - lib/config_loader/missing_config_file_error.rb
60
60
  - lib/config_loader/missing_config_file_name_error.rb
61
61
  - lib/configloader.rb
62
- - spec/config_loader/map_spec.rb
62
+ - spec/alternative_root_dir/config/database.yml
63
63
  - spec/config_loader_spec.rb
64
+ - spec/root_dir/config/database.yml
64
65
  - spec/spec.opts
65
66
  - spec/spec_helper.rb
66
67
  has_rdoc: true
@@ -96,8 +97,7 @@ rubyforge_project:
96
97
  rubygems_version: 1.3.7
97
98
  signing_key:
98
99
  specification_version: 3
99
- summary: ConfigLoader makes it easy to load the values of a custom configuration file in a Rails project.
100
+ summary: ConfigLoader makes it easy to load the values of a custom configuration file in a Rails project or in a regular Ruby project.
100
101
  test_files:
101
- - spec/config_loader/map_spec.rb
102
102
  - spec/config_loader_spec.rb
103
103
  - spec/spec_helper.rb
@@ -1,75 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
- require 'yaml'
3
-
4
- module ConfigLoader
5
-
6
- describe "Map" do
7
-
8
- before(:each) do
9
- Rails.stub!(:env).and_return('development')
10
- @config_loader = Map.new("database", 'development', '/home/user/project')
11
- end
12
-
13
- describe "initialize" do
14
-
15
- it "should raise appropriate exception when no config file name is given" do
16
- lambda { Map.new(nil, nil, nil) }.should raise_error(MissingConfigFileNameError)
17
- end
18
-
19
- it "should store the configuration file name" do
20
- @config_loader.file_name.should == 'database.yml'
21
- end
22
-
23
- it "should store the given running environment" do
24
- config_loader = Map.new("database", "production", '/home/user/project')
25
- config_loader.running_env.should == 'production'
26
- end
27
-
28
- it "should store the given project root" do
29
- config_loader = Map.new("database", "development", "/home/user/another_project")
30
- config_loader.project_root.should == "/home/user/another_project"
31
- end
32
-
33
- end
34
-
35
- describe ".full_file_name" do
36
-
37
- it "should return the full path of the configuration file name" do
38
- @config_loader.full_file_name.should == '/home/user/project/config/database.yml'
39
- end
40
-
41
- end
42
-
43
- describe ".file_content" do
44
-
45
- it "should load the contents of the YAML file" do
46
- file_mock = mock('File')
47
- File.should_receive(:exists?).with('/home/user/project/config/database.yml').and_return(true)
48
- File.should_receive(:open).with('/home/user/project/config/database.yml').and_yield(file_mock)
49
- YAML.should_receive(:load).with(file_mock).and_return({ 'development' => 'content'})
50
- @config_loader.file_content.should == { 'development' => 'content'}
51
- end
52
-
53
- it "should raise appropriate exception when the config file is not found" do
54
- File.should_receive(:exists?).with('/home/user/project/config/database.yml').and_return(false)
55
- lambda { @config_loader.file_content }.should raise_error(MissingConfigFileError)
56
- end
57
-
58
- end
59
-
60
- describe ".load" do
61
-
62
- it "should load config for a specific running environment" do
63
- development_config = { 'server' => 'localhost',
64
- 'port' => 5984,
65
- 'name' => 'customers' }
66
- file_content = { 'development' => development_config }
67
- @config_loader.should_receive(:file_content).and_return(file_content)
68
- @config_loader.load.should == development_config
69
- end
70
-
71
- end
72
-
73
- end
74
-
75
- end