yaml_extend 0.1.1 → 0.2.0

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: c5ab39dd4d1b790d1d73de077e99b30963040951ef98b583085ce58331083144
4
- data.tar.gz: a67afd6dce0a398d466efda67c773d00bdaffeaa5eddac74ab07469e425e9177
3
+ metadata.gz: e05fc2afe5d8f44419bf0c3d7a2fc801ab04e81864114b000080f6beea8337fd
4
+ data.tar.gz: 46ac756ebcb6b620930eeda313673c2a5990d6e78f8fe7bf4c609576664429a3
5
5
  SHA512:
6
- metadata.gz: 604d8a4232dfe3eed9d74d8077ee16c9cfe7341c730fc06667a0bfdd0ab24d3a9bdf67e0ee5b87f4230d1b14c010d9b187b3dd5d34d8db3b3f734fca544dad46
7
- data.tar.gz: 12cee38d68fabaee77b4cfd039d17bafce15b3545fd4937bf25135a8c5a812c05072162b8592c0fd742751a572be6de331e94ccd43786fe0268d8710d09c8b3b
6
+ metadata.gz: 87f97749740b0954e62bf184418950f57d19612fdead49ae1cc7afb4ce6291f842bff5a887d3b4ba8fda1e2d512a61eb6a9eddf17737fe45136615138ddec27b
7
+ data.tar.gz: a922bc2aa0479db54e03bda900535bad1abfb23a03360f1024aa0caea3a7a0d478c6a6861a8c8af775f9e4df63dc8b88bac93f6e755cb8fd66fd90fe941e5c45
data/README.md CHANGED
@@ -3,16 +3,6 @@
3
3
  Extends YAML to support file based inheritance,
4
4
  to e.g. to build a configuration hierachy.
5
5
 
6
- It is possible to build inheritance trees like
7
- ```
8
- default.yml english.yml default.yml german.yml de.yml
9
- \ / \ / |
10
- uk.yml de.yml at.yml
11
- ```
12
-
13
- A file can inherit from as many as you want. Trees can be nested as deep as you want.
14
- The child file overwrites same values if given in the parent file.
15
-
16
6
  ## Installation
17
7
 
18
8
  Add this line to your application's Gemfile:
@@ -28,6 +18,24 @@ And then execute:
28
18
  Or install it yourself as:
29
19
 
30
20
  $ gem install yaml_extend
21
+
22
+ ## Common information
23
+
24
+ It is possible to build inheritance trees like
25
+ ```
26
+ default.yml english.yml default.yml german.yml de.yml
27
+ \ / \ / |
28
+ uk.yml de.yml at.yml
29
+ ```
30
+
31
+ A file can inherit from as many as you want. Trees can be nested as deep as you want.
32
+
33
+ YAML files are deep merged, the latest specified child file overwrites the former ones.
34
+ Array values are merged as well by default. You can specifiy this with the 3rd Parameter.
35
+
36
+ The files to inherit from are specified by the key 'extends:' in the YAML file.
37
+ That key can be customized if you prefer another one.
38
+ See the examples below.
31
39
 
32
40
  ## Usage
33
41
  yaml_extend adds the method YAML#ext_load_file to YAML.
@@ -36,7 +44,10 @@ This method works like the original YAML#load_file, by extending it with file in
36
44
 
37
45
  ### Examples
38
46
 
39
- ```
47
+ #### Basic Inheritance
48
+ Given the following both files are defined:
49
+
50
+ ```yaml
40
51
  # start.yml
41
52
  extends: 'super.yml'
42
53
  data:
@@ -46,7 +57,7 @@ data:
46
57
  - 'Raspberrys'
47
58
  ```
48
59
 
49
- ```
60
+ ```yaml
50
61
  # super.yml
51
62
  data:
52
63
  name: 'Unknown'
@@ -55,14 +66,16 @@ data:
55
66
  - 'Bananas'
56
67
  - 'Apples'
57
68
  ```
58
- #### Basic Inheritance
59
- ```
60
- YAML.ext_load_file('start.yml')
61
- ```
62
69
 
63
- results in
70
+ When you then call #ext_load_file
64
71
 
72
+ ```ruby
73
+ YAML.ext_load_file 'start.yml'
65
74
  ```
75
+
76
+ the returned YAML value results in
77
+
78
+ ```yaml
66
79
  data:
67
80
  name: 'Mr. Superman'
68
81
  age: 134
@@ -75,7 +88,10 @@ data:
75
88
 
76
89
  #### Inherit from several files
77
90
 
78
- ```
91
+ If you want to inherit from several files, you can specify a list (Array) of files.
92
+ They are merged from top to bottom, so the latest file "wins" - that means it overwrites duplicate values if they exist with the values in the latest file where they occur.
93
+
94
+ ```yaml
79
95
  extends:
80
96
  - 'super_file.yml'
81
97
  - 'parent_file.yml'
@@ -83,19 +99,36 @@ extends:
83
99
  ```
84
100
 
85
101
  #### Using custom extend key
86
- ```
102
+
103
+ If you don't want to use the default key 'extends:', you can specify your own custom key in two ways.
104
+
105
+ ```yaml
87
106
  #custom1.yml
88
107
  inherit_from:
89
108
  - 'super_file.yml'
90
109
  foo: 'bar'
91
110
  ...
92
111
  ```
93
-
112
+ ##### 1. Specify by parameter
113
+ 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.
114
+ ```ruby
115
+ YAML.ext_load_file 'custom1.yml', 'inherit_from'
116
+ ```
117
+ ##### 2. Global configuration of the key
118
+ You can specify the key by configuration globally. So you only need to set the key once and not as parameter anymore
119
+ ```ruby
120
+ YAML.ext_load_key = 'inherit_from'
121
+ YAML.ext_load_file 'custom1.yml'
122
+ YAML.ext_load_file 'custom2.yml'
94
123
  ```
95
- YAML.ext_load_file('custom1.yml','inherit_from')
124
+ ##### Reset the global key
125
+ To reset the global inheritance key, you can either set it to nil or call the #reset_load_key method.
126
+ ```ruby
127
+ YAML.reset_load_key # more readable
128
+ YAML.ext_load_key = nil # more explicit
96
129
  ```
97
130
  #### Using custom nested extend key
98
- ```
131
+ ```yaml
99
132
  #custom2.yml
100
133
  options:
101
134
  extend_file: 'super_file.yml'
@@ -104,8 +137,8 @@ foo: 'bar'
104
137
  ...
105
138
  ```
106
139
 
107
- ```
108
- YAML.ext_load_file('custom2.yml',['options','extend_file'])
140
+ ```ruby
141
+ YAML.ext_load_file 'custom2.yml', ['options','extend_file']
109
142
  ```
110
143
 
111
144
  ## Documentation
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
3
 
4
- RSpec::Core::RakeTask.new(:spec)
4
+ task :default do
5
+ system 'rake --tasks'
6
+ end
5
7
 
6
- task :default => :spec
8
+ RSpec::Core::RakeTask.new(:spec)
data/lib/yaml_extend.rb CHANGED
@@ -10,6 +10,28 @@ require_relative 'custom_errors/invalid_key_type_error'
10
10
  #
11
11
 
12
12
  module YAML
13
+ # default path in the yaml file where the files to inherit from are defined
14
+ DEFAULT_INHERITANCE_KEY = 'extends'
15
+ @@ext_load_key = nil
16
+ #
17
+ # Set a custom inheritance key globally once.
18
+ # So you don't need to specify it on every call of ext_load_file()
19
+ #
20
+ # @param key [String|Array<String>|nil] the key in the yaml file, containing the file strings to extend from. Set nil or call #reset_load_key to reset the key.
21
+ def self.ext_load_key=(key)
22
+ if key.is_a?(String) || key.is_a?(Array) || key.nil?
23
+ @@ext_load_key = key
24
+ else
25
+ raise "Parameter 'key' must be of type String or Array<String> or nil"
26
+ end
27
+ end
28
+
29
+ #
30
+ # Reset the ext_load_key and use the default settings
31
+ #
32
+ def self.reset_load_key()
33
+ @@ext_load_key = nil
34
+ end
13
35
  #
14
36
  # Extended variant of the #load_file method by providing the
15
37
  # ability to inherit from other YAML file(s)
@@ -21,7 +43,10 @@ module YAML
21
43
  #
22
44
  # @return [Hash] the resulting yaml config
23
45
  #
24
- def self.ext_load_file(yaml_path, inheritance_key='extends', extend_existing_arrays=true, config = {})
46
+ def self.ext_load_file(yaml_path, inheritance_key=nil, extend_existing_arrays=true, config = {})
47
+ if inheritance_key.nil?
48
+ inheritance_key = @@ext_load_key || DEFAULT_INHERITANCE_KEY
49
+ end
25
50
  total_config ||= {}
26
51
  yaml_path = YAML.make_absolute_path yaml_path
27
52
  super_config = YAML.load_file(File.open(yaml_path))
@@ -1,3 +1,3 @@
1
1
  module YamlExtend
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  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.1.1
4
+ version: 0.2.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: 2018-02-22 00:00:00.000000000 Z
11
+ date: 2018-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deep_merge