yaml_extend 0.2.2 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +1 -1
- data/README.md +24 -17
- data/lib/yaml_extend.rb +32 -19
- data/lib/yaml_extend/version.rb +1 -1
- data/yaml_extend.gemspec +6 -6
- metadata +15 -17
- data/lib/yaml_extend/yaml_extend_helper.rb +0 -68
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 709ab41867197b612c61535154fe7e65bad1354c7605417d0fcad1601d45c134
|
4
|
+
data.tar.gz: 72f4e212f76e450b96420bb61eeff03aa2e7dc685c6600312ebc767b34987c80
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aebb3c47f6b90557ac6683ed673a036a3b05a469b543c7bf4b3eb07108ff721410a6a5e57b5fa3a643360fe27cf59f3eb99b724a98f8bd483c34159d1d40956e
|
7
|
+
data.tar.gz: e15f2449c5c4d93abb0ba1a56cde3928fd06a7219d2cedb6ee20290cabc8fe1c2a27ecd976a04558395b26e26ab42c60f080595ca458c337996861313b532494
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
|
|
55
55
|
## Enforcement
|
56
56
|
|
57
57
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
-
reported by contacting the project team at
|
58
|
+
reported by contacting the project team at yaml_extend.gemspec@mail.magynhard.de. All
|
59
59
|
complaints will be reviewed and investigated and will result in a response that
|
60
60
|
is deemed necessary and appropriate to the circumstances. The project team is
|
61
61
|
obligated to maintain confidentiality with regard to the reporter of an incident.
|
data/README.md
CHANGED
@@ -22,11 +22,20 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Common information
|
24
24
|
|
25
|
-
It is possible to build inheritance trees like
|
25
|
+
It is possible to build inheritance trees like:
|
26
26
|
```
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
defaults.yml
|
28
|
+
________\_________
|
29
|
+
\ \ \
|
30
|
+
dev.yml int.yml prod.yml
|
31
|
+
```
|
32
|
+
or like this:
|
33
|
+
```
|
34
|
+
default.yml english.yml default.yml german.yml
|
35
|
+
\ / \ /
|
36
|
+
uk.yml de.yml
|
37
|
+
|
|
38
|
+
at.yml
|
30
39
|
```
|
31
40
|
|
32
41
|
A file can inherit from as many as you want. Trees can be nested as deep as you want.
|
@@ -39,9 +48,9 @@ That key can be customized if you prefer another one.
|
|
39
48
|
See the examples below.
|
40
49
|
|
41
50
|
## Usage
|
42
|
-
yaml_extend adds the method YAML
|
51
|
+
yaml_extend adds the method YAML.ext_load_file to YAML.
|
43
52
|
|
44
|
-
This method works like the original YAML
|
53
|
+
This method works like the original YAML.load_file, by extending it with file inheritance.
|
45
54
|
|
46
55
|
### Examples
|
47
56
|
|
@@ -68,10 +77,10 @@ data:
|
|
68
77
|
- 'Apples'
|
69
78
|
```
|
70
79
|
|
71
|
-
When you then call
|
80
|
+
When you then call `ext_load_file`
|
72
81
|
|
73
82
|
```ruby
|
74
|
-
YAML.ext_load_file 'start.yml'
|
83
|
+
config = YAML.ext_load_file 'start.yml'
|
75
84
|
```
|
76
85
|
|
77
86
|
the returned YAML value results in
|
@@ -111,19 +120,18 @@ foo: 'bar'
|
|
111
120
|
...
|
112
121
|
```
|
113
122
|
##### 1. Specify by parameter
|
114
|
-
You can specify the key by parameter, this is the way to go if you want to use the different key only once or you use the
|
123
|
+
You can specify the key by parameter, this is the way to go if you want to use the different key only once or you use the `ext_load_file` method only once in your application.
|
115
124
|
```ruby
|
116
|
-
YAML.ext_load_file 'custom1.yml', 'inherit_from'
|
125
|
+
config = YAML.ext_load_file 'custom1.yml', 'inherit_from'
|
117
126
|
```
|
118
127
|
##### 2. Global configuration of the key
|
119
128
|
You can specify the key by configuration globally. So you only need to set the key once and not as parameter anymore
|
120
129
|
```ruby
|
121
130
|
YAML.ext_load_key = 'inherit_from'
|
122
|
-
YAML.ext_load_file 'custom1.yml'
|
123
|
-
YAML.ext_load_file 'custom2.yml'
|
131
|
+
config = YAML.ext_load_file 'custom1.yml'
|
124
132
|
```
|
125
133
|
##### Reset the global key
|
126
|
-
To reset the global inheritance key, you can either set it to nil or call the
|
134
|
+
To reset the global inheritance key, you can either set it to nil or call the `reset_load_key` method.
|
127
135
|
```ruby
|
128
136
|
YAML.reset_load_key # more readable
|
129
137
|
YAML.ext_load_key = nil # more explicit
|
@@ -139,18 +147,17 @@ foo: 'bar'
|
|
139
147
|
```
|
140
148
|
|
141
149
|
```ruby
|
142
|
-
YAML.ext_load_file 'custom2.yml', ['options','extend_file']
|
150
|
+
config = YAML.ext_load_file 'custom2.yml', ['options','extend_file']
|
143
151
|
```
|
144
152
|
|
145
153
|
## Documentation
|
146
|
-
YAML
|
154
|
+
YAML.ext_load_file(yaml_path, inheritance_key='extends', extend_existing_arrays=true)
|
147
155
|
|
148
156
|
- *yaml_path* relative or absolute path to yaml file to inherit from
|
149
157
|
- *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'
|
150
158
|
- *extend_existing_arrays* Extends existing arrays in yaml structure instead of replacing them. Default: true
|
151
|
-
- *config* only intended to be used by the method itself due recursive algorithm
|
152
159
|
|
153
160
|
## Contributing
|
154
161
|
|
155
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
162
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/magynhard/yaml_extend. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
156
163
|
|
data/lib/yaml_extend.rb
CHANGED
@@ -3,8 +3,6 @@ require 'yaml_extend/version'
|
|
3
3
|
require 'yaml'
|
4
4
|
require 'deep_merge/rails_compat'
|
5
5
|
|
6
|
-
require_relative 'yaml_extend/yaml_extend_helper'
|
7
|
-
|
8
6
|
require_relative 'custom_errors/invalid_key_type_error'
|
9
7
|
|
10
8
|
#
|
@@ -34,50 +32,65 @@ module YAML
|
|
34
32
|
def self.reset_load_key()
|
35
33
|
@@ext_load_key = nil
|
36
34
|
end
|
35
|
+
|
37
36
|
#
|
38
|
-
# Extended variant of the
|
37
|
+
# Extended variant of the YAML.load_file method by providing the
|
39
38
|
# ability to inherit from other YAML file(s)
|
40
39
|
#
|
41
40
|
# @param yaml_path [String] the path to the yaml file to be loaded
|
42
41
|
# @param inheritance_key [String|Array] The key used in the yaml file to extend from another YAML file. Use an Array if you want to use a tree structure key like "options.extends" => ['options','extends']
|
43
42
|
# @param extend_existing_arrays [Boolean] extend existing arrays instead of replacing them
|
44
|
-
# @param config [Hash] a hash to be merged into the result, usually only recursivly called by the method itself
|
45
|
-
#
|
46
43
|
# @return [Hash] the resulting yaml config
|
47
44
|
#
|
48
|
-
def self.ext_load_file(yaml_path, inheritance_key=nil, extend_existing_arrays=true
|
45
|
+
def self.ext_load_file(yaml_path, inheritance_key=nil, extend_existing_arrays=true)
|
46
|
+
YAML.ext_load_file_recursive(yaml_path, inheritance_key, extend_existing_arrays, {})
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
#
|
52
|
+
# @param config [Hash] a hash to be merged into the result, usually only recursivly called by the method itself
|
53
|
+
#
|
54
|
+
def self.ext_load_file_recursive(yaml_path, inheritance_key, extend_existing_arrays, config)
|
55
|
+
private_class_method
|
49
56
|
if inheritance_key.nil?
|
50
57
|
inheritance_key = @@ext_load_key || DEFAULT_INHERITANCE_KEY
|
51
58
|
end
|
52
|
-
total_config
|
59
|
+
total_config = config.clone
|
60
|
+
|
53
61
|
yaml_path = YAML.make_absolute_path yaml_path
|
54
|
-
super_config =
|
62
|
+
super_config = YAML.load_file(File.open(yaml_path))
|
55
63
|
super_inheritance_files = yaml_value_by_key inheritance_key, super_config
|
56
64
|
delete_yaml_key inheritance_key, super_config # we don't merge the super inheritance keys into the base yaml
|
57
|
-
|
65
|
+
|
58
66
|
if super_inheritance_files && super_inheritance_files != ''
|
59
67
|
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
|
60
68
|
super_inheritance_files.each_with_index do |super_inheritance_file, index|
|
61
|
-
|
62
|
-
|
69
|
+
# Extend a YAML path in an absolute directory
|
70
|
+
if YAML.absolute_path?(super_inheritance_file)
|
71
|
+
super_config_path = YAML.make_absolute_path(super_inheritance_file)
|
72
|
+
# Extend a YAML path in a relative directory
|
73
|
+
else
|
74
|
+
super_config_path = File.dirname(yaml_path) + '/' + super_inheritance_file
|
75
|
+
end
|
76
|
+
total_config = YAML.ext_load_file_recursive(super_config_path, inheritance_key, extend_existing_arrays, total_config)
|
63
77
|
end
|
64
|
-
YamlExtendHelper.decode_booleans total_config
|
65
|
-
else
|
66
|
-
delete_yaml_key inheritance_key, merged_config
|
67
|
-
YamlExtendHelper.decode_booleans merged_config
|
68
78
|
end
|
79
|
+
total_config.deeper_merge!(super_config, extend_existing_arrays: extend_existing_arrays)
|
69
80
|
end
|
70
81
|
|
71
|
-
private
|
72
|
-
|
73
82
|
# some logic to ensure absolute file inheritance as well as
|
74
83
|
# relative file inheritance in yaml files
|
75
84
|
def self.make_absolute_path(file_path)
|
76
85
|
private_class_method
|
77
86
|
return file_path if YAML.absolute_path?(file_path) && File.exist?(file_path)
|
78
|
-
|
87
|
+
# caller_locations returns the current execution stack
|
88
|
+
# [0] is the call from ext_load_file_recursive,
|
89
|
+
# [1] is inside ext_load_file,
|
90
|
+
# [2] is the exteranl caller of YAML.ext_load_file
|
91
|
+
base_path = File.dirname(caller_locations[2].path)
|
79
92
|
return base_path + '/' + file_path if File.exist? base_path + '/' + file_path # relative path from yaml file
|
80
|
-
return Dir.pwd + '/' + file_path if File.exist? Dir.pwd + '/' + file_path
|
93
|
+
return Dir.pwd + '/' + file_path if File.exist? Dir.pwd + '/' + file_path # relative path from project
|
81
94
|
error_message = "Can not find absolute path of '#{file_path}'"
|
82
95
|
unless YAML.absolute_path? file_path
|
83
96
|
error_message += "\nAlso tried:\n- #{base_path + '/' + file_path}\n"\
|
data/lib/yaml_extend/version.rb
CHANGED
data/yaml_extend.gemspec
CHANGED
@@ -6,11 +6,11 @@ require 'yaml_extend/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "yaml_extend"
|
8
8
|
spec.version = YamlExtend::VERSION
|
9
|
-
spec.authors = ["
|
10
|
-
spec.email = ["
|
9
|
+
spec.authors = ["Matthäus Beyrle"]
|
10
|
+
spec.email = ["yaml_extend.gemspec@mail.magynhard.de"]
|
11
11
|
|
12
12
|
spec.summary = %q{Extends YAML to support file based inheritance}
|
13
|
-
spec.homepage = "https://github.com/
|
13
|
+
spec.homepage = "https://github.com/magynhard/yaml_extend"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
|
|
31
31
|
|
32
32
|
spec.add_dependency 'deep_merge', '~> 1.1'
|
33
33
|
|
34
|
-
spec.add_development_dependency 'bundler', '
|
35
|
-
spec.add_development_dependency 'rake', '
|
36
|
-
spec.add_development_dependency 'rspec', '
|
34
|
+
spec.add_development_dependency 'bundler', '>= 1.14'
|
35
|
+
spec.add_development_dependency 'rake', '>= 10.0'
|
36
|
+
spec.add_development_dependency 'rspec', '>= 3.0'
|
37
37
|
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.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthäus Beyrle
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: deep_merge
|
@@ -28,47 +28,47 @@ dependencies:
|
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.14'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.14'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '10.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '10.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '3.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '3.0'
|
69
|
-
description:
|
69
|
+
description:
|
70
70
|
email:
|
71
|
-
-
|
71
|
+
- yaml_extend.gemspec@mail.magynhard.de
|
72
72
|
executables: []
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
@@ -86,14 +86,13 @@ files:
|
|
86
86
|
- lib/custom_errors/invalid_key_type_error.rb
|
87
87
|
- lib/yaml_extend.rb
|
88
88
|
- lib/yaml_extend/version.rb
|
89
|
-
- lib/yaml_extend/yaml_extend_helper.rb
|
90
89
|
- yaml_extend.gemspec
|
91
|
-
homepage: https://github.com/
|
90
|
+
homepage: https://github.com/magynhard/yaml_extend
|
92
91
|
licenses:
|
93
92
|
- MIT
|
94
93
|
metadata:
|
95
94
|
allowed_push_host: https://rubygems.org
|
96
|
-
post_install_message:
|
95
|
+
post_install_message:
|
97
96
|
rdoc_options: []
|
98
97
|
require_paths:
|
99
98
|
- lib
|
@@ -108,9 +107,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
107
|
- !ruby/object:Gem::Version
|
109
108
|
version: '0'
|
110
109
|
requirements: []
|
111
|
-
|
112
|
-
|
113
|
-
signing_key:
|
110
|
+
rubygems_version: 3.1.3
|
111
|
+
signing_key:
|
114
112
|
specification_version: 4
|
115
113
|
summary: Extends YAML to support file based inheritance
|
116
114
|
test_files: []
|
@@ -1,68 +0,0 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
# This class includes a workaround patch, providing a solution of the unaccepted pull request
|
4
|
-
# 'false is not overriden by true if preserve_unmergeables'
|
5
|
-
# https://github.com/danielsdeleo/deep_merge/pull/28
|
6
|
-
#
|
7
|
-
# It ensures, that booleans can be merged correctly, by en- and decoding them to strings, before and after merging
|
8
|
-
# see #encode_boolens and #decode_booleans
|
9
|
-
#
|
10
|
-
|
11
|
-
class YamlExtendHelper
|
12
|
-
|
13
|
-
TRUE_CLASS_ENCODED = '#={TrueClass}=#'
|
14
|
-
FALSE_CLASS_ENCODED = '#={FalseClass}=#'
|
15
|
-
|
16
|
-
def self.encode_booleans(hash)
|
17
|
-
hash.each_with_object({}) do |(k,v),g|
|
18
|
-
g[k] = if v.is_a? Hash
|
19
|
-
YamlExtendHelper.encode_booleans(v)
|
20
|
-
elsif v.is_a? Array
|
21
|
-
v.each_with_index do |av, ai|
|
22
|
-
v[ai] = if av.is_a? Hash
|
23
|
-
YamlExtendHelper.encode_booleans(av)
|
24
|
-
elsif av.is_a? TrueClass
|
25
|
-
TRUE_CLASS_ENCODED
|
26
|
-
elsif av.is_a? FalseClass
|
27
|
-
FALSE_CLASS_ENCODED
|
28
|
-
else
|
29
|
-
av
|
30
|
-
end
|
31
|
-
end
|
32
|
-
elsif v.is_a? TrueClass
|
33
|
-
TRUE_CLASS_ENCODED
|
34
|
-
elsif v.is_a? FalseClass
|
35
|
-
FALSE_CLASS_ENCODED
|
36
|
-
else
|
37
|
-
v
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.decode_booleans(hash)
|
43
|
-
hash.each_with_object({}) do |(k,v),g|
|
44
|
-
g[k] = if v.is_a? Hash
|
45
|
-
YamlExtendHelper.decode_booleans(v)
|
46
|
-
elsif v.is_a? Array
|
47
|
-
v.each_with_index do |av, ai|
|
48
|
-
v[ai] = if av.is_a? Hash
|
49
|
-
YamlExtendHelper.decode_booleans(av)
|
50
|
-
elsif av === TRUE_CLASS_ENCODED
|
51
|
-
true
|
52
|
-
elsif av === FALSE_CLASS_ENCODED
|
53
|
-
false
|
54
|
-
else
|
55
|
-
av
|
56
|
-
end
|
57
|
-
end
|
58
|
-
elsif v === TRUE_CLASS_ENCODED
|
59
|
-
true
|
60
|
-
elsif v === FALSE_CLASS_ENCODED
|
61
|
-
false
|
62
|
-
else
|
63
|
-
v
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|