extended_yaml 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 75375be6f35d137f2c0cc92fcd0a69b8c706ab0f4152566d0b6dc037d0b66204
4
+ data.tar.gz: 6ea26f239f8a148d5e07c0e20f6e30e734834ffe37953be1e79041b47fd21257
5
+ SHA512:
6
+ metadata.gz: 8df1afcc6db22d7a5ce02f67c71839d2e62648af49f0df3532fcec88965506e7a758df286d507720097de766f38f378b060ba1eba350b59ef01729dcbb21b91b
7
+ data.tar.gz: 91592326390e18c981cf25cd668c63a9727c8b3b9ce067ef9006e112d845fcf6c034e324baf3dc5c1a42070c628c6cf9b89d1ce639a244e78735d169ba2becdd
@@ -0,0 +1,77 @@
1
+ Extended YAML
2
+ ==================================================
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/extended_yaml.svg)](https://badge.fury.io/rb/extended_yaml)
5
+
6
+ ---
7
+
8
+ ExtendedYAML adds a couple of additional features to the standard YAML
9
+ library:
10
+
11
+ 1. Each YAML file can extend (inherit from) other YAML files by specifying
12
+ `extends: other_file`
13
+ 2. YAML files are parsed for ERB tags.
14
+
15
+ It is a simpler reimplementation of [yaml_extend][1].
16
+
17
+
18
+ Installation
19
+ --------------------------------------------------
20
+
21
+
22
+ $ gem install extended_yaml
23
+
24
+
25
+
26
+ Usage
27
+ --------------------------------------------------
28
+
29
+ Given this [simple.yml](examples/simple.yml) file:
30
+
31
+ ```yaml
32
+ extends: subdir/production.yml
33
+
34
+ settings:
35
+ host: localhost
36
+ port: 80
37
+ ```
38
+
39
+ which uses `extends` to load this
40
+ [subdir/production.yml](examples/subdir/production.yml) file.
41
+
42
+ ```yaml
43
+ settings:
44
+ host: example.com
45
+ ```
46
+
47
+ We can now load the extended YAML file likw this:
48
+
49
+ ```ruby
50
+ # Load an extended YAML
51
+ require 'extended_yaml'
52
+
53
+ p ExtendedYAML.load 'examples/simple.yml'
54
+ #=> {"settings"=>{"host"=>"example.com", "port"=>80}}
55
+ ```
56
+
57
+ Notes
58
+ --------------------------------------------------
59
+
60
+ 1. Arrays will be merged.
61
+ 2. Nested hashes will be merged.
62
+ 3. Other types of values will be overridden based on which loaded file was
63
+ the last to define them.
64
+ 4. ERB tags will be evaluated in all YAML files.
65
+ 5. The `extends` option can use either a single file string, or an array.
66
+ 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
+ ```ruby
70
+ ExtendedYAML.load 'examples/simple.yml, key: 'include'
71
+ ```
72
+
73
+ See the [examples/master.yml](examples/master.yml) file for additional
74
+ information.
75
+
76
+
77
+ [1]: https://github.com/magynhard/yaml_extend
@@ -0,0 +1,57 @@
1
+ require 'extended_yaml/deep_merge'
2
+ require 'erb'
3
+ require 'yaml'
4
+
5
+ class ExtendedYAML
6
+ using DeepMerge
7
+ attr_reader :file, :key
8
+
9
+ # @param [String] file path to YAML file
10
+ # @param [String] key YAML key to use for loading files
11
+ # @return [Hash, Array] the parsed YAML
12
+ def self.load(file, key: 'extends')
13
+ new(file, key: key).result
14
+ end
15
+
16
+ def initialize(file, key: 'extends')
17
+ @file, @key = file, key
18
+ end
19
+
20
+ # @return [Hash, Array] the parsed YAML
21
+ def result
22
+ data = ::YAML.load evaluate
23
+ resolve_extends data
24
+ end
25
+
26
+ # @return [String] the YAML string, with evaluated and ERB
27
+ def evaluate
28
+ ERB.new(File.read file).result
29
+ end
30
+
31
+ private
32
+
33
+ def extension
34
+ @extension ||= File.extname file
35
+ end
36
+
37
+ def base_dir
38
+ @base_dir ||= File.dirname file
39
+ end
40
+
41
+ # @param [Hash] data structure, possibly with 'extends' array
42
+ # @return [Hash] the merged data
43
+ def resolve_extends(data)
44
+ extra_files = data.delete key
45
+ return data unless extra_files
46
+
47
+ extra_files = [extra_files] unless extra_files.is_a? Array
48
+
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
53
+ end
54
+
55
+ data
56
+ end
57
+ end
@@ -0,0 +1,24 @@
1
+ class ExtendedYAML
2
+ # Hash refinement to allow deep merging of other hashes.
3
+ module DeepMerge
4
+ refine Hash do
5
+ def deep_merge(other_hash, &block)
6
+ dup.deep_merge! other_hash, &block
7
+ end
8
+
9
+ def deep_merge!(other_hash, &block)
10
+ merge!(other_hash) do |key, this_val, other_val|
11
+ if this_val.is_a?(Hash) && other_val.is_a?(Hash)
12
+ this_val.deep_merge(other_val, &block)
13
+ elsif this_val.is_a? Array and other_val.is_a? Array
14
+ this_val + other_val
15
+ # elsif block_given?
16
+ # block.call(key, this_val, other_val)
17
+ else
18
+ other_val
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ class ExtendedYAML
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extended_yaml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Load YAML files that deep merge other YAML files
14
+ email: db@dannyben.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/extended_yaml.rb
21
+ - lib/extended_yaml/deep_merge.rb
22
+ - lib/extended_yaml/version.rb
23
+ homepage: https://github.com/dannyben/extended_yaml
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 2.3.0
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 3.0.3
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Load YAML files that merge other YAML files
46
+ test_files: []