extended_yaml 0.1.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -6
- data/lib/extended_yaml/version.rb +1 -1
- data/lib/extended_yaml.rb +41 -9
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d4ee11f47e33513fd8398d47f605460d2730beea6420340a906d6bf802f35996
|
4
|
+
data.tar.gz: f9e6c2fd5ff1150a9694e871f79d2471680f18d2240eb9df21caf81e0687495f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1058232576e3c99969dede0083812f3dc566ccf5c9a6d7e293ccc99bc06f4a026cba2d531c79efec16b05e6bee13b85c0b261c10a409968b3d1e893b7d26978d
|
7
|
+
data.tar.gz: b621f31a3c9a4895a2aa1c6cbd823e803a364dffa0113f6329c35b7590f14f373cbad893663fcb37b23c4560c83f18641ec23f65560c31de06d042e1ce95ae38
|
data/README.md
CHANGED
@@ -2,6 +2,8 @@ Extended YAML
|
|
2
2
|
==================================================
|
3
3
|
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/extended_yaml.svg)](https://badge.fury.io/rb/extended_yaml)
|
5
|
+
[![Build Status](https://github.com/DannyBen/extended_yaml/workflows/Test/badge.svg)](https://github.com/DannyBen/extended_yaml/actions?query=workflow%3ATest)
|
6
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/0d162ff84c50abe7c83a/maintainability)](https://codeclimate.com/github/DannyBen/extended_yaml/maintainability)
|
5
7
|
|
6
8
|
---
|
7
9
|
|
@@ -44,14 +46,14 @@ settings:
|
|
44
46
|
host: example.com
|
45
47
|
```
|
46
48
|
|
47
|
-
We can now load the extended YAML file
|
49
|
+
We can now load the extended YAML file like this:
|
48
50
|
|
49
51
|
```ruby
|
50
52
|
# Load an extended YAML
|
51
53
|
require 'extended_yaml'
|
52
54
|
|
53
55
|
p ExtendedYAML.load 'examples/simple.yml'
|
54
|
-
#=> {"settings"=>{"host"=>"
|
56
|
+
#=> {"settings"=>{"host"=>"localhost", "port"=>80}}
|
55
57
|
```
|
56
58
|
|
57
59
|
Notes
|
@@ -64,14 +66,16 @@ Notes
|
|
64
66
|
4. ERB tags will be evaluated in all YAML files.
|
65
67
|
5. The `extends` option can use either a single file string, or an array.
|
66
68
|
Extensions are optional.
|
67
|
-
6.
|
68
|
-
|
69
|
+
6. Using `*` anywhere in the `extends` path will load multiple files with one
|
70
|
+
call.
|
71
|
+
7. If you need to use a key that is named differently than `extends`, provide
|
72
|
+
it using the `key` keyword argument:
|
69
73
|
```ruby
|
70
|
-
ExtendedYAML.load 'examples/simple.yml, key: 'include'
|
74
|
+
ExtendedYAML.load 'examples/simple.yml', key: 'include'
|
71
75
|
```
|
72
76
|
|
73
77
|
See the [examples/master.yml](examples/master.yml) file for additional
|
74
78
|
information.
|
75
79
|
|
76
80
|
|
77
|
-
[1]: https://github.com/magynhard/yaml_extend
|
81
|
+
[1]: https://github.com/magynhard/yaml_extend
|
data/lib/extended_yaml.rb
CHANGED
@@ -17,10 +17,17 @@ class ExtendedYAML
|
|
17
17
|
@file, @key = file, key
|
18
18
|
end
|
19
19
|
|
20
|
-
# @return [Hash, Array] the parsed YAML
|
20
|
+
# @return [Hash, Array, nil] the parsed YAML
|
21
21
|
def result
|
22
|
-
|
23
|
-
|
22
|
+
# ref: https://bugs.ruby-lang.org/issues/17866
|
23
|
+
begin
|
24
|
+
data = ::YAML.load evaluate, aliases: true
|
25
|
+
rescue ArgumentError
|
26
|
+
# :nocov:
|
27
|
+
data = ::YAML.load evaluate
|
28
|
+
# :nocov:
|
29
|
+
end
|
30
|
+
data ? resolve_extends(data) : nil
|
24
31
|
end
|
25
32
|
|
26
33
|
# @return [String] the YAML string, with evaluated and ERB
|
@@ -38,20 +45,45 @@ private
|
|
38
45
|
@base_dir ||= File.dirname file
|
39
46
|
end
|
40
47
|
|
41
|
-
# @param [Hash] data structure, possibly with 'extends' array
|
48
|
+
# @param [Hash] data the data structure, possibly with 'extends' array
|
42
49
|
# @return [Hash] the merged data
|
43
50
|
def resolve_extends(data)
|
44
51
|
extra_files = data.delete key
|
45
52
|
return data unless extra_files
|
46
53
|
|
47
|
-
extra_files =
|
54
|
+
extra_files = expand_file_list extra_files
|
48
55
|
|
49
|
-
extra_files.each do |
|
50
|
-
|
51
|
-
path = File.expand_path extra_file, base_dir
|
52
|
-
data.deep_merge! self.class.new(path).result
|
56
|
+
extra_files.each do |path|
|
57
|
+
data = self.class.new(path, key: key).result.deep_merge data
|
53
58
|
end
|
54
59
|
|
55
60
|
data
|
56
61
|
end
|
62
|
+
|
63
|
+
# Receives a string or an array of strings, each representing an acceptable
|
64
|
+
# path definition. Each definition may be with or without a file etxension,
|
65
|
+
# and may be a glob pattern. The resulting array will be a normalized list
|
66
|
+
# of full paths.
|
67
|
+
#
|
68
|
+
# @param [Array, String] files one or more path definitions
|
69
|
+
# @return [Array] a normalized list of absolute paths
|
70
|
+
def expand_file_list(files)
|
71
|
+
list = []
|
72
|
+
files = [files] unless files.is_a? Array
|
73
|
+
|
74
|
+
files.each do |path|
|
75
|
+
list += expand_path path
|
76
|
+
end
|
77
|
+
|
78
|
+
list
|
79
|
+
end
|
80
|
+
|
81
|
+
# @param [String] path the path to the YAML file, with or without extension.
|
82
|
+
# May include a glob pattern wildcard.
|
83
|
+
# @return [Array] one or more absolute paths.
|
84
|
+
def expand_path(path)
|
85
|
+
path += extension unless path.end_with? extension
|
86
|
+
path = File.expand_path path, base_dir
|
87
|
+
path.include?('*') ? Dir[path].sort : [path]
|
88
|
+
end
|
57
89
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: extended_yaml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Load YAML files that deep merge other YAML files
|
14
14
|
email: db@dannyben.com
|
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
requirements: []
|
42
|
-
rubygems_version: 3.
|
42
|
+
rubygems_version: 3.2.15
|
43
43
|
signing_key:
|
44
44
|
specification_version: 4
|
45
45
|
summary: Load YAML files that merge other YAML files
|