ez_config 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", ">= 2.8.0"
10
+ gem "yard", "~> 0.7"
11
+ gem "rdoc", "~> 3.12"
12
+ gem "bundler", "~> 1.0.0"
13
+ gem "jeweler", "~> 1.8.3"
14
+ if RUBY_VERSION =~ /^1\.9/
15
+ gem "simplecov"
16
+ else
17
+ gem "rcov"
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Aaron Qian
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,93 @@
1
+ ez_config
2
+ =========
3
+
4
+ This gem can load your yaml configuration files from a specified path.
5
+
6
+
7
+ The idea behind this type of configuration is a similar concept to the
8
+ traditional database.yml file found in Rails applications. This file specifies
9
+ all environments for a service in one file, instead of all services for an
10
+ environment in one file.
11
+
12
+ Install
13
+ --------
14
+
15
+ Terminal:
16
+
17
+ ```
18
+ gem install ez_config
19
+ ```
20
+
21
+ Bundler:
22
+
23
+ ```ruby
24
+ gem 'ez_config'
25
+ ```
26
+
27
+ Usage
28
+ -----
29
+
30
+ The following example creates a new `EzConfig` object and loads all configurations
31
+ from your applications `config` directory. Obviously you can use this gem in any
32
+ ruby project, and it is not rails specific.
33
+
34
+ ```ruby
35
+ @config = EzConfig.new({
36
+ :env => Rails.env,
37
+ :path => "#{Rais.root}/config"
38
+ })
39
+ ```
40
+
41
+ To configure a new service called 'new_service', you will create a YML file in
42
+ your config direcotry. The file has to have an extension of `.yml`. The name of
43
+ the file will become the config key. This file has to contain at least two
44
+ environments, `production` and `non_production`:
45
+
46
+ ```yaml
47
+ non_production:
48
+ uri: http://dev.path.to.new.service
49
+ something: else
50
+
51
+ production:
52
+ uri: http://path.to.new.service
53
+ something: else
54
+ ```
55
+
56
+ After this file has been created, access to it can be obtained by doing:
57
+
58
+ ```ruby
59
+ @config['new_service']
60
+ # {'uri' => 'http://path.to.new.service', 'something' => 'else'}
61
+ ```
62
+
63
+ By the fault if your `env` name does not start with `production` and is not specified in
64
+ your yaml file, EzConfig will load the default `non_production` env. If your `env`
65
+ name starts with `production`( e.g. `production-1` ), and is not specified in your yaml
66
+ file, it will load the default `production` env. This simplifies configuration for large
67
+ projects that may have many a dozen different environments.
68
+
69
+ Lastly, if your cretiria in recognizing `production` and `production` environments differs
70
+ from the default, you can pass in `:production_regex` to specify your own cretieria
71
+
72
+ ```ruby
73
+ @config = EzConfig.new({
74
+ :env => Rails.env,
75
+ :path => "#{Rais.root}/config",
76
+ :production_regex => /-prod$/
77
+ })
78
+
79
+ == Contributing to ez_config
80
+
81
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
82
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
83
+ * Fork the project.
84
+ * Start a feature/bugfix branch.
85
+ * Commit and push until you are happy with your contribution.
86
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
87
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
88
+
89
+ == Copyright
90
+
91
+ Copyright (c) 2012 Aaron Qian. See LICENSE.txt for
92
+ further details.
93
+
@@ -0,0 +1,52 @@
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 = "ez_config"
18
+ gem.homepage = "http://github.com/aq1018/ez_config"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{load yml files from specified path and easily access your config data.}
21
+ gem.description = %Q{ez_config loads yml files from specified path and allows you to easily access your config data.}
22
+ gem.email = "aq1018@gmail.com"
23
+ gem.authors = ["Aaron Qian"]
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
+ spec.rspec_opts = "--color --format progress"
33
+ end
34
+
35
+ if RUBY_VERSION =~ /^1\.9/
36
+ desc "Code coverage detail"
37
+ task :simplecov do
38
+ ENV['COVERAGE'] = "true"
39
+ Rake::Task[:spec].execute
40
+ end
41
+ elsif RUBY_VERSION =~ /^1\.8/
42
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
43
+ spec.pattern = 'spec/**/*_spec.rb'
44
+ spec.rcov = true
45
+ spec.rcov_opts = "--exclude ~\/.rvm,spec"
46
+ end
47
+ end
48
+
49
+ task :default => :spec
50
+
51
+ require 'yard'
52
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,66 @@
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 = "ez_config"
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Aaron Qian"]
12
+ s.date = "2012-02-06"
13
+ s.description = "ez_config loads yml files from specified path and allows you to easily access your config data."
14
+ s.email = "aq1018@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "LICENSE.txt",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "ez_config.gemspec",
27
+ "lib/ez_config.rb",
28
+ "spec/config/bar.yml",
29
+ "spec/config/foo.yml",
30
+ "spec/ez_config_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = "http://github.com/aq1018/ez_config"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.10"
37
+ s.summary = "load yml files from specified path and easily access your config data."
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
44
+ s.add_development_dependency(%q<yard>, ["~> 0.7"])
45
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
46
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
47
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
48
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
51
+ s.add_dependency(%q<yard>, ["~> 0.7"])
52
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
53
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
55
+ s.add_dependency(%q<simplecov>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
59
+ s.add_dependency(%q<yard>, ["~> 0.7"])
60
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
61
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
62
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
63
+ s.add_dependency(%q<simplecov>, [">= 0"])
64
+ end
65
+ end
66
+
@@ -0,0 +1,36 @@
1
+ class EzConfig
2
+ class NoConfigForEnv < StandardError; end
3
+
4
+ PRODUCTION_REGEX = /^production/
5
+
6
+ def initialize(opt)
7
+ @env = opt[:env].to_s
8
+ @path = opt[:path]
9
+ @production_regex = opt[:production_regex] || PRODUCTION_REGEX
10
+ end
11
+
12
+ def [](k)
13
+ config[k]
14
+ end
15
+
16
+ def files
17
+ Dir.glob File.join(@path, '*.yml')
18
+ end
19
+
20
+ def default_env
21
+ @env =~ @production_regex ? 'production' : 'non_production'
22
+ end
23
+
24
+ def config
25
+ @config ||= files.inject({}) do |config, file|
26
+ key = File.basename file, '.yml'
27
+ yaml = YAML.load_file file
28
+ val = yaml[@env] || yaml[default_env]
29
+
30
+ raise NoConfigForEnv, "Environment #{@env} not found in #{file}" unless val
31
+
32
+ config[key] = val
33
+ config
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ non_production:
2
+ foo_path: /path/to/dev
3
+
4
+ production:
5
+ foo_path: /path/to/prod
6
+
7
+ production_west_coast:
8
+ foo_path: /path/to/prod_west
9
+
10
+ production_east_coast:
11
+ foo_path: /path/to/prod_east
@@ -0,0 +1,11 @@
1
+ non_production:
2
+ foo_path: /path/to/dev
3
+
4
+ production:
5
+ foo_path: /path/to/prod
6
+
7
+ production_west_coast:
8
+ foo_path: /path/to/prod_west
9
+
10
+ production_east_coast:
11
+ foo_path: /path/to/prod_east
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe EzConfig do
4
+ def ez_config(env)
5
+ EzConfig.new({
6
+ :env => env,
7
+ :path => File.join(File.dirname(__FILE__), 'config')
8
+ })
9
+ end
10
+
11
+ it "should load all configs" do
12
+ config = ez_config(:test)
13
+ config['foo'].should_not be_empty
14
+ config['bar'].should_not be_empty
15
+ end
16
+
17
+ it "should load default non_production configurations" do
18
+ config = ez_config(:test)
19
+ config['foo']['foo_path'].should == '/path/to/dev'
20
+ end
21
+
22
+ it "should load default production configurations" do
23
+ config = ez_config(:production_japan)
24
+ config['foo']['foo_path'].should == '/path/to/prod'
25
+ end
26
+
27
+ it "should load specific configurations" do
28
+ config = ez_config(:production_west_coast)
29
+ config['foo']['foo_path'].should == '/path/to/prod_west'
30
+ end
31
+ end
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'rspec'
12
+
13
+ GEM_ROOT = File.join(File.dirname(__FILE__), '..')
14
+
15
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
16
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
17
+
18
+ if RUBY_VERSION =~ /^1\.9/
19
+ require 'simplecov'
20
+
21
+ module SimpleCov::Configuration
22
+ def clean_filters
23
+ @filters = []
24
+ end
25
+ end
26
+
27
+ SimpleCov.configure do
28
+ clean_filters
29
+ load_adapter 'test_frameworks'
30
+ end
31
+
32
+ ENV["COVERAGE"] && SimpleCov.start do
33
+ add_filter "/.rvm/"
34
+ end
35
+ end
36
+
37
+ require 'ez_config'
38
+
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ez_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aaron Qian
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &19350100 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.8.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *19350100
25
+ - !ruby/object:Gem::Dependency
26
+ name: yard
27
+ requirement: &19349520 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '0.7'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *19349520
36
+ - !ruby/object:Gem::Dependency
37
+ name: rdoc
38
+ requirement: &19349020 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.12'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *19349020
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &19368200 !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: *19368200
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &19367640 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.8.3
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *19367640
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: &19367120 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *19367120
80
+ description: ez_config loads yml files from specified path and allows you to easily
81
+ access your config data.
82
+ email: aq1018@gmail.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files:
86
+ - LICENSE.txt
87
+ - README.md
88
+ files:
89
+ - .document
90
+ - Gemfile
91
+ - LICENSE.txt
92
+ - README.md
93
+ - Rakefile
94
+ - VERSION
95
+ - ez_config.gemspec
96
+ - lib/ez_config.rb
97
+ - spec/config/bar.yml
98
+ - spec/config/foo.yml
99
+ - spec/ez_config_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: http://github.com/aq1018/ez_config
102
+ licenses:
103
+ - MIT
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ segments:
115
+ - 0
116
+ hash: -3859281619698538461
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.10
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: load yml files from specified path and easily access your config data.
129
+ test_files: []