yaml_extend 0.2.5 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -7
- data/lib/yaml_extend.rb +25 -18
- data/lib/yaml_extend/version.rb +1 -1
- metadata +3 -4
- data/lib/yaml_extend/yaml_extend_helper.rb +0 -68
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8de4a06253714132d952fcaebc2592ef5410fe38
|
4
|
+
data.tar.gz: 6fb4e757d42962afa2d85e4fe754a83e0584e204
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db63a4f3749846aa07412dbc8f95738f9cd43c6aa19ef45dee3b6cd098da66afd16739f3a6e74e0d5cc6188030ec57bd78b874583a8a5ea158350757322a7b8a
|
7
|
+
data.tar.gz: 9c1a44b1075bab488ce5f02b40ba20cc9afac1d5da487f6dcddf5f7f48acabeb1d980865d5f36c2c1b80658feaffb281096f523a9e74811c234e981081aa2092
|
data/README.md
CHANGED
@@ -48,9 +48,9 @@ That key can be customized if you prefer another one.
|
|
48
48
|
See the examples below.
|
49
49
|
|
50
50
|
## Usage
|
51
|
-
yaml_extend adds the method YAML
|
51
|
+
yaml_extend adds the method YAML.ext_load_file to YAML.
|
52
52
|
|
53
|
-
This method works like the original YAML
|
53
|
+
This method works like the original YAML.load_file, by extending it with file inheritance.
|
54
54
|
|
55
55
|
### Examples
|
56
56
|
|
@@ -77,7 +77,7 @@ data:
|
|
77
77
|
- 'Apples'
|
78
78
|
```
|
79
79
|
|
80
|
-
When you then call
|
80
|
+
When you then call `ext_load_file`
|
81
81
|
|
82
82
|
```ruby
|
83
83
|
config = YAML.ext_load_file 'start.yml'
|
@@ -120,7 +120,7 @@ foo: 'bar'
|
|
120
120
|
...
|
121
121
|
```
|
122
122
|
##### 1. Specify by parameter
|
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
|
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.
|
124
124
|
```ruby
|
125
125
|
config = YAML.ext_load_file 'custom1.yml', 'inherit_from'
|
126
126
|
```
|
@@ -131,7 +131,7 @@ YAML.ext_load_key = 'inherit_from'
|
|
131
131
|
config = YAML.ext_load_file 'custom1.yml'
|
132
132
|
```
|
133
133
|
##### Reset the global key
|
134
|
-
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.
|
135
135
|
```ruby
|
136
136
|
YAML.reset_load_key # more readable
|
137
137
|
YAML.ext_load_key = nil # more explicit
|
@@ -151,12 +151,11 @@ config = YAML.ext_load_file 'custom2.yml', ['options','extend_file']
|
|
151
151
|
```
|
152
152
|
|
153
153
|
## Documentation
|
154
|
-
YAML
|
154
|
+
YAML.ext_load_file(yaml_path, inheritance_key='extends', extend_existing_arrays=true)
|
155
155
|
|
156
156
|
- *yaml_path* relative or absolute path to yaml file to inherit from
|
157
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'
|
158
158
|
- *extend_existing_arrays* Extends existing arrays in yaml structure instead of replacing them. Default: true
|
159
|
-
- *config* only intended to be used by the method itself due recursive algorithm
|
160
159
|
|
161
160
|
## Contributing
|
162
161
|
|
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,59 @@ 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
69
|
super_config_path = File.dirname(yaml_path) + '/' + super_inheritance_file
|
62
|
-
total_config =
|
70
|
+
total_config = YAML.ext_load_file_recursive(super_config_path, inheritance_key, extend_existing_arrays, total_config)
|
63
71
|
end
|
64
|
-
YamlExtendHelper.decode_booleans total_config
|
65
|
-
else
|
66
|
-
delete_yaml_key inheritance_key, merged_config
|
67
|
-
YamlExtendHelper.decode_booleans merged_config
|
68
72
|
end
|
73
|
+
total_config.deeper_merge!(super_config, extend_existing_arrays: extend_existing_arrays)
|
69
74
|
end
|
70
75
|
|
71
|
-
private
|
72
|
-
|
73
76
|
# some logic to ensure absolute file inheritance as well as
|
74
77
|
# relative file inheritance in yaml files
|
75
78
|
def self.make_absolute_path(file_path)
|
76
79
|
private_class_method
|
77
80
|
return file_path if YAML.absolute_path?(file_path) && File.exist?(file_path)
|
78
|
-
|
81
|
+
# caller_locations returns the current execution stack
|
82
|
+
# [0] is the call from ext_load_file_recursive,
|
83
|
+
# [1] is inside ext_load_file,
|
84
|
+
# [2] is the exteranl caller of YAML.ext_load_file
|
85
|
+
base_path = File.dirname(caller_locations[2].path)
|
79
86
|
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
|
87
|
+
return Dir.pwd + '/' + file_path if File.exist? Dir.pwd + '/' + file_path # relative path from project
|
81
88
|
error_message = "Can not find absolute path of '#{file_path}'"
|
82
89
|
unless YAML.absolute_path? file_path
|
83
90
|
error_message += "\nAlso tried:\n- #{base_path + '/' + file_path}\n"\
|
data/lib/yaml_extend/version.rb
CHANGED
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.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: 2019-
|
11
|
+
date: 2019-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: deep_merge
|
@@ -86,7 +86,6 @@ 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
90
|
homepage: https://github.com/magynhard/yaml_extend
|
92
91
|
licenses:
|
@@ -109,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
108
|
version: '0'
|
110
109
|
requirements: []
|
111
110
|
rubyforge_project:
|
112
|
-
rubygems_version: 2.
|
111
|
+
rubygems_version: 2.5.2
|
113
112
|
signing_key:
|
114
113
|
specification_version: 4
|
115
114
|
summary: Extends YAML to support file based inheritance
|
@@ -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
|