global 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.ruby-version +1 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/README.md +54 -0
- data/Rakefile +11 -0
- data/global.gemspec +28 -0
- data/lib/global.rb +12 -0
- data/lib/global/base.rb +65 -0
- data/lib/global/configuration.rb +22 -0
- data/lib/global/version.rb +3 -0
- data/spec/files/rspec/config.yml +4 -0
- data/spec/files/rspec_config.yml +4 -0
- data/spec/global/configuration_spec.rb +58 -0
- data/spec/global_spec.rb +66 -0
- data/spec/spec_helper.rb +17 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 93e3427e1eb0655bda5b432903488038d53ffa6f
|
4
|
+
data.tar.gz: 8dd72fdd5856a96a15e9cf0c4b63448e45298b9a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 55e64a30578abff796bc7305095857a624e5af9bf3defe5da465461fdb0db316f7d29e42497d76d1ef8e1a36646c3527adfb46b551b4b06542ed73b464d447b9
|
7
|
+
data.tar.gz: 0d4f0672ce23acadeb374cf3dbf3695e5828b2d53718a956c52886d5bb5b547072e53e39d88a5f13525c2e645a4595a91c9717b6ab214b7abaeeb7ad6f3c7e8f
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 paladiy
|
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.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# global [![Build Status](https://travis-ci.org/paladiy/global.png)](https://travis-ci.org/paladiy/global) [![Code Climate](https://codeclimate.com/github/paladiy/global.png)](https://codeclimate.com/github/paladiy/global)
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
Global provides accessors methods to your configuration which is stored in yaml files.
|
6
|
+
|
7
|
+
## Instalation
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'global'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Configuration
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
> Global.environment = "YOUR_ENV_HERE"
|
17
|
+
> Global.config_directory = "PATH_TO_DIRECTORY_WITH_FILES"
|
18
|
+
```
|
19
|
+
|
20
|
+
##Usage
|
21
|
+
|
22
|
+
Loading configuration from: `PATH_TO_DIRECTORY_WITH_FILES/hosts.yml`
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
> Global.hosts
|
26
|
+
=> { "api" => "api.localhost.dev", "app" => "localhost.dev" }
|
27
|
+
> Global.hosts.api
|
28
|
+
=> { "api" => "api.localhost.dev" }
|
29
|
+
```
|
30
|
+
|
31
|
+
Loading recursive from: `PATH_TO_DIRECTORY_WITH_FILES/sites/api.yml`
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
> Global.sites.api
|
35
|
+
=> { "host" => "api.localhost.dev", "port" => 3000 }
|
36
|
+
> Global.sites.api.host
|
37
|
+
=> "api.localhost.dev"
|
38
|
+
```
|
39
|
+
|
40
|
+
## Contributing to global
|
41
|
+
|
42
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
43
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
44
|
+
* Fork the project.
|
45
|
+
* Start a feature/bugfix branch.
|
46
|
+
* Commit and push until you are happy with your contribution.
|
47
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
48
|
+
* 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.
|
49
|
+
|
50
|
+
## Copyright
|
51
|
+
|
52
|
+
Copyright (c) 2013 paladiy. See LICENSE.txt for
|
53
|
+
further details.
|
54
|
+
|
data/Rakefile
ADDED
data/global.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "global/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "global"
|
6
|
+
s.version = Global::VERSION
|
7
|
+
s.authors = ["paladiy"]
|
8
|
+
s.email = "olexanderpaladiy@gmail.com"
|
9
|
+
|
10
|
+
s.rubyforge_project = "global"
|
11
|
+
|
12
|
+
s.description = "Simple way to load your configs from yaml"
|
13
|
+
s.summary = "Simple way to load your configs from yaml"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.homepage = "http://github.com/paladiy/global"
|
21
|
+
s.licenses = ["MIT"]
|
22
|
+
|
23
|
+
s.add_development_dependency "rspec", "~> 2.14.1"
|
24
|
+
s.add_development_dependency "simplecov", "~> 0.7.1"
|
25
|
+
s.add_development_dependency "rake", "~> 10.1.0"
|
26
|
+
|
27
|
+
s.add_runtime_dependency "activesupport"
|
28
|
+
end
|
data/lib/global.rb
ADDED
data/lib/global/base.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require "erb"
|
2
|
+
|
3
|
+
module Global
|
4
|
+
module Base
|
5
|
+
def configuration(env = nil)
|
6
|
+
@configuration = nil if env
|
7
|
+
@configuration ||= load_configuration(config_directory, env.is_a?(String) ? env : environment)
|
8
|
+
end
|
9
|
+
|
10
|
+
def environment=(env)
|
11
|
+
@environment = env
|
12
|
+
end
|
13
|
+
|
14
|
+
def config_directory=(dir)
|
15
|
+
@config_directory = dir
|
16
|
+
end
|
17
|
+
|
18
|
+
def environment
|
19
|
+
@environment || raise("environment should be defined")
|
20
|
+
end
|
21
|
+
|
22
|
+
def config_directory
|
23
|
+
@config_directory || raise("config_directory should be defined")
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
def load_configuration(dir, env)
|
29
|
+
config = load_from_file(dir, env)
|
30
|
+
|
31
|
+
config.deep_merge!(load_from_directory(dir, env))
|
32
|
+
|
33
|
+
Configuration.new(config)
|
34
|
+
end
|
35
|
+
|
36
|
+
def load_from_file(dir, env)
|
37
|
+
config = {}
|
38
|
+
|
39
|
+
if File.exists?(file = "#{dir}.yml")
|
40
|
+
configurations = YAML::load(ERB.new(IO.read(file)).result)
|
41
|
+
config = configurations[:default] || {}
|
42
|
+
config.deep_merge!(configurations[env] || {})
|
43
|
+
end
|
44
|
+
|
45
|
+
config
|
46
|
+
end
|
47
|
+
|
48
|
+
def load_from_directory(dir, env)
|
49
|
+
config = {}
|
50
|
+
|
51
|
+
if File.directory?(dir)
|
52
|
+
Dir["#{dir}/*"].each do |entry|
|
53
|
+
namespace = entry.gsub(/^#{dir}\/?/, '').gsub(/\.yml$/, '')
|
54
|
+
config.deep_merge!(namespace => load_configuration(entry.gsub(/\.yml$/, ''), env))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
config
|
59
|
+
end
|
60
|
+
|
61
|
+
def method_missing(method, *args, &block)
|
62
|
+
configuration.key?(method) ? configuration[method] : super
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "forwardable"
|
2
|
+
|
3
|
+
module Global
|
4
|
+
class Configuration
|
5
|
+
extend Forwardable
|
6
|
+
|
7
|
+
attr_reader :hash
|
8
|
+
|
9
|
+
def_delegators :hash, :to_hash, :key?, :[], :[]=, :inspect
|
10
|
+
|
11
|
+
|
12
|
+
def initialize(hash)
|
13
|
+
@hash = hash.respond_to?(:with_indifferent_access) ? hash.with_indifferent_access : hash
|
14
|
+
end
|
15
|
+
|
16
|
+
protected
|
17
|
+
|
18
|
+
def method_missing(method, *args, &block)
|
19
|
+
key?(method) ? hash[method] : super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Global::Configuration do
|
4
|
+
let(:hash){ { "key" => "value" } }
|
5
|
+
let(:configuration){ described_class.new hash }
|
6
|
+
|
7
|
+
describe "#hash" do
|
8
|
+
subject{ configuration.hash }
|
9
|
+
|
10
|
+
it{ should == hash }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#to_hash" do
|
14
|
+
subject{ configuration.to_hash }
|
15
|
+
|
16
|
+
it{ should == hash }
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "key?" do
|
20
|
+
subject{ configuration.key?(:key) }
|
21
|
+
|
22
|
+
it{ should be_true }
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#[]" do
|
26
|
+
subject{ configuration[:key] }
|
27
|
+
|
28
|
+
it{ should == "value" }
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#[]=" do
|
32
|
+
subject{ configuration[:new_key] }
|
33
|
+
|
34
|
+
before{ configuration[:new_key] = "new_value" }
|
35
|
+
|
36
|
+
it{ should == "new_value" }
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#inspect" do
|
40
|
+
subject{ configuration.inspect }
|
41
|
+
|
42
|
+
it{ should == hash.inspect }
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#method_missing" do
|
46
|
+
context "when key exist" do
|
47
|
+
subject{ configuration.key }
|
48
|
+
|
49
|
+
it{ should == "value" }
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when key does not exist" do
|
53
|
+
subject{ configuration.some_key }
|
54
|
+
|
55
|
+
it{ lambda { subject }.should raise_error(NoMethodError) }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/spec/global_spec.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Global do
|
4
|
+
before(:each) do
|
5
|
+
described_class.environment = "test"
|
6
|
+
described_class.config_directory = File.join(Dir.pwd, "spec/files")
|
7
|
+
end
|
8
|
+
|
9
|
+
describe ".environment" do
|
10
|
+
subject{ described_class.environment }
|
11
|
+
|
12
|
+
it{ should == "test" }
|
13
|
+
|
14
|
+
context "when undefined" do
|
15
|
+
before{ described_class.environment = nil }
|
16
|
+
|
17
|
+
it{ lambda{ subject }.should raise_error("environment should be defined") }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe ".config_directory" do
|
22
|
+
subject{ described_class.config_directory }
|
23
|
+
|
24
|
+
it{ should == File.join(Dir.pwd, "spec/files")}
|
25
|
+
|
26
|
+
context "when undefined" do
|
27
|
+
before{ described_class.config_directory = nil }
|
28
|
+
|
29
|
+
it{ lambda{ subject }.should raise_error("config_directory should be defined") }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe ".configuration" do
|
34
|
+
subject{ described_class.configuration }
|
35
|
+
|
36
|
+
it{ should be_instance_of(Global::Configuration) }
|
37
|
+
|
38
|
+
context "when load from directory" do
|
39
|
+
its("rspec_config.to_hash"){ should == {"default_value"=>"default value", "test_value"=>"test value"} }
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when load from file" do
|
43
|
+
before{ described_class.config_directory = File.join(Dir.pwd, "spec/files/rspec_config") }
|
44
|
+
|
45
|
+
its("rspec_config.to_hash"){ should == {"default_value"=>"default value", "test_value"=>"test value"} }
|
46
|
+
end
|
47
|
+
|
48
|
+
context "when nested directories" do
|
49
|
+
it{ subject.rspec["config"].to_hash.should == {"default_value"=>"default nested value", "test_value"=>"test nested value"} }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe ".method_missing" do
|
54
|
+
context "when file exists" do
|
55
|
+
subject{ described_class.rspec_config }
|
56
|
+
|
57
|
+
it{ should be_kind_of(Global::Configuration) }
|
58
|
+
end
|
59
|
+
|
60
|
+
context "when file does not exist" do
|
61
|
+
subject{ described_class.some_file }
|
62
|
+
|
63
|
+
it{ lambda{ subject }.should raise_error(NoMethodError) }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
|
3
|
+
SimpleCov.start do
|
4
|
+
add_filter '/spec/'
|
5
|
+
|
6
|
+
add_group 'Libraries', '/lib/'
|
7
|
+
end
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
require "global"
|
11
|
+
|
12
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
13
|
+
config.run_all_when_everything_filtered = true
|
14
|
+
config.filter_run :focus
|
15
|
+
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: global
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- paladiy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.14.1
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.14.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: simplecov
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.7.1
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.7.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 10.1.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 10.1.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Simple way to load your configs from yaml
|
70
|
+
email: olexanderpaladiy@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- .gitignore
|
76
|
+
- .rspec
|
77
|
+
- .ruby-version
|
78
|
+
- .travis.yml
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- global.gemspec
|
84
|
+
- lib/global.rb
|
85
|
+
- lib/global/base.rb
|
86
|
+
- lib/global/configuration.rb
|
87
|
+
- lib/global/version.rb
|
88
|
+
- spec/files/rspec/config.yml
|
89
|
+
- spec/files/rspec_config.yml
|
90
|
+
- spec/global/configuration_spec.rb
|
91
|
+
- spec/global_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
homepage: http://github.com/paladiy/global
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project: global
|
113
|
+
rubygems_version: 2.0.4
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Simple way to load your configs from yaml
|
117
|
+
test_files:
|
118
|
+
- spec/files/rspec/config.yml
|
119
|
+
- spec/files/rspec_config.yml
|
120
|
+
- spec/global/configuration_spec.rb
|
121
|
+
- spec/global_spec.rb
|
122
|
+
- spec/spec_helper.rb
|