yaml_pack 0.0.2.alpha → 0.0.3.alpha
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/lib/yaml_pack.rb +3 -90
- data/lib/yaml_pack/util.rb +22 -0
- data/lib/yaml_pack/version.rb +3 -0
- data/lib/yaml_pack/yaml_pack.rb +83 -0
- data/spec/yaml_pack/util_spec.rb +13 -0
- data/spec/yaml_pack/yaml_pack_spec.rb +0 -11
- data/yaml_pack.gemspec +1 -1
- metadata +9 -5
- data/lib/core_ext/hash.rb +0 -17
data/lib/yaml_pack.rb
CHANGED
@@ -1,91 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'yaml_pack/yaml_pack'
|
2
|
+
require 'yaml_pack/util'
|
3
|
+
require 'yaml_pack/version'
|
2
4
|
|
3
|
-
class YamlPack
|
4
|
-
VERSION = "0.0.2.alpha"
|
5
|
-
|
6
|
-
KEY_CONVERTERS = {
|
7
|
-
:symbolize => Proc.new{|key, previous_keys| key.respond_to?(:to_sym) ? key.to_sym : key }
|
8
|
-
}.freeze
|
9
|
-
|
10
|
-
attr_reader :files, :base_dir
|
11
|
-
|
12
|
-
def initialize(files, opts = {})
|
13
|
-
@files = [files].flatten
|
14
|
-
|
15
|
-
@base_dir = opts.fetch(:base_dir, nil)
|
16
|
-
@with_erb = opts.fetch(:erb, true)
|
17
|
-
@key_converter = opts.fetch(:key_converter, nil)
|
18
|
-
@header_file = opts.fetch(:header_file, nil)
|
19
|
-
|
20
|
-
if KEY_CONVERTERS.has_key?(@key_converter)
|
21
|
-
@key_converter = KEY_CONVERTERS[@key_converter]
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def load_deep_merged
|
26
|
-
hsh = {}
|
27
|
-
|
28
|
-
files.select{|f| File.exists?(f) }.each do |f|
|
29
|
-
content = self.content(f)
|
30
|
-
content = ERB.new(content).result(binding) if @with_erb
|
31
|
-
|
32
|
-
result = YAML::load(content) || {}
|
33
|
-
result = prepend_subfolders(f, result) if @base_dir
|
34
|
-
result = convert_keys_recursive(result) if @key_converter
|
35
|
-
|
36
|
-
hsh.yaml_pack_deep_merge!(result)
|
37
|
-
end
|
38
|
-
|
39
|
-
hsh
|
40
|
-
end
|
41
|
-
|
42
|
-
# Include the contents of given file into yaml template.
|
43
|
-
# Use this inside your yaml-file.
|
44
|
-
#
|
45
|
-
# <%= include_file('/path/to/file.yml') %>
|
46
|
-
#
|
47
|
-
# DEBT: extract into own module/class
|
48
|
-
#
|
49
|
-
def include_file(file_path)
|
50
|
-
File.read(file_path)
|
51
|
-
end
|
52
|
-
|
53
|
-
def content(file_path)
|
54
|
-
body = File.read(file_path) || ""
|
55
|
-
[header, body].join("\n")
|
56
|
-
end
|
57
|
-
|
58
|
-
def header
|
59
|
-
(@header_file && File.exists?(@header_file)) ? File.read(@header_file) : ""
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.subfolders(base_dir, file_path)
|
63
|
-
base_dir = "#{base_dir}/".gsub("//", '/') # hack to always have a trailing /
|
64
|
-
file_path.gsub(/^#{base_dir}/, '').split('/')[0...-1]
|
65
|
-
end
|
66
|
-
|
67
|
-
protected
|
68
|
-
|
69
|
-
def convert_keys_recursive(result, previous_keys = [])
|
70
|
-
if result.is_a?(Hash)
|
71
|
-
hsh = {}
|
72
|
-
result.each do |key, value|
|
73
|
-
converted_key = @key_converter.call(key, previous_keys)
|
74
|
-
hsh[converted_key] = convert_keys_recursive(value, [*previous_keys, key])
|
75
|
-
end
|
76
|
-
hsh
|
77
|
-
else # If it's not a hash, return the object itself (end of recursion)
|
78
|
-
result
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def prepend_subfolders(file_path, object)
|
83
|
-
subfolders(file_path).inject(object) do |object, subfolder|
|
84
|
-
{subfolder => object}
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
def subfolders(file_path)
|
89
|
-
self.class.subfolders(base_dir, file_path)
|
90
|
-
end
|
91
|
-
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class YamlPack::Util
|
2
|
+
|
3
|
+
def self.subfolders(base_dir, file_path)
|
4
|
+
base_dir = "#{base_dir}/".gsub("//", '/') # hack to always have a trailing /
|
5
|
+
file_path.gsub(/^#{base_dir}/, '').split('/')[0...-1]
|
6
|
+
end
|
7
|
+
|
8
|
+
# Deep merge method.
|
9
|
+
#
|
10
|
+
# Inspired from:
|
11
|
+
# http://www.gemtacular.com/gemdocs/cerberus-0.2.2/doc/classes/Hash.html
|
12
|
+
# File lib/cerberus/utils.rb, line 42
|
13
|
+
def self.deep_merge!(first, second)
|
14
|
+
second.each_pair do |k,v|
|
15
|
+
if first[k].is_a?(Hash) and second[k].is_a?(Hash)
|
16
|
+
deep_merge!(first[k], second[k])
|
17
|
+
else
|
18
|
+
first[k] = second[k]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
class YamlPack
|
2
|
+
|
3
|
+
KEY_CONVERTERS = {
|
4
|
+
:symbolize => Proc.new{|key, previous_keys| key.respond_to?(:to_sym) ? key.to_sym : key }
|
5
|
+
}.freeze
|
6
|
+
|
7
|
+
attr_reader :files, :base_dir
|
8
|
+
|
9
|
+
def initialize(files, opts = {})
|
10
|
+
@files = [files].flatten
|
11
|
+
|
12
|
+
@base_dir = opts.fetch(:base_dir, nil)
|
13
|
+
@with_erb = opts.fetch(:erb, true)
|
14
|
+
@key_converter = opts.fetch(:key_converter, nil)
|
15
|
+
@header_file = opts.fetch(:header_file, nil)
|
16
|
+
|
17
|
+
if KEY_CONVERTERS.has_key?(@key_converter)
|
18
|
+
@key_converter = KEY_CONVERTERS[@key_converter]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def load_deep_merged
|
23
|
+
hsh = {}
|
24
|
+
|
25
|
+
files.select{|f| File.exists?(f) }.each do |f|
|
26
|
+
content = self.content(f)
|
27
|
+
begin
|
28
|
+
content = ERB.new(content).result(binding) if @with_erb
|
29
|
+
rescue => e
|
30
|
+
raise "Error in #{f}: #{e.message}"
|
31
|
+
end
|
32
|
+
|
33
|
+
result = YAML::load(content) || {}
|
34
|
+
result = add_subfolders_as_namespaces(f, result) if @base_dir
|
35
|
+
result = convert_keys_recursive(result) if @key_converter
|
36
|
+
|
37
|
+
Util.deep_merge!(hsh, result)
|
38
|
+
end
|
39
|
+
|
40
|
+
hsh
|
41
|
+
end
|
42
|
+
|
43
|
+
# Include the contents of given file into yaml template.
|
44
|
+
# Use this inside your yaml-file.
|
45
|
+
#
|
46
|
+
# <%= include_file('/path/to/file.yml') %>
|
47
|
+
#
|
48
|
+
# DEBT: extract into own module/class
|
49
|
+
#
|
50
|
+
def include_file(file_path)
|
51
|
+
File.read(file_path)
|
52
|
+
end
|
53
|
+
|
54
|
+
def content(file_path)
|
55
|
+
body = File.read(file_path) || ""
|
56
|
+
[header, body].join("\n")
|
57
|
+
end
|
58
|
+
|
59
|
+
def header
|
60
|
+
(@header_file && File.exists?(@header_file)) ? File.read(@header_file) : ""
|
61
|
+
end
|
62
|
+
|
63
|
+
protected
|
64
|
+
|
65
|
+
def convert_keys_recursive(result, previous_keys = [])
|
66
|
+
if result.is_a?(Hash)
|
67
|
+
hsh = {}
|
68
|
+
result.each do |key, value|
|
69
|
+
converted_key = @key_converter.call(key, previous_keys)
|
70
|
+
hsh[converted_key] = convert_keys_recursive(value, [*previous_keys, key])
|
71
|
+
end
|
72
|
+
hsh
|
73
|
+
else # If it's not a hash, return the object itself (end of recursion)
|
74
|
+
result
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def add_subfolders_as_namespaces(file_path, object)
|
79
|
+
Util.subfolders(base_dir, file_path).inject(object) do |object, subfolder|
|
80
|
+
{subfolder => object}
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe YamlPack::Util do
|
4
|
+
it "#subfolders" do
|
5
|
+
YamlPack::Util.subfolders("/foo/", "/foo/baz.yml").should == []
|
6
|
+
YamlPack::Util.subfolders("/foo/", "/foo/bar/baz.yml").should == ['bar']
|
7
|
+
YamlPack::Util.subfolders("/foo/", "/foo/bar/baz/lorem.yml").should == ['bar', 'baz']
|
8
|
+
YamlPack::Util.subfolders("foo/", "foo/bar/baz.yml").should == ['bar']
|
9
|
+
YamlPack::Util.subfolders("/foo", "/foo/baz.yml").should == []
|
10
|
+
YamlPack::Util.subfolders("", "/foo/baz.yml").should == ['foo']
|
11
|
+
YamlPack::Util.subfolders(nil, "/foo/baz.yml").should == ['foo']
|
12
|
+
end
|
13
|
+
end
|
@@ -77,15 +77,4 @@ describe YamlPack do
|
|
77
77
|
result['countries']['se']['name'].should == 'Sweden'
|
78
78
|
end
|
79
79
|
|
80
|
-
|
81
|
-
it "#subfolders" do
|
82
|
-
YamlPack.subfolders("/foo/", "/foo/baz.yml").should == []
|
83
|
-
YamlPack.subfolders("/foo/", "/foo/bar/baz.yml").should == ['bar']
|
84
|
-
YamlPack.subfolders("/foo/", "/foo/bar/baz/lorem.yml").should == ['bar', 'baz']
|
85
|
-
|
86
|
-
YamlPack.subfolders("foo/", "foo/bar/baz.yml").should == ['bar']
|
87
|
-
YamlPack.subfolders("/foo", "/foo/baz.yml").should == []
|
88
|
-
YamlPack.subfolders("", "/foo/baz.yml").should == ['foo']
|
89
|
-
YamlPack.subfolders(nil, "/foo/baz.yml").should == ['foo']
|
90
|
-
end
|
91
80
|
end
|
data/yaml_pack.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaml_pack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3.alpha
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-03-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70213350229160 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70213350229160
|
25
25
|
description: ! "\n YamlPack is a small framework for writing large and complex
|
26
26
|
configuration files in yml.\n It supports nesting and merging merging yaml files.\n
|
27
27
|
\ "
|
@@ -35,8 +35,10 @@ files:
|
|
35
35
|
- Gemfile
|
36
36
|
- README.md
|
37
37
|
- Rakefile
|
38
|
-
- lib/core_ext/hash.rb
|
39
38
|
- lib/yaml_pack.rb
|
39
|
+
- lib/yaml_pack/util.rb
|
40
|
+
- lib/yaml_pack/version.rb
|
41
|
+
- lib/yaml_pack/yaml_pack.rb
|
40
42
|
- spec/.DS_Store
|
41
43
|
- spec/fixtures/.DS_Store
|
42
44
|
- spec/fixtures/yaml_pack/countries/sweden.yml
|
@@ -46,6 +48,7 @@ files:
|
|
46
48
|
- spec/fixtures/yaml_pack/names_german.yml
|
47
49
|
- spec/fixtures/yaml_pack/population.yml
|
48
50
|
- spec/spec_helper.rb
|
51
|
+
- spec/yaml_pack/util_spec.rb
|
49
52
|
- spec/yaml_pack/yaml_pack_spec.rb
|
50
53
|
- yaml_pack.gemspec
|
51
54
|
homepage: ''
|
@@ -81,5 +84,6 @@ test_files:
|
|
81
84
|
- spec/fixtures/yaml_pack/names_german.yml
|
82
85
|
- spec/fixtures/yaml_pack/population.yml
|
83
86
|
- spec/spec_helper.rb
|
87
|
+
- spec/yaml_pack/util_spec.rb
|
84
88
|
- spec/yaml_pack/yaml_pack_spec.rb
|
85
89
|
has_rdoc:
|
data/lib/core_ext/hash.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
# Extend Hash with recursive merging abilities
|
2
|
-
class Hash
|
3
|
-
# Deep merge method. Silly name prevents overwriting other implementations.
|
4
|
-
#
|
5
|
-
# Inspired from:
|
6
|
-
# http://www.gemtacular.com/gemdocs/cerberus-0.2.2/doc/classes/Hash.html
|
7
|
-
# File lib/cerberus/utils.rb, line 42
|
8
|
-
def yaml_pack_deep_merge!(second)
|
9
|
-
second.each_pair do |k,v|
|
10
|
-
if self[k].is_a?(Hash) and second[k].is_a?(Hash)
|
11
|
-
self[k].yaml_pack_deep_merge!(second[k])
|
12
|
-
else
|
13
|
-
self[k] = second[k]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|