configict 0.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.
- data/.gitignore +19 -0
- data/Gemfile +8 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/configict.gemspec +21 -0
- data/lib/configict.rb +47 -0
- data/lib/configict/version.rb +3 -0
- data/test/fixtures/config_loader.yml +8 -0
- data/test/fixtures/config_loader.yml.example +8 -0
- data/test/fixtures/config_loader_has_diff.yml +9 -0
- data/test/fixtures/config_loader_has_diff.yml.sample +9 -0
- data/test/run-test.rb +9 -0
- data/test/test_configict.rb +13 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
configict
|
2
|
+
===========
|
3
|
+
|
4
|
+
A YAML config loader with checking conflict from a example.
|
5
|
+
|
6
|
+
## Features
|
7
|
+
|
8
|
+
* You can load a config .yml file and get a hash from YAML.
|
9
|
+
* Notify the conflict between .yml and .yml.(example|sample)
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
# config.yml(not committed)
|
14
|
+
name: "taro"
|
15
|
+
|
16
|
+
# config.yml.sample(committed)
|
17
|
+
name: "sample name"
|
18
|
+
|
19
|
+
You can load config.yml.
|
20
|
+
|
21
|
+
require "configict"
|
22
|
+
p Configict.load_yml("config.yml")
|
23
|
+
# => {name: "taro"}
|
24
|
+
|
25
|
+
Then, other developer updates config.yml.sample.
|
26
|
+
|
27
|
+
# config.yml.sample(committed)
|
28
|
+
fullname: "sample fullname"
|
29
|
+
age: 20
|
30
|
+
|
31
|
+
Configict raises a confiction error when you load it.
|
32
|
+
|
33
|
+
require "configict"
|
34
|
+
p Configict.load_yml("config.yml")
|
35
|
+
# => RuntimeError:
|
36
|
+
# You must specify follow keys in config file (config.yml)
|
37
|
+
# - fullname
|
38
|
+
# - age
|
39
|
+
#
|
40
|
+
# You must not specify follow keys in config file (config.yml):
|
41
|
+
# + name
|
data/Rakefile
ADDED
data/configict.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "configict/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "configict"
|
7
|
+
s.version = Configict::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Narihiro Nakamura"]
|
10
|
+
s.email = ["authornari@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/authorNari/configict"
|
12
|
+
s.summary = %q{YAML config loader with checking conflict from example}
|
13
|
+
s.description = %q{YAML config loader with checking conflict from example}
|
14
|
+
|
15
|
+
s.rubyforge_project = "configict"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
data/lib/configict.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "erb"
|
2
|
+
require "yaml"
|
3
|
+
|
4
|
+
module Configict
|
5
|
+
class LoadError < StandardError; end
|
6
|
+
def self.load_yml(path)
|
7
|
+
config = YAML.load(ERB.new(File.read(path)).result)
|
8
|
+
example_config = example_config(path)
|
9
|
+
if example_config
|
10
|
+
diff = diff_keys(config, example_config)
|
11
|
+
if !diff["-"].empty? || !diff["+"].empty?
|
12
|
+
res = ["You must specify follow keys in config file (#{path}):"]
|
13
|
+
res += diff['-'].map{|d| "- #{d}"}
|
14
|
+
res << ""
|
15
|
+
res << "You must not specify follow keys in config file (#{path}):"
|
16
|
+
res += diff['+'].map{|d| "+ #{d}"}
|
17
|
+
raise LoadError, "\n" + res.join("\n")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
return config
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def self.example_config(path)
|
25
|
+
%w(.sample .example).each do |s|
|
26
|
+
sample = path + s
|
27
|
+
if File.exists?(sample)
|
28
|
+
return YAML.load(ERB.new(File.read(sample)).result)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
return nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.diff_keys(orig, other)
|
35
|
+
res = {'-' => [], '+' => []}
|
36
|
+
res['+'] = (orig.keys - other.keys).map(&:to_s)
|
37
|
+
res['-'] = (other.keys - orig.keys).map(&:to_s)
|
38
|
+
(orig.keys & other.keys).each do |k|
|
39
|
+
if orig[k].is_a?(Hash) && other[k].is_a?(Hash)
|
40
|
+
d = diff_keys(orig[k], other[k])
|
41
|
+
res['+'] << ([k, d['+']].join(" -> ")) if not d['+'].empty?
|
42
|
+
res['-'] << ([k, d['-']].join(" -> ")) if not d['-'].empty?
|
43
|
+
end
|
44
|
+
end
|
45
|
+
return res
|
46
|
+
end
|
47
|
+
end
|
data/test/run-test.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
class TestConfigict < Test::Unit::TestCase
|
2
|
+
def test_load_yml
|
3
|
+
Configict.load_yml(File.join(
|
4
|
+
File.dirname(__FILE__),
|
5
|
+
"fixtures/config_loader.yml"))
|
6
|
+
|
7
|
+
assert_raise(Configict::LoadError) do
|
8
|
+
Configict.load_yml(File.join(
|
9
|
+
File.dirname(__FILE__),
|
10
|
+
"fixtures/config_loader_has_diff.yml"))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: configict
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Narihiro Nakamura
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-03 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: YAML config loader with checking conflict from example
|
15
|
+
email:
|
16
|
+
- authornari@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- configict.gemspec
|
26
|
+
- lib/configict.rb
|
27
|
+
- lib/configict/version.rb
|
28
|
+
- test/fixtures/config_loader.yml
|
29
|
+
- test/fixtures/config_loader.yml.example
|
30
|
+
- test/fixtures/config_loader_has_diff.yml
|
31
|
+
- test/fixtures/config_loader_has_diff.yml.sample
|
32
|
+
- test/run-test.rb
|
33
|
+
- test/test_configict.rb
|
34
|
+
homepage: https://github.com/authorNari/configict
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project: configict
|
54
|
+
rubygems_version: 1.8.6
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: YAML config loader with checking conflict from example
|
58
|
+
test_files: []
|