strict_konf 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 293820438c4c79b061924c550a228b57f48f6807
4
+ data.tar.gz: f6161b314ef615d3030cadb8335e67780b4aee88
5
+ SHA512:
6
+ metadata.gz: ec62272bca6d967e86bdc124677966de5029f3df8eec697a6f7dd948e7bba76b5304dae43666633202d2556497721c91850b456c522fb5e75503e9c78429860b
7
+ data.tar.gz: bcfa9bd708221358a1275cad2503a9e9e66d7b20928e2b1ce4318492fd4ce38cb47cfa8e3c739ac459345a2a15f7454f765a4f08b5825515f430d0ddd027e355
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0.0
4
+ - 2.1.0
5
+ - rbx-2
@@ -0,0 +1,6 @@
1
+ 1. Fork it ( http://github.com/wojtekmach/strict_konf/fork )
2
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
3
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
4
+ 4. Push to the branch (`git push origin my-new-feature`)
5
+ 5. Create new Pull Request
6
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in strict_konf.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Wojtek Mach
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.
@@ -0,0 +1,55 @@
1
+ # StrictKonf
2
+
3
+ Extend Konf with required keys
4
+
5
+ [![Build Status](http://img.shields.io/travis/wojtekmach/strict_konf.svg)][travis]
6
+
7
+ [travis]: https://travis-ci.org/wojtekmach/strict_konf
8
+
9
+ ## Rationale
10
+
11
+ It's very annoying when configuration files get out of sync, i.e.: you have a developer's `config.yml` that is ignored, `config.yml.sample` checked-in and another `config.yml` on the production box. Problems arise when the developer, when adding a new key, forgets to update `.sample` file or the production config and the code & configuration is out of sync.
12
+
13
+ This gem extends Konf to fail when there is a discrepancy between the required list of keys and the actual configuration file content.
14
+
15
+ ## Example
16
+
17
+ Based on <https://github.com/GBH/konf#usage>
18
+
19
+ ```yaml
20
+ development:
21
+ name: Dev
22
+ email: dev@test.test
23
+ ```
24
+
25
+ You can read the file like usual with Konf:
26
+
27
+ ```ruby
28
+ config = StrictKonf.new('configuration.yml', 'development', [:name, :email])
29
+ config.name # => Dev
30
+ ```
31
+
32
+ It will fail when you have too much or too few keys in your configuration:
33
+
34
+ ```ruby
35
+ StrictKonf.new('configuration.yml', 'development', [:name]) # raises UnknownKeys error
36
+ StrictKonf.new('configuration.yml', 'development', [:name, :email, :role]) # raises NotFound error
37
+ ```
38
+
39
+ It also supports nested hashes. We can pass `nil` to start from the root of the hash:
40
+
41
+ ```ruby
42
+ StrictKonf.new('configuration.yml', nil, development: [:name, :email])
43
+ ```
44
+
45
+ ## Installation
46
+
47
+ Add this line to your application's Gemfile:
48
+
49
+ ```ruby
50
+ gem 'strict_konf', github: 'wojtekmach/strict_konf'
51
+ ```
52
+
53
+ And then execute:
54
+
55
+ $ bundle
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'lib'
6
+ t.pattern = 'test/**/*_test.rb'
7
+ end
8
+
9
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,61 @@
1
+ require "konf"
2
+
3
+ class StrictKonf < Konf
4
+ VERSION = File.read(File.dirname(__FILE__) + '/../VERSION').strip
5
+
6
+ class UnknownKeys < StandardError; end
7
+
8
+ def initialize(source, root, required_keys)
9
+ super(source, root)
10
+ @required_keys = required_keys
11
+
12
+ _validate_required_keys
13
+ _validate_provided_keys
14
+ end
15
+
16
+ def method_missing(name, *args, &block)
17
+ result = super
18
+ if result.is_a? Konf
19
+ StrictKonf.new(result, nil, _normalize_keys.fetch(name))
20
+ else
21
+ result
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def _validate_required_keys
28
+ keys = @required_keys
29
+ keys = keys.keys if keys.is_a? Hash
30
+
31
+ keys.each { |key|
32
+ if key.is_a? Symbol
33
+ public_send(key.to_s)
34
+ end
35
+ }
36
+ end
37
+
38
+ def _validate_provided_keys
39
+ required = @required_keys
40
+ required = required.keys if keys.is_a? Hash
41
+ required = required.map { |key| key.is_a?(Hash) ? key.keys : key }
42
+ required = required.flatten.map(&:to_s)
43
+
44
+ extra = keys - required
45
+ if extra.any?
46
+ raise UnknownKeys, "Unknown keys in config: #{extra}"
47
+ end
48
+ end
49
+
50
+ def _normalize_keys
51
+ @required_keys.each_with_object({}) {|key, hash|
52
+ if key.is_a? Hash
53
+ hash.merge!(key)
54
+ elsif key.is_a? Array
55
+ hash[key[0]] = key[1]
56
+ else
57
+ hash[key] = true
58
+ end
59
+ }
60
+ end
61
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ version = File.read(File.dirname(__FILE__) + '/VERSION').strip
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "strict_konf"
8
+ spec.version = version
9
+ spec.authors = ["Wojtek Mach"]
10
+ spec.email = ["wojtek@wojtekmach.pl"]
11
+ spec.summary = "Extend Konf to require/whitelist fields"
12
+ spec.description = ""
13
+ spec.homepage = ""
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.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "konf", "0.0.2"
24
+ end
@@ -0,0 +1,57 @@
1
+ require 'minitest/autorun'
2
+ require 'strict_konf'
3
+
4
+ describe StrictKonf do
5
+ it "behaves like Konf" do
6
+ config = StrictKonf.new({'development' => {'foo' => 1, 'bar' => 2}}, 'development', [:foo, :bar])
7
+ config.foo.must_equal 1
8
+ config.bar.must_equal 2
9
+ end
10
+
11
+ it "raises error when required key is missing" do
12
+ lambda {
13
+ StrictKonf.new({'foo' => 1}, nil, [:foo, :baz])
14
+ }.must_raise Konf::NotFound
15
+ end
16
+
17
+ it "raises error when config has key that is not a required key" do
18
+ lambda {
19
+ StrictKonf.new({'foo' => 1, 'bar' => 2}, nil, [:foo])
20
+ }.must_raise StrictKonf::UnknownKeys
21
+ end
22
+
23
+ describe "with nested hash" do
24
+ let(:data) {
25
+ {
26
+ 'a' => {
27
+ 'aa' => 1,
28
+ 'ab' => 2,
29
+ 'ac' => {
30
+ 'aca' => 3
31
+ }
32
+ },
33
+ 'b' => 4,
34
+ }
35
+ }
36
+
37
+ it "behaves like Konf" do
38
+ config = StrictKonf.new(data, nil, a: [:aa, :ab, {ac: [:aca]}], b: true)
39
+ config.a.aa.must_equal 1
40
+ config.a.ab.must_equal 2
41
+ config.a.ac.aca.must_equal 3
42
+ config.b.must_equal 4
43
+ end
44
+
45
+ it "raises error when required key is missing" do
46
+ lambda {
47
+ StrictKonf.new(data, nil, a: [:aa, :ab, {ac: [:aca]}, :ad], b: true)
48
+ }.must_raise Konf::NotFound
49
+ end
50
+
51
+ it "raises error when config has key that is not a required key" do
52
+ lambda {
53
+ StrictKonf.new(data, nil, a: [:aa], b: true)
54
+ }.must_raise StrictKonf::UnknownKeys
55
+ end
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strict_konf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Wojtek Mach
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-06 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
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: konf
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.2
55
+ description: ''
56
+ email:
57
+ - wojtek@wojtekmach.pl
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CONTRIBUTING.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - VERSION
70
+ - lib/strict_konf.rb
71
+ - strict_konf.gemspec
72
+ - test/strict_konf_test.rb
73
+ homepage: ''
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Extend Konf to require/whitelist fields
97
+ test_files:
98
+ - test/strict_konf_test.rb