yaml_extend 0.0.12 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/README.md +27 -1
- data/lib/custom_errors/invalid_key_type_error.rb +2 -0
- data/lib/yaml_extend/version.rb +1 -1
- data/lib/yaml_extend.rb +45 -5
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 04f4c2478bcf2e8aba3680677eceab0842a7d9fc2e7365aa2f28d0d6dd63a117
|
4
|
+
data.tar.gz: 43e083063da5ef8ed5908e0f9468424b92ed3da4dd3c263a8771f264a4e5d81d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d34ec7a111411afa7422674d3de2a5b5703016ffae3e5ad20c98844dc1fcd9a80c18465e10cbb4421d2eae0a76f861cc21762ad063c556e22b048a096a8bb546
|
7
|
+
data.tar.gz: 2a5a773487b707570e3a615278594998003fbab324c86b486c3cef0f5bcd8748b62926271b121be8a3389ffdc1a4b830e5f5fbe53467de1d8f49b2f23668dfd0
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -82,11 +82,37 @@ extends:
|
|
82
82
|
...
|
83
83
|
```
|
84
84
|
|
85
|
+
#### Using custom extend key
|
86
|
+
```
|
87
|
+
#custom1.yml
|
88
|
+
inherit_from:
|
89
|
+
- 'super_file.yml'
|
90
|
+
foo: 'bar'
|
91
|
+
...
|
92
|
+
```
|
93
|
+
|
94
|
+
```
|
95
|
+
YAML.ext_load_file('custom1.yml','inherit_from')
|
96
|
+
```
|
97
|
+
#### Using custom nested extend key
|
98
|
+
```
|
99
|
+
#custom2.yml
|
100
|
+
options:
|
101
|
+
extend_file: 'super_file.yml'
|
102
|
+
database: false
|
103
|
+
foo: 'bar'
|
104
|
+
...
|
105
|
+
```
|
106
|
+
|
107
|
+
```
|
108
|
+
YAML.ext_load_file('custom2.yml',['options','extend_file'])
|
109
|
+
```
|
110
|
+
|
85
111
|
## Documentation
|
86
112
|
YAML#ext_load_file(yaml_path, inheritance_key='extends', extend_existing_arrays=true, config = {})
|
87
113
|
|
88
114
|
- *yaml_path* relative or absolute path to yaml file to inherit from
|
89
|
-
- *inheritance_key* you can overwrite the default key, if you use
|
115
|
+
- *inheritance_key* you can overwrite the default key, if you use the default 'extends' already as part of your configuration. The inheritance_key is NOT included, that means it will be deleted, in the final merged file. Default: 'extends'
|
90
116
|
- *extend_existing_arrays* Extends existing arrays in yaml structure instead of replacing them. Default: true
|
91
117
|
- *config* only intended to be used by the method itself due recursive algorithm
|
92
118
|
|
data/lib/yaml_extend/version.rb
CHANGED
data/lib/yaml_extend.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
require
|
1
|
+
require 'yaml_extend/version'
|
2
2
|
|
3
3
|
require 'yaml'
|
4
4
|
require 'deep_merge/rails_compat'
|
5
5
|
|
6
|
+
require_relative 'custom_errors/invalid_key_type_error'
|
7
|
+
|
6
8
|
#
|
7
9
|
# Extending the YAML library to allow to inherit from another YAML file(s)
|
8
10
|
#
|
@@ -13,7 +15,9 @@ module YAML
|
|
13
15
|
# ability to inherit from other YAML file(s)
|
14
16
|
#
|
15
17
|
# @param yaml_path [String] the path to the yaml file to be loaded
|
16
|
-
# @param inheritance_key [String]
|
18
|
+
# @param inheritance_key [String|Array]
|
19
|
+
# The key used in the yaml file to extend from another YAML file.
|
20
|
+
# Use an Array if you want to use a tree structure key like "options.extends" => ['options','extends']
|
17
21
|
# @param extend_existing_arrays [Boolean] extend existing arrays instead of replacing them
|
18
22
|
# @param config [Hash] a hash to be merged into the result, usually only recursivly called by the method itself
|
19
23
|
#
|
@@ -23,8 +27,8 @@ module YAML
|
|
23
27
|
total_config ||= {}
|
24
28
|
yaml_path = YAML.make_absolute_path yaml_path
|
25
29
|
super_config = YAML.load_file(File.open(yaml_path))
|
26
|
-
super_inheritance_files = super_config
|
27
|
-
|
30
|
+
super_inheritance_files = yaml_value_by_key inheritance_key, super_config
|
31
|
+
delete_yaml_key inheritance_key, super_config # we don't merge the super inheritance keys into the base yaml
|
28
32
|
merged_config = config.clone.deeper_merge(super_config, extend_existing_arrays: extend_existing_arrays)
|
29
33
|
if super_inheritance_files && super_inheritance_files != ''
|
30
34
|
super_inheritance_files = [super_inheritance_files] unless super_inheritance_files.is_a? Array # we support strings as well as arrays of type string to extend from
|
@@ -34,7 +38,7 @@ module YAML
|
|
34
38
|
end
|
35
39
|
total_config
|
36
40
|
else
|
37
|
-
merged_config
|
41
|
+
delete_yaml_key inheritance_key, merged_config
|
38
42
|
merged_config
|
39
43
|
end
|
40
44
|
end
|
@@ -62,4 +66,40 @@ module YAML
|
|
62
66
|
path.start_with?('/') || # unix like
|
63
67
|
(path.length >= 3 && path[1] == ':') # ms windows
|
64
68
|
end
|
69
|
+
|
70
|
+
# Return the value of the corresponding key
|
71
|
+
# @param key [String|Array]
|
72
|
+
def self.yaml_value_by_key(key, config)
|
73
|
+
return config[key] if key.is_a? String
|
74
|
+
if valid_key_type? key
|
75
|
+
cfg_copy = config.clone
|
76
|
+
key.each do |key|
|
77
|
+
if cfg_copy.nil?
|
78
|
+
return
|
79
|
+
elsif valid_key_type? key
|
80
|
+
cfg_copy = cfg_copy[key]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
cfg_copy
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.valid_key_type?(key)
|
88
|
+
key.is_a?(Array) || key.is_a?(String) ||
|
89
|
+
raise(InvalidKeyTypeError,"Invalid key of type '#{key.class.name}'. Valid types are String and Array.")
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.delete_yaml_key(key, config)
|
93
|
+
return config.delete(key) if key.is_a? String
|
94
|
+
cfg_ref = config
|
95
|
+
last_ref = nil
|
96
|
+
key.each do |key|
|
97
|
+
if valid_key_type?(key) && !cfg_ref[key].nil?
|
98
|
+
last_ref = cfg_ref
|
99
|
+
cfg_ref = cfg_ref[key] unless cfg_ref.nil?
|
100
|
+
end
|
101
|
+
end
|
102
|
+
last_ref.delete key.last unless last_ref.nil?
|
103
|
+
end
|
104
|
+
|
65
105
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaml_extend
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthäus Beyrle
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: deep_merge
|
@@ -83,6 +83,7 @@ files:
|
|
83
83
|
- Rakefile
|
84
84
|
- bin/console
|
85
85
|
- bin/setup
|
86
|
+
- lib/custom_errors/invalid_key_type_error.rb
|
86
87
|
- lib/yaml_extend.rb
|
87
88
|
- lib/yaml_extend/version.rb
|
88
89
|
- yaml_extend.gemspec
|
@@ -107,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
108
|
version: '0'
|
108
109
|
requirements: []
|
109
110
|
rubyforge_project:
|
110
|
-
rubygems_version: 2.
|
111
|
+
rubygems_version: 2.7.3
|
111
112
|
signing_key:
|
112
113
|
specification_version: 4
|
113
114
|
summary: Extends YAML to support file based inheritance
|