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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 75375be6f35d137f2c0cc92fcd0a69b8c706ab0f4152566d0b6dc037d0b66204
4
- data.tar.gz: 6ea26f239f8a148d5e07c0e20f6e30e734834ffe37953be1e79041b47fd21257
3
+ metadata.gz: d4ee11f47e33513fd8398d47f605460d2730beea6420340a906d6bf802f35996
4
+ data.tar.gz: f9e6c2fd5ff1150a9694e871f79d2471680f18d2240eb9df21caf81e0687495f
5
5
  SHA512:
6
- metadata.gz: 8df1afcc6db22d7a5ce02f67c71839d2e62648af49f0df3532fcec88965506e7a758df286d507720097de766f38f378b060ba1eba350b59ef01729dcbb21b91b
7
- data.tar.gz: 91592326390e18c981cf25cd668c63a9727c8b3b9ce067ef9006e112d845fcf6c034e324baf3dc5c1a42070c628c6cf9b89d1ce639a244e78735d169ba2becdd
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 likw this:
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"=>"example.com", "port"=>80}}
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. If you need to use a key that is named differently, provide it using the
68
- `key` keyword argument:
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
@@ -1,3 +1,3 @@
1
1
  class ExtendedYAML
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.2"
3
3
  end
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
- data = ::YAML.load evaluate
23
- resolve_extends data
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 = [extra_files] unless extra_files.is_a? Array
54
+ extra_files = expand_file_list extra_files
48
55
 
49
- extra_files.each do |extra_file|
50
- extra_file = extra_file + extension unless extra_file.end_with? extension
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.1.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: 2019-10-25 00:00:00.000000000 Z
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.0.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