config_yml 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "rspec"
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.2.4)
5
+ rspec (2.13.0)
6
+ rspec-core (~> 2.13.0)
7
+ rspec-expectations (~> 2.13.0)
8
+ rspec-mocks (~> 2.13.0)
9
+ rspec-core (2.13.1)
10
+ rspec-expectations (2.13.0)
11
+ diff-lcs (>= 1.1.3, < 2.0)
12
+ rspec-mocks (2.13.1)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ rspec
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ config.yml
2
+ ==========
3
+
4
+ Simplify your app configuration.
5
+
6
+ Description
7
+ -----------
8
+
9
+ config.yml provides a very simple way of configure your ruby applications through yaml files.
10
+
11
+ Examples
12
+ --------
13
+
14
+ ```yaml
15
+ # config/redis.yml
16
+ password: foo
17
+ host: localhost
18
+ port: 6379
19
+ database: bar
20
+ ```
21
+
22
+ ```ruby
23
+ if redis = Configuration.redis
24
+ password = redis[:password] && ":#{redis[:password]}@"
25
+ ENV["REDIS_URL"] = %(redis://#{password}#{redis[:host]}:#{redis[:port]}/#{redis[:database]})
26
+ end
27
+
28
+ ENV["REDIS_URL"] # => "redis://:foo@localhost:6379/bar"
29
+ ```
30
+
31
+
32
+ #### Environment based:
33
+
34
+ ```yaml
35
+ # config/mysql.yml
36
+ development:
37
+ adapter: mysql2
38
+ encoding: utf8
39
+ database: myapp_development
40
+ username: foo
41
+ password:
42
+ socket: /tmp/mysql.sock
43
+
44
+ test:
45
+ adapter: mysql2
46
+ encoding: utf8
47
+ database: myapp_test
48
+ username: foo
49
+ password:
50
+ socket: /tmp/mysql.sock
51
+ ```
52
+
53
+ Rack
54
+ ```ruby
55
+ ENV["RACK_ENV"] # => "development"
56
+ Configuration.mysql[:database] # => "myapp_development"
57
+ ```
58
+
59
+ Rails
60
+ ```ruby
61
+ Rails.env # => "test"
62
+ Configuration.mysql[:database] # => "myapp_test"
63
+ ```
64
+
65
+
66
+ #### Nested:
67
+
68
+ ```yaml
69
+ # config/russian_doll.yml
70
+ foo:
71
+ bar:
72
+ baz:
73
+ array:
74
+ - :red
75
+ - :green
76
+ - :blue
77
+ ```
78
+
79
+ ```ruby
80
+ Configuration.russian_doll[:foo][:bar][:baz] # => { :array => [:red, :green, :blue] }
81
+ ```
82
+
83
+ License
84
+ -------
85
+
86
+ Copyright 2013 Vitor Kiyoshi Arimitsu - http://vitork.com
87
+
88
+ Permission is hereby granted, free of charge, to any person obtaining a copy
89
+ of this software and associated documentation files (the "Software"), to deal
90
+ in the Software without restriction, including without limitation the rights
91
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
92
+ copies of the Software, and to permit persons to whom the Software is
93
+ furnished to do so, subject to the following conditions:
94
+
95
+ The above copyright notice and this permission notice shall be included in
96
+ all copies or substantial portions of the Software.
97
+
98
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
99
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
100
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
101
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
102
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
103
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
104
+ THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ $:.unshift(File.expand_path("../lib", __FILE__))
2
+ require "configuration/version"
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "config_yml"
6
+ spec.version = Configuration::VERSION.dup
7
+ spec.summary = "Simplify your app configuration."
8
+ spec.description = "A very simple way of configure your ruby applications through yaml files."
9
+ spec.license = "MIT"
10
+
11
+ spec.authors = ["Vitor Kiyoshi Arimitsu"]
12
+ spec.email = "to@vitork.com"
13
+ spec.homepage = "https://github.com/vitork/config_yml"
14
+
15
+ spec.files = `git ls-files`.split("\n")
16
+ spec.test_files = `git ls-files -- spec/*`.split("\n")
17
+ end
@@ -0,0 +1,3 @@
1
+ module Configuration
2
+ VERSION = "0.0.1".freeze
3
+ end
@@ -0,0 +1,53 @@
1
+ require "yaml"
2
+
3
+ module Configuration
4
+ # Returns a hash with configuration for the given class method
5
+ # called. The configuration is loaded from the file config/+method_called+.yml.
6
+ # If the hash contains a key in the first level with the same name of environment,
7
+ # the value of this key is returned. Returns nil if file is not found.
8
+ #
9
+ # For example:
10
+ # ## config/database.yml
11
+ # # development:
12
+ # # host: "localhost"
13
+ # # username: "root"
14
+ #
15
+ # ENV["RACK_ENV"] # => "development"
16
+ # Configuration.database # => { :host => "localhost", :username => "root" }
17
+ #
18
+ class << self
19
+ def method_missing(method, *args)
20
+ if file = files.select { |f| f =~ /#{method.to_s}/ }[0]
21
+ hash[method] ||= load_yml(file)
22
+ end
23
+ end
24
+
25
+ def files
26
+ @files ||= Dir.glob("config/*.yml")
27
+ end
28
+
29
+ def hash
30
+ @hash ||= {}
31
+ end
32
+
33
+ def env
34
+ @env ||= (ENV["RACK_ENV"] || Rails.env).to_sym rescue nil
35
+ end
36
+
37
+ private
38
+
39
+ def load_yml(file)
40
+ config = with_symbol_keys YAML.load_file(file)
41
+ config.is_a?(Hash) && config.has_key?(env) ?
42
+ config[env] : config
43
+ end
44
+
45
+ def with_symbol_keys(hash_config)
46
+ return hash_config unless hash_config.is_a?(Hash)
47
+
48
+ hash_config.inject({}) do |symbolized, (key, value)|
49
+ symbolized.merge({ key.to_sym => with_symbol_keys(value) })
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,107 @@
1
+ require "spec_helper"
2
+
3
+ describe Configuration do
4
+ let(:files) { ["config/file_a.yml", "config/file_b.yml"] }
5
+
6
+ before do
7
+ Configuration.instance_variable_set(:@hash, nil)
8
+ Dir.stub(:glob).and_return(files)
9
+ YAML.stub(:load_file).and_return(file_a, file_b)
10
+ end
11
+
12
+ describe "loading config files" do
13
+ let(:file_a) { double }
14
+ let(:file_b) { double }
15
+
16
+ it "loads all config files" do
17
+ Configuration.file_a
18
+ Configuration.files.should be_eql files
19
+ end
20
+ end
21
+
22
+ describe ".env" do
23
+ let(:file_a) { double }
24
+ let(:file_b) { double }
25
+ let(:rack_env) { "rack_test" }
26
+ let(:rails_env) { "rails_test" }
27
+
28
+ before { Configuration.instance_variable_set(:@env, nil) }
29
+
30
+ context "when Rack application" do
31
+ before { ENV["RACK_ENV"] = rack_env }
32
+ after { ENV["RACK_ENV"] = nil }
33
+
34
+ it "returns the environment as symbol" do
35
+ Configuration.env.should be_eql rack_env.to_sym
36
+ end
37
+ end
38
+
39
+ context "when is a Rails application" do
40
+ before do
41
+ module Rails ; end
42
+ Rails.stub(:env).and_return(rails_env)
43
+ end
44
+
45
+ after { Object.send(:remove_const, :Rails) }
46
+
47
+ it "returns the environment as symbol" do
48
+ Configuration.env.should be_eql rails_env.to_sym
49
+ end
50
+ end
51
+
52
+ context "when environment isn't defined" do
53
+ it "returns nil" do
54
+ Configuration.env.should be_nil
55
+ end
56
+ end
57
+ end
58
+
59
+ describe "acessing a key from configuration files" do
60
+ context "when a file has a flat hash" do
61
+ let(:file_a) { { key_a: "value_a" } }
62
+ let(:file_b) { { key_b: "value_b" } }
63
+
64
+ it "responds to keys split in different files" do
65
+ Configuration.file_a[:key_a].should be_eql "value_a"
66
+ Configuration.file_b[:key_b].should be_eql "value_b"
67
+ end
68
+ end
69
+
70
+ context "when a file has a nested hash" do
71
+ let(:file_a) { { nested: { key: "value" } } }
72
+ let(:file_b) { double }
73
+
74
+ it "responds to nested key values" do
75
+ Configuration.file_a[:nested][:key].should be_eql "value"
76
+ end
77
+ end
78
+
79
+ context "when a file first level keys is environment names" do
80
+ let(:file_b) { double }
81
+ let(:file_a) do
82
+ { test: { key: "value_test" },
83
+ development: { key: "value_dev" } }
84
+ end
85
+
86
+ before { ENV["RACK_ENV"] = "test" }
87
+ after { ENV["RACK_ENV"] = nil }
88
+
89
+ it "returns hash of environment key" do
90
+ Configuration.file_a[:key].should be_eql "value_test"
91
+ end
92
+
93
+ it "doesn't load hash of other environments" do
94
+ Configuration.file_a[:key].should_not be_eql "value_dev"
95
+ end
96
+ end
97
+
98
+ context "when configuration file doesn't exist" do
99
+ let(:file_a) { double }
100
+ let(:file_b) { double }
101
+
102
+ it "returns nil" do
103
+ Configuration.foo.should be_nil
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,6 @@
1
+ require "configuration"
2
+
3
+ RSpec.configure do |config|
4
+ config.color_enabled = true
5
+ config.formatter = "documentation"
6
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: config_yml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vitor Kiyoshi Arimitsu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-27 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A very simple way of configure your ruby applications through yaml files.
15
+ email: to@vitork.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - Gemfile.lock
22
+ - README.md
23
+ - config_yml.gemspec
24
+ - lib/configuration.rb
25
+ - lib/configuration/version.rb
26
+ - spec/lib/configuration_spec.rb
27
+ - spec/spec_helper.rb
28
+ homepage: https://github.com/vitork/config_yml
29
+ licenses:
30
+ - MIT
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.24
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Simplify your app configuration.
53
+ test_files:
54
+ - spec/lib/configuration_spec.rb
55
+ - spec/spec_helper.rb