armoire 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +6 -0
- data/armoire.gemspec +25 -0
- data/lib/armoire/railtie.rb +7 -0
- data/lib/armoire/setting.rb +18 -0
- data/lib/armoire/version.rb +3 -0
- data/lib/armoire.rb +36 -0
- data/spec/fixtures/application.yml +6 -0
- data/spec/fixtures/custom_load.yml +2 -0
- data/spec/lib/armoire/setting_spec.rb +44 -0
- data/spec/lib/armoire_spec.rb +122 -0
- data/spec/spec_helper.rb +15 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 42a40ddec2fa56e735333d7637b35fa2c2cf0888
|
4
|
+
data.tar.gz: 3aa33261a4a659f3aaa01b10ce90a2d21dc5340f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f2a1e5a29ab8c10fb9a0f85f6e51d5c9c545840c4ae09f4e882a5eaa1a8a6aedb8840519854c0d0a881830e2e7e5f959f6ed3cc328783bbe26ac80dd44a87edb
|
7
|
+
data.tar.gz: 9afb5e3ab52352fbbbbdb74418d9e2dda6d146acd61df59ca555c196bb45564c63873c45c624390a3925c9993887e3d355a4a9d3cfe8a84f85500bdbd60a8657
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Michael Smith
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Armoire [](https://travis-ci.org/mikespokefire/armoire) [](https://codeclimate.com/github/mikespokefire/armoire)
|
2
|
+
|
3
|
+
A simple configuration tool for your ruby application settings
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'configuration'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install configuration
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Armoire should be simple. Simply create a yaml file in `config/application.yml` with the following:
|
22
|
+
|
23
|
+
development:
|
24
|
+
foo: "bar"
|
25
|
+
baz: 42
|
26
|
+
nested_options:
|
27
|
+
nested_foo: "nested_bar"
|
28
|
+
nested_baz: 99.999
|
29
|
+
boolean_value: true
|
30
|
+
parsed_things: <% "something_in_erb" %>
|
31
|
+
|
32
|
+
test:
|
33
|
+
foo: "bar"
|
34
|
+
baz: 42
|
35
|
+
nested_options:
|
36
|
+
nested_foo: "nested bar"
|
37
|
+
nested_baz: 99.999
|
38
|
+
boolean_value: true
|
39
|
+
parsed_things: <% "something_in_erb" %>
|
40
|
+
|
41
|
+
Simply call `Armoire["foo"]` to get `"bar"` or `Armoire["nested_options"]["nested_foo"]` to get `"nested_bar"`. Any ERB will be parsed when the config file is loaded. If a configuration option is missing, it will throw an `Armoire::ConfigSettingMissing` exception.
|
42
|
+
|
43
|
+
The configuration environment will initially be taken from `ENV['RAILS_ENV']`, then `ENV['RACK_ENV']` and if neither exist then it will fall back to `development`
|
44
|
+
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/armoire.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'armoire/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "armoire"
|
8
|
+
spec.version = Armoire::VERSION
|
9
|
+
spec.authors = ["Michael Smith"]
|
10
|
+
spec.email = ["mike@spokefire.co.uk"]
|
11
|
+
spec.description = %q{A simple configuration tool}
|
12
|
+
spec.summary = %q{A simple configuration tool}
|
13
|
+
spec.homepage = "https://github.com/mikespokefire/armoire"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.13"
|
24
|
+
spec.add_development_dependency "simplecov", "~> 0.7"
|
25
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Armoire
|
2
|
+
class Setting
|
3
|
+
def initialize(setting)
|
4
|
+
@setting = setting
|
5
|
+
end
|
6
|
+
|
7
|
+
def [](key)
|
8
|
+
value = setting.fetch(key) do
|
9
|
+
raise ConfigSettingMissing, %Q{"#{key}" is not set}
|
10
|
+
end
|
11
|
+
|
12
|
+
value.kind_of?(Hash) ? self.class.new(value) : value
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
attr_reader :setting
|
17
|
+
end
|
18
|
+
end
|
data/lib/armoire.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
require "armoire/setting"
|
5
|
+
require "armoire/version"
|
6
|
+
require "armoire/railtie" if defined?(Rails)
|
7
|
+
|
8
|
+
class Armoire
|
9
|
+
include Singleton
|
10
|
+
|
11
|
+
attr_accessor :settings
|
12
|
+
|
13
|
+
def environment
|
14
|
+
ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.[](key)
|
18
|
+
instance.settings[key]
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.load!(path_to_config_file)
|
22
|
+
instance.settings = Setting.new(instance.load_settings(path_to_config_file))
|
23
|
+
end
|
24
|
+
|
25
|
+
def load_settings(path_to_config_file)
|
26
|
+
YAML.load(ERB.new(File.read(path_to_config_file)).result)[environment]
|
27
|
+
rescue Errno::ENOENT => e
|
28
|
+
raise MissingSettingsFile.new('The settings file cannot be found')
|
29
|
+
end
|
30
|
+
|
31
|
+
# When the settings file cannot be read, this exception will be raised
|
32
|
+
class MissingSettingsFile < StandardError; end
|
33
|
+
|
34
|
+
# When a config setting isn't set, this exception will be raised
|
35
|
+
class ConfigSettingMissing < StandardError; end
|
36
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Armoire::Setting do
|
4
|
+
let(:setting) do
|
5
|
+
described_class.new({
|
6
|
+
"simple" => "simple value",
|
7
|
+
"nested" => {
|
8
|
+
"config" => "nested config value"
|
9
|
+
}
|
10
|
+
})
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#[]' do
|
14
|
+
context 'setting is a value' do
|
15
|
+
subject { setting["simple"] }
|
16
|
+
|
17
|
+
it 'returns the value' do
|
18
|
+
expect(subject).to eql("simple value")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'setting is a hash' do
|
23
|
+
subject { setting["nested"] }
|
24
|
+
|
25
|
+
it 'returns a new setting object' do
|
26
|
+
expect(subject).to be_an_instance_of(described_class)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'it can be chained' do
|
31
|
+
subject { setting["nested"]["config"] }
|
32
|
+
it 'returns the nested value' do
|
33
|
+
expect(subject).to eql("nested config value")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'missing key' do
|
38
|
+
subject { setting["missing_setting"] }
|
39
|
+
it "raises an error" do
|
40
|
+
expect { subject }.to raise_error(Armoire::ConfigSettingMissing, '"missing_setting" is not set')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Armoire do
|
4
|
+
describe '#.environment' do
|
5
|
+
subject { described_class.instance.environment }
|
6
|
+
|
7
|
+
context "ENV['RAILS_ENV'] is set" do
|
8
|
+
before do
|
9
|
+
@prev_rails_env = ENV['RAILS_ENV']
|
10
|
+
ENV['RAILS_ENV'] = 'rails_env'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns the result of ENV['RAILS_ENV'] as first priority" do
|
14
|
+
expect(subject).to eql('rails_env')
|
15
|
+
end
|
16
|
+
|
17
|
+
after do
|
18
|
+
ENV['RAILS_ENV'] = @prev_rails_env
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "ENV['RACK_ENV'] is set" do
|
23
|
+
before do
|
24
|
+
@prev_rails_env = ENV['RAILS_ENV']
|
25
|
+
@prev_rack_env = ENV['RACK_ENV']
|
26
|
+
ENV['RAILS_ENV'] = nil
|
27
|
+
ENV['RACK_ENV'] = 'rack_env'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "returns the result of ENV['RACK_ENV'] as second priority" do
|
31
|
+
expect(subject).to eql('rack_env')
|
32
|
+
end
|
33
|
+
|
34
|
+
after do
|
35
|
+
ENV['RAILS_ENV'] = @prev_rails_env
|
36
|
+
ENV['RACK_ENV'] = @prev_rack_env
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "nothing is set" do
|
41
|
+
before do
|
42
|
+
@prev_rails_env = ENV['RAILS_ENV']
|
43
|
+
@prev_rack_env = ENV['RACK_ENV']
|
44
|
+
ENV['RAILS_ENV'] = nil
|
45
|
+
ENV['RACK_ENV'] = nil
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns development as a fallback if nothing else is set" do
|
49
|
+
expect(subject).to eql("development")
|
50
|
+
end
|
51
|
+
|
52
|
+
after do
|
53
|
+
ENV['RAILS_ENV'] = @prev_rails_env
|
54
|
+
ENV['RACK_ENV'] = @prev_rack_env
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#[]' do
|
60
|
+
context "simple config option" do
|
61
|
+
subject { Armoire["simple_config_option"] }
|
62
|
+
|
63
|
+
it { expect(subject).to eql("simple_config_option_value") }
|
64
|
+
end
|
65
|
+
|
66
|
+
context "erb config option" do
|
67
|
+
subject { Armoire["erb_config_option"] }
|
68
|
+
|
69
|
+
it { expect(subject).to eql("erb_config_option_value") }
|
70
|
+
end
|
71
|
+
|
72
|
+
context "nested config option" do
|
73
|
+
subject { Armoire["nested"]["config"]["option"] }
|
74
|
+
|
75
|
+
it { expect(subject).to eql("nested_config_option_value") }
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'config setting is missing' do
|
79
|
+
subject { Armoire["missing_setting"] }
|
80
|
+
|
81
|
+
it "raises an error" do
|
82
|
+
expect { subject }.to raise_error(Armoire::ConfigSettingMissing, '"missing_setting" is not set')
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'nested config setting is missing' do
|
87
|
+
subject { Armoire["nested"]["config"]["missing_setting"] }
|
88
|
+
|
89
|
+
it "raises an error" do
|
90
|
+
expect { subject }.to raise_error(Armoire::ConfigSettingMissing, '"missing_setting" is not set')
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '.load!' do
|
96
|
+
context 'setting files exists' do
|
97
|
+
before do
|
98
|
+
Armoire.load! File.expand_path('../fixtures/custom_load.yml', File.dirname(__FILE__))
|
99
|
+
end
|
100
|
+
|
101
|
+
it "returns the new config settings" do
|
102
|
+
expect(Armoire["custom_config_option"]).to eql("custom_config_option_value")
|
103
|
+
end
|
104
|
+
|
105
|
+
it "doesn't keep the original settings" do
|
106
|
+
expect { Armoire["simple_config_option"] }.to raise_error(Armoire::ConfigSettingMissing, '"simple_config_option" is not set')
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context 'settings file does not exist' do
|
111
|
+
subject { Armoire.load! File.expand_path('../fixtures/missing_file.yml', File.dirname(__FILE__)) }
|
112
|
+
|
113
|
+
it "returns an exception" do
|
114
|
+
expect { subject }.to raise_error(Armoire::MissingSettingsFile, 'The settings file cannot be found')
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
after do
|
119
|
+
Armoire.load! File.expand_path('../fixtures/application.yml', File.dirname(__FILE__))
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
ENV['RACK_ENV'] = 'test'
|
2
|
+
|
3
|
+
require 'simplecov'
|
4
|
+
SimpleCov.start
|
5
|
+
|
6
|
+
require 'armoire'
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
10
|
+
config.order = 'random'
|
11
|
+
|
12
|
+
config.before(:all) do
|
13
|
+
Armoire.load! File.expand_path('fixtures/application.yml', File.dirname(__FILE__))
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: armoire
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Smith
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.13'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.13'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.7'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.7'
|
69
|
+
description: A simple configuration tool
|
70
|
+
email:
|
71
|
+
- mike@spokefire.co.uk
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- .ruby-version
|
79
|
+
- .travis.yml
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- armoire.gemspec
|
85
|
+
- lib/armoire.rb
|
86
|
+
- lib/armoire/railtie.rb
|
87
|
+
- lib/armoire/setting.rb
|
88
|
+
- lib/armoire/version.rb
|
89
|
+
- spec/fixtures/application.yml
|
90
|
+
- spec/fixtures/custom_load.yml
|
91
|
+
- spec/lib/armoire/setting_spec.rb
|
92
|
+
- spec/lib/armoire_spec.rb
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
homepage: https://github.com/mikespokefire/armoire
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.0.3
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: A simple configuration tool
|
118
|
+
test_files:
|
119
|
+
- spec/fixtures/application.yml
|
120
|
+
- spec/fixtures/custom_load.yml
|
121
|
+
- spec/lib/armoire/setting_spec.rb
|
122
|
+
- spec/lib/armoire_spec.rb
|
123
|
+
- spec/spec_helper.rb
|