configatron_plus 0.2.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
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'configatron'
4
+
5
+ group :development do
6
+ gem "rspec", "~> 2.3.0"
7
+ gem "yard", "~> 0.6.0"
8
+ gem "bundler", "~> 1.0.0"
9
+ gem "jeweler", "~> 1.6.2"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ configatron (2.8.2)
5
+ yamler (>= 0.1.0)
6
+ diff-lcs (1.1.2)
7
+ git (1.2.5)
8
+ jeweler (1.6.2)
9
+ bundler (~> 1.0)
10
+ git (>= 1.2.5)
11
+ rake
12
+ rake (0.8.7)
13
+ rspec (2.3.0)
14
+ rspec-core (~> 2.3.0)
15
+ rspec-expectations (~> 2.3.0)
16
+ rspec-mocks (~> 2.3.0)
17
+ rspec-core (2.3.1)
18
+ rspec-expectations (2.3.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.3.0)
21
+ yamler (0.1.0)
22
+ yard (0.6.8)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler (~> 1.0.0)
29
+ configatron
30
+ jeweler (~> 1.6.2)
31
+ rspec (~> 2.3.0)
32
+ yard (~> 0.6.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Scott M Parrish
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.textile ADDED
@@ -0,0 +1,84 @@
1
+ h1. ConfigatronPlus
2
+
3
+ An addition to Configatron to make it easy to read configuration from default settings and multiple locations including environmental variables.
4
+
5
+ h2. Configatron
6
+
7
+ Find out more at "Configatron":https://github.com/markbates/configatron
8
+
9
+ h2. Installation
10
+
11
+ Installation of ConfigatronPlus is easy, as it is just a RubyGem:
12
+
13
+ TODO: finish wrapping things up as gem
14
+
15
+ <pre><code>
16
+ $ sudo gem install configatron-plus
17
+ </code></pre>
18
+
19
+ Once installed you just need to require it:
20
+
21
+ <pre><code>
22
+ require 'configatron-plus'
23
+ </code></pre>
24
+
25
+ h2. Usage
26
+
27
+ ConfigatronPlus stores all of its own configuration inside configatron. Use the following to see the current setup:
28
+
29
+ <pre><code>
30
+ puts configatron.to_hash #dump it as a straight ruby code.
31
+ puts configatron.inspect #show the code you'd need to recreate the current config
32
+ </code></pre>
33
+
34
+ h2. Config
35
+
36
+ There are 5 parameters that can be configured, all of which have defaults. All 5 are located at
37
+ <pre><code>
38
+ configatron.configatron_plus
39
+ </code></pre>
40
+
41
+ |_. Parameter |_. Use |_. Default |
42
+ | env_prefix | change this to change the prefix used in to look for values in the ENV | configatron_ |
43
+ | fail_when_missing | change this to true to raise an error when a file in #sources is not found on the filesystem | false |
44
+ | rc_name | the default name of the file to look for | config.rb |
45
+ | source_files | the actual file name used for a given symbol | <pre>:current => #{configatron.configatron_plus.rc_name}</pre> |
46
+ | sources | The list of files and symbols to load | <pre>[:current, :env]</pre> |
47
+
48
+ h3. default
49
+
50
+ The default config looks like this when #inspect
51
+
52
+ <pre><code>
53
+ configatron.configatron_plus.env_prefix = "configatron_"
54
+ configatron.configatron_plus.fail_when_missing = false
55
+ configatron.configatron_plus.rc_name = "config.rb"
56
+ configatron.configatron_plus.source_files = {:current=>"config.rb"}
57
+ configatron.configatron_plus.sources = [:current, :env]
58
+ </code></pre>
59
+
60
+ h2. Examples
61
+
62
+ h3. Default
63
+
64
+ By default, ConfigurationPlus looks for a config.rb file in the current directory, and reads any Environmental Variables with a name that starts with configatron_
65
+
66
+ <pre><code>
67
+ ConfigatronPlus.fetch_sources #Checks the current dir for config.rb and reads from environmental variables
68
+ </code></pre>
69
+
70
+ h3. Different default filename and prefix
71
+
72
+ To look for an alternate filename, set the rc_name parameter
73
+
74
+ <pre><code>
75
+ configatron.configatron_plus.rc_name = "my_app.rb"
76
+ configatron.configatron_plus.env_prefix = "my_app_
77
+ ConfigatronPlus.fetch_sources #Checks the current dir for my_app.rb and reads from environmental variables starting with my_app_
78
+ </code></pre>
79
+
80
+
81
+
82
+ h2. Contact
83
+
84
+
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "configatron_plus"
18
+ gem.homepage = "http://github.com/anithri/configatron-plus"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A helper for the configatron library}
21
+ gem.description = %Q{Aids in using the configatron library by making it easy to read in multiple configuration files and environmental variables.}
22
+ gem.email = "anithri@gmail.com"
23
+ gem.authors = ["Scott M Parrish"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/config/config.rb ADDED
File without changes
@@ -0,0 +1,72 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
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 = "configatron_plus"
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Scott M Parrish"]
12
+ s.date = "2011-09-08"
13
+ s.description = "Aids in using the configatron library by making it easy to read in multiple configuration files and environmental variables."
14
+ s.email = "anithri@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.textile"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.textile",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "config/config.rb",
29
+ "configatron_plus.gemspec",
30
+ "examples/config.rb",
31
+ "examples/first.rb",
32
+ "examples/second.rb",
33
+ "examples/use_two_files",
34
+ "examples/with_defaults",
35
+ "examples/with_env",
36
+ "lib/configatron_plus.rb",
37
+ "lib/configatron_plus/errors.rb",
38
+ "spec/config_files/testing_config.rb",
39
+ "spec/configatron_plus_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+ s.homepage = "http://github.com/anithri/configatron-plus"
43
+ s.licenses = ["MIT"]
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = "1.8.10"
46
+ s.summary = "A helper for the configatron library"
47
+
48
+ if s.respond_to? :specification_version then
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_runtime_dependency(%q<configatron>, [">= 0"])
53
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
54
+ s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
55
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
56
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
57
+ else
58
+ s.add_dependency(%q<configatron>, [">= 0"])
59
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
60
+ s.add_dependency(%q<yard>, ["~> 0.6.0"])
61
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
62
+ s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<configatron>, [">= 0"])
66
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
67
+ s.add_dependency(%q<yard>, ["~> 0.6.0"])
68
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
69
+ s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
70
+ end
71
+ end
72
+
@@ -0,0 +1,5 @@
1
+ configatron.example do |c|
2
+ c.this = "is a test"
3
+ c.file = "config.rb"
4
+ end
5
+
data/examples/first.rb ADDED
@@ -0,0 +1,6 @@
1
+ configatron.example do |c|
2
+ c.this = "is a test"
3
+ c.replaced = "from first file"
4
+ c.file.first = "first.rb"
5
+ end
6
+
@@ -0,0 +1,6 @@
1
+ configatron.example do |c|
2
+ c.that = "is a test"
3
+ c.replaced = "from second file"
4
+ c.file.second = "second.rb"
5
+ end
6
+
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
3
+ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
4
+ require 'configatron_plus'
5
+
6
+ ConfigatronPlus.sources =['first.rb','second.rb']
7
+ ConfigatronPlus.fetch_sources
8
+
9
+ puts "first.rb called first, second.rb called second"
10
+ puts configatron.inspect
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
3
+ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
4
+ require 'configatron_plus'
5
+
6
+ puts "Default Configuration before #fetch_sources"
7
+ puts configatron.inspect
8
+ ConfigatronPlus.fetch_sources
9
+ puts "\nDefault Configuration after #fetch_sources"
10
+ puts configatron.inspect
data/examples/with_env ADDED
@@ -0,0 +1,4 @@
1
+ #!/bin/bash
2
+ export configatron_from_env_first="abc"
3
+ export configatron_from_env_again="xyz"
4
+ ./$1
@@ -0,0 +1,143 @@
1
+ require 'configatron'
2
+ require 'configatron_plus/errors'
3
+ require 'pathname'
4
+ module ConfigatronPlus
5
+
6
+ DEFAULT_SOURCES = [:current, :env]
7
+
8
+ def self.config
9
+ configatron.configatron_plus
10
+ end
11
+
12
+ configatron.configatron_plus do |c|
13
+ c.set_default(:sources, DEFAULT_SOURCES.dup)
14
+ c.set_default(:rc_name, "config.rb")
15
+ c.set_default(:env_prefix, "configatron_")
16
+ c.set_default(:source_files, {:current => configatron.configatron_plus.rc_name})
17
+ c.set_default(:fail_when_missing, false)
18
+ end
19
+
20
+ def self.fail_when_missing
21
+ configatron.configatron_plus.fail_when_missing
22
+ end
23
+
24
+ def self.fail_when_missing=(val)
25
+ configatron.configatron_plus.fail_when_missing = val
26
+ end
27
+
28
+ # Set the sources to use for configatron
29
+ # @param new_val [Array<String,Symbol>] new array of sources.
30
+ def self.sources=(new_val)
31
+ @@errors = nil
32
+ raise ArgumentError.new "value must be an Array." unless new_val.is_a?(Array)
33
+ unless self.valid_list?(new_val)
34
+ raise ArgumentError.new "Array members must be a symbol, string, or Pathname: #{self.errors}"
35
+ end
36
+ self.config.sources = new_val
37
+ end
38
+
39
+ # Get the current list of sources
40
+ # @return [Array<String,Symbol>] of sources to look for configurations in
41
+ def self.sources
42
+ self.config.sources
43
+ end
44
+
45
+ # @return [Array] of symbols.
46
+
47
+ def self.errors
48
+ @@errors ||= []
49
+ end
50
+
51
+ #set @@sources = DEFAULT_SOURCES
52
+ def self.reset_sources
53
+ self.sources = DEFAULT_SOURCES.dup
54
+ end
55
+
56
+ #check each element of the list to see if it's a correct symbol or can be used a a filename.
57
+ # @return [Boolean]
58
+ def self.valid_list?(val)
59
+ val.each do |i|
60
+ errors << "Invalid entry: #{i}" unless self.can_use_as_filename?(i)
61
+ if i.is_a?(Symbol) && ! self.is_symbol_defined?(i)
62
+ errors << "Invalid symbol: #{i.to_s}"
63
+ end
64
+ end
65
+ errors.empty?
66
+ end
67
+
68
+ def self.is_symbol_defined?(sym)
69
+ return true if sym == :env
70
+ self.config.source_files.keys.include?(sym)
71
+ end
72
+
73
+ # @param opts [Hash] the options for getting configuration
74
+ # @option opts [Boolean] :fail_on_missing (false) Raise an exception if a file in the @sources list is not present
75
+ def self.fetch_sources
76
+ self.sources.each do |s|
77
+ if s.is_a?(Symbol)
78
+ get_from_special(s)
79
+ else
80
+ get_from_file(s.is_a?(Pathname) ? s : Pathname(s))
81
+ end
82
+ end
83
+ end
84
+
85
+ # Checks to see if we can use this as a filename
86
+ # @param val [Object] Object to check
87
+ # @return [Boolean]]
88
+ def self.can_use_as_filename?(val)
89
+ return true if val.is_a?(String)
90
+ return true if val.is_a?(Pathname)
91
+ return true if val.is_a?(Symbol)
92
+
93
+ #if no other
94
+ false
95
+ end
96
+
97
+ # looks upo the file for a given symbol and calls get_from_file with it
98
+ # @param s [Symbol] use to look up and load a file
99
+ def self.get_from_special(s)
100
+ raise ArgumentError unless s.is_a?(Symbol)
101
+ return get_from_env if s == :env
102
+ file = config.source_files.fetch(s,nil)
103
+ raise ConfigatronPlus::ConfigError.new("Setting does not exist for: #{s}") if file.nil?
104
+ file = Pathname(file)
105
+ self.get_from_file(file)
106
+ end
107
+
108
+ # checks to see if file exists and loads it
109
+ # @param file [Pathname] file to attempt to load
110
+ def self.get_from_file(file)
111
+ raise ArgumentError.new("Not Pathname: #{file}") unless file.is_a?(Pathname)
112
+ if file.expand_path.file?
113
+ c = File.read(file.expand_path)
114
+ eval(c)
115
+ elsif self.fail_when_missing
116
+ raise ConfigatronPlus::ConfigError.new("Config file does not exist: #{file.to_s}")
117
+ end
118
+ end
119
+
120
+ #scan ENV for any keys that start with alfred_
121
+ #check to see that they are valid names
122
+ #add them to configatron
123
+ def self.get_from_env
124
+ ENV.keys.find_all {|e| self.use_this_env?(e)}.each do |key|
125
+ setting = key[config.env_prefix.length .. -1]
126
+ setting.split('_').each do |e|
127
+ raise ConfigatronPlus::ConfigError.new("Env variable '#{key}' contains invalid characters") unless self.is_valid_key_name?(e)
128
+ end
129
+ eval("configatron." + setting.gsub('_','.') + " = \"#{ENV[key]}\"")
130
+ end
131
+ end
132
+
133
+ def self.use_this_env?(env)
134
+ env.length > config.env_prefix.length && env.start_with?(config.env_prefix)
135
+ end
136
+
137
+ # Checks a string to make sure it is a valid identifier so we can eval it
138
+ # @param e [String] string to check for validity
139
+ # @return [Boolean] is valid?
140
+ def self.is_valid_key_name?(e)
141
+ /^[a-z][a-z0-9]*$/.match(e.downcase)
142
+ end
143
+ end
@@ -0,0 +1,4 @@
1
+ module ConfigatronPlus
2
+ class ConfigError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ configatron.file do |c|
2
+ c.test.one = 123
3
+ c.test.two = 'abc'
4
+ c.another = 'xyz'
5
+ c.foo.bar.baz = 'woot'
6
+ end
@@ -0,0 +1,212 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require "rspec"
4
+
5
+ describe "ConfigatronPlus" do
6
+ after :each do
7
+ ConfigatronPlus.reset_sources
8
+ end
9
+
10
+ describe "#can_use_as_filename?(val)" do
11
+ it "should be true for symbols, strings and Pathnames" do
12
+ ConfigatronPlus.can_use_as_filename?(:testme).should be_true
13
+ ConfigatronPlus.can_use_as_filename?("Hi").should be_true
14
+ ConfigatronPlus.can_use_as_filename?(Pathname("Hi")).should be_true
15
+ end
16
+
17
+ it "should be false for anything else" do
18
+ ConfigatronPlus.can_use_as_filename?(123).should be_false
19
+ ConfigatronPlus.can_use_as_filename?({:error => 'yes'}).should be_false
20
+ ConfigatronPlus.can_use_as_filename?([1,2,3]).should be_false
21
+ end
22
+ end
23
+
24
+ describe "#valid_list?" do
25
+ it "should be true and errors should be empty for valid @sources" do
26
+ ConfigatronPlus.valid_list?(ConfigatronPlus::DEFAULT_SOURCES).should be_true
27
+ ConfigatronPlus.errors.should == []
28
+ end
29
+
30
+ it "should be false and have errors for invalid symbols" do
31
+ ConfigatronPlus.valid_list?([:error]).should be_false
32
+ end
33
+ end
34
+
35
+ describe "#sources and #sources=" do
36
+ it "should initialize with defaults" do
37
+ ConfigatronPlus.sources.should == ConfigatronPlus::DEFAULT_SOURCES
38
+ end
39
+
40
+ it "should accept a valid sources array" do
41
+ expect {
42
+ ConfigatronPlus.sources = [:current]
43
+ }.should_not raise_error
44
+ end
45
+
46
+ it "should not accept an invalid array" do
47
+ expect {ConfigatronPlus.sources = "error"}.should raise_error(ArgumentError)
48
+ expect {ConfigatronPlus.sources = 123 }.should raise_error(ArgumentError)
49
+ expect {ConfigatronPlus.sources = Pathname("/")}.should raise_error(ArgumentError)
50
+ expect {ConfigatronPlus.sources = {:error => "yes indeed"}}.should raise_error(ArgumentError)
51
+ expect {ConfigatronPlus.sources = []}.should_not raise_error(ArgumentError)
52
+ expect {ConfigatronPlus.sources = ["error"]}.should_not raise_error(ArgumentError)
53
+ expect {ConfigatronPlus.sources = [:error]}.should raise_error(ArgumentError)
54
+ end
55
+ end
56
+
57
+ describe "#fetch_sources" do
58
+ before :all do
59
+ ConfigatronPlus.stub(:get_from_special)
60
+ ConfigatronPlus.stub(:get_from_file)
61
+ end
62
+
63
+ context "default values and all symbols" do
64
+ it "should correctly call #get_from_special and #get_from_file" do
65
+ ConfigatronPlus.should_receive(:get_from_special).exactly(2).times
66
+ ConfigatronPlus.fetch_sources
67
+ end
68
+ end
69
+ context "all strings" do
70
+ it "should correctly call #get_from_special and #get_from_file" do
71
+ ConfigatronPlus.sources = %w(a b c d e)
72
+ ConfigatronPlus.should_receive(:get_from_file).exactly(5).times
73
+ ConfigatronPlus.fetch_sources
74
+ end
75
+ end
76
+ context "mixed strings and symbols" do
77
+ it "should correctly call #get_from_special and #get_from_file" do
78
+ ConfigatronPlus.sources = ['a', :current, 'b', 'c', 'd', 'e', :env]
79
+ ConfigatronPlus.should_receive(:get_from_special).exactly(2).times
80
+ ConfigatronPlus.should_receive(:get_from_file).exactly(5).times
81
+ ConfigatronPlus.fetch_sources
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "#is_valid_key_name?(e)" do
87
+ it "should be true for good values" do
88
+ ConfigatronPlus.is_valid_key_name?("a").should be_true
89
+ ConfigatronPlus.is_valid_key_name?("abc").should be_true
90
+ ConfigatronPlus.is_valid_key_name?("ABC").should be_true
91
+ ConfigatronPlus.is_valid_key_name?("abC").should be_true
92
+ ConfigatronPlus.is_valid_key_name?("Cacd").should be_true
93
+ ConfigatronPlus.is_valid_key_name?("a124").should be_true
94
+ end
95
+ it "should be false for bad values" do
96
+ ConfigatronPlus.is_valid_key_name?("1").should be_false
97
+ ConfigatronPlus.is_valid_key_name?("a.1").should be_false
98
+ ConfigatronPlus.is_valid_key_name?("-e").should be_false
99
+ ConfigatronPlus.is_valid_key_name?("po^").should be_false
100
+ ConfigatronPlus.is_valid_key_name?("this is").should be_false
101
+ ConfigatronPlus.is_valid_key_name?("this_is").should be_false
102
+ end
103
+ end
104
+
105
+ describe "#use_this_env?(env)" do
106
+ it "should be true when passed a valid name" do
107
+ ConfigatronPlus.use_this_env?('configatron_test').should be_true
108
+ ConfigatronPlus.use_this_env?('configatron_foo_bar').should be_true
109
+ ConfigatronPlus.use_this_env?('configatron_foo_bar_baz').should be_true
110
+ end
111
+
112
+ it "should be false when passed an invalid name" do
113
+ ConfigatronPlus.use_this_env?('testing_me').should be_false
114
+ ConfigatronPlus.use_this_env?('t').should be_false
115
+ ConfigatronPlus.use_this_env?('configatron').should be_false
116
+ ConfigatronPlus.use_this_env?('configatron_').should be_false
117
+ ConfigatronPlus.use_this_env?('configatro_foo').should be_false
118
+ ConfigatronPlus.use_this_env?('configatron-bar-baz').should be_false
119
+ end
120
+ end
121
+ describe "#is_symbol_defined?(sym)" do
122
+ it "should be true when passed an existing symbol" do
123
+ ConfigatronPlus.is_symbol_defined?(:env).should be_true
124
+ ConfigatronPlus.is_symbol_defined?(:current).should be_true
125
+ ConfigatronPlus.config.source_files[:testing] = "test"
126
+ ConfigatronPlus.is_symbol_defined?(:testing).should be_true
127
+ end
128
+
129
+ it "should be false when passed any other symbol or object" do
130
+ ConfigatronPlus.is_symbol_defined?(:foo).should be_false
131
+ ConfigatronPlus.is_symbol_defined?('foo').should be_false
132
+ ConfigatronPlus.is_symbol_defined?(123).should be_false
133
+ ConfigatronPlus.is_symbol_defined?([1,2,3]).should be_false
134
+ end
135
+ end
136
+ describe "#get_from_env" do
137
+ it "should set configatron settings from environment" do
138
+ ENV['configatron_test_one'] = "123"
139
+ ENV['configatron_test_two'] = "abc"
140
+ ENV['configatron_another'] = "foo,bar,baz"
141
+
142
+ ConfigatronPlus.get_from_env
143
+ configatron.test.one.should == "123"
144
+ configatron.test.two.should == "abc"
145
+ configatron.another.should == "foo,bar,baz"
146
+ end
147
+ end
148
+
149
+ describe "#get_from_special(s)" do
150
+ before :each do
151
+ ConfigatronPlus.stub(:get_from_file)
152
+ end
153
+ it "should not raise an error for the default settings (less env)" do
154
+ [:current, :env].each do |sym|
155
+ expect {
156
+ ConfigatronPlus.get_from_special(sym)
157
+ }.should_not raise_error ConfigatronPlus::ConfigError
158
+ end
159
+ end
160
+
161
+ it "should raise an error if setting does not exist for symbol" do
162
+ expect {
163
+ ConfigatronPlus.get_from_special(:bad)
164
+ }.should raise_error ConfigatronPlus::ConfigError
165
+ end
166
+
167
+ it "should call get_from_file when called correctly" do
168
+ ConfigatronPlus.should_receive(:get_from_file).once
169
+ ConfigatronPlus.get_from_special(:current)
170
+ end
171
+ end
172
+
173
+ describe "#get_from_file(file)" do
174
+ after :each do
175
+ ConfigatronPlus.reset_sources
176
+ ConfigatronPlus.fail_when_missing = false
177
+ end
178
+
179
+ it "should raise an error if not called with a pathname" do
180
+ expect {
181
+ ConfigatronPlus.get_from_file("failure")
182
+ }.should raise_error ArgumentError
183
+ end
184
+
185
+ context "fail_when_missing is true" do
186
+ it "should raise an error when the file doesn't exist" do
187
+ ConfigatronPlus.fail_when_missing = true
188
+ expect {
189
+ ConfigatronPlus.get_from_file(Pathname.new("/some/long/foo/bar/baz/path"))
190
+ }.should raise_error ConfigatronPlus::ConfigError
191
+ end
192
+ end
193
+ context "fail_when_missing is false" do
194
+ it "should not raise an error when the file doesn't exist" do
195
+ ConfigatronPlus.fail_when_missing = false
196
+ expect {
197
+ ConfigatronPlus.get_from_file(Pathname.new("/some/long/foo/bar/baz/path"))
198
+ }.should_not raise_error ConfigatronPlus::ConfigError
199
+
200
+ end
201
+ end
202
+
203
+ it "should read a file into configatron when file is present" do
204
+ tmp = Pathname('spec/config_files/testing_config.rb')
205
+ ConfigatronPlus.get_from_file(tmp)
206
+ configatron.file.test.one.should == 123
207
+ configatron.file.test.two.should == "abc"
208
+ configatron.file.another.should == 'xyz'
209
+ configatron.file.foo.bar.baz.should == 'woot'
210
+ end
211
+ end
212
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'configatron_plus'
5
+ require 'tmpdir'
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configatron_plus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Scott M Parrish
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-08 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: configatron
16
+ requirement: &19998860 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *19998860
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &19998340 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.3.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *19998340
36
+ - !ruby/object:Gem::Dependency
37
+ name: yard
38
+ requirement: &19997820 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.6.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *19997820
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &19997320 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *19997320
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &19996800 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.6.2
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *19996800
69
+ description: Aids in using the configatron library by making it easy to read in multiple
70
+ configuration files and environmental variables.
71
+ email: anithri@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - LICENSE.txt
76
+ - README.textile
77
+ files:
78
+ - .document
79
+ - .rspec
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.textile
84
+ - Rakefile
85
+ - VERSION
86
+ - config/config.rb
87
+ - configatron_plus.gemspec
88
+ - examples/config.rb
89
+ - examples/first.rb
90
+ - examples/second.rb
91
+ - examples/use_two_files
92
+ - examples/with_defaults
93
+ - examples/with_env
94
+ - lib/configatron_plus.rb
95
+ - lib/configatron_plus/errors.rb
96
+ - spec/config_files/testing_config.rb
97
+ - spec/configatron_plus_spec.rb
98
+ - spec/spec_helper.rb
99
+ homepage: http://github.com/anithri/configatron-plus
100
+ licenses:
101
+ - MIT
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ segments:
113
+ - 0
114
+ hash: -1691687962721209573
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 1.8.10
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: A helper for the configatron library
127
+ test_files: []