configict 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+ Gemfile.lock
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in configict.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'test-unit'
8
+ end
@@ -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
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -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
@@ -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
@@ -0,0 +1,3 @@
1
+ module Configict
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ test1: 'a'
2
+ test2:
3
+ test2-1: 'a'
4
+ test2-2:
5
+ test2-2-1: 'b'
6
+ test3:
7
+ - t
8
+ - s
@@ -0,0 +1,8 @@
1
+ test1: 'a'
2
+ test2:
3
+ test2-1: 'a'
4
+ test2-2:
5
+ test2-2-1: 'b'
6
+ test3:
7
+ - t
8
+ - s
@@ -0,0 +1,9 @@
1
+ test1: 'a'
2
+ test2:
3
+ test2-1: 'a'
4
+ test2-2:
5
+ test2-2-1-ex: 'b'
6
+ test2-3: 'c'
7
+ test3:
8
+ - t
9
+ - s
@@ -0,0 +1,9 @@
1
+ test1: 'a'
2
+ test2:
3
+ test2-1: 'a'
4
+ test2-2:
5
+ test2-2-1: 'b'
6
+ test3:
7
+ - t
8
+ - s
9
+ test4: 'aa'
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ require "rubygems"
3
+ require "bundler"
4
+ Bundler.require(:default, :test)
5
+
6
+ test_file = "./test/test_*.rb"
7
+ Dir.glob(test_file) do |file|
8
+ require file
9
+ end
@@ -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: []