snippr 0.16.2 → 0.17.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
  SHA1:
3
- metadata.gz: ab3ac24a4943900527af0835dd4c75fcef4ff65e
4
- data.tar.gz: c5e273b76fa6e7958816d4da9091d123f5439ebc
3
+ metadata.gz: 62a34d096d4e43ff626eb69835c161385d17286c
4
+ data.tar.gz: 9471aec2a2e21590e4510bc1b8d648d0494c5622
5
5
  SHA512:
6
- metadata.gz: cd91728a4b090798135643c02244943f41f60aba1449629eaf1c20b98dad385631dc1c4c62fa1de229774b63939f084e28a33da57698d9176f94a1333562a8d7
7
- data.tar.gz: c0a5d41e4f58b5375054400807b2a61995e96726e2cc99f4c0519430c79cd3b5c13aa9517843631298a324d6bd84c54cb7bda162135586771e7b65379c83a49d
6
+ metadata.gz: 429bbb9e104d98c08e81508c9475d7f5921ab20f4c10cd5e58792c63cd58c61181c08744ac632ca0211045d6027feb6f0b985920bfb3c68d6e45e9727df7811d
7
+ data.tar.gz: 23d835aea3caae6e78c520ab631019a3f99e74c2e57e0704da39d73c598e4f115a7aa436e626145d1883e47210f3bc42bbad0acb1a7aa471f69761493e447b5b
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ Changelog:
2
+ ==========
3
+
4
+ 0.17.0:
5
+ - The _include YAML keyword merges two array by adding both together [potentially breaking]
data/README.md CHANGED
@@ -140,8 +140,26 @@ If you have a snippet `a/path/snippet.snip`:
140
140
  This would merge the metadata from `path/from/root/to/snippet.snip` and `a/path/relative/to/this/snippet.snip` into the containing snippets metadata.
141
141
  If there are duplicate keys the metadata in the main/base snippet is chosen.
142
142
 
143
+ This only works on the *first level* of yaml data.
143
144
  Be aware that the `_include` key is *removed* from the metadata after including.
144
145
 
146
+ If the YAML contains two arrays to be merged, both will be ADDED:
147
+
148
+ ---
149
+ key:
150
+ - a: 1
151
+ ---
152
+
153
+ and
154
+
155
+ ---
156
+ key:
157
+ - b: 2
158
+ ---
159
+
160
+ will result in:
161
+
162
+ {key: [{"a" => 1, "b" => 2}]}
145
163
 
146
164
  ### Including snippr files inside other files
147
165
 
@@ -15,13 +15,15 @@ module Snippr
15
15
  if content =~ /^(---\s*\n.*?\n?)^(---\s*$?)/m
16
16
  content = Regexp.last_match.post_match.strip
17
17
  meta = yaml_load(name, $1)
18
- if meta && meta.keys.include?(INCLUDE)
19
- Array(meta[INCLUDE]).each do |include_path|
20
- if (snippet && include_path.start_with?("./"))
21
- include_path = snippet.name.gsub(/\/.*?$/,"") + "/" + include_path.gsub(/^\.\//, "")
18
+ if meta
19
+ if meta.keys.include?(INCLUDE)
20
+ Array(meta[INCLUDE]).each do |include_path|
21
+ if (snippet && include_path.start_with?("./"))
22
+ include_path = snippet.name.gsub(/\/.*?$/,"") + "/" + include_path.gsub(/^\.\//, "")
23
+ end
24
+ snippet = Snippr.load(include_path)
25
+ meta = deep_yaml_merge(snippet.meta, meta)
22
26
  end
23
- snippet = Snippr.load(include_path)
24
- meta = snippet.meta.deep_merge(meta)
25
27
  end
26
28
  end
27
29
  end
@@ -31,7 +33,22 @@ module Snippr
31
33
  [content, meta]
32
34
  end
33
35
 
34
- private
36
+ private
37
+
38
+ def self.deep_yaml_merge(first_hash, other_hash)
39
+ other_hash.each_pair do |k,v|
40
+ tv = first_hash[k]
41
+ if tv.is_a?(Hash) && v.is_a?(Hash)
42
+ first_hash[k] = deep_yaml_merge(tv, v)
43
+ elsif tv.is_a?(Array) && v.is_a?(Array)
44
+ first_hash[k] = tv.concat(v)
45
+ else
46
+ first_hash[k] = v
47
+ end
48
+ end
49
+ first_hash
50
+ end
51
+
35
52
 
36
53
  def self.yaml_load(name, yml)
37
54
  YAML.load(yml)
data/snippr.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  Gem::Specification.new do |s|
3
3
  s.name = "snippr"
4
- s.version = '0.16.2'
4
+ s.version = '0.17.0'
5
5
  s.date = Time.now
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ["Daniel Harrington", "Thomas Jachmann", "Frank Schumacher"]
@@ -0,0 +1,6 @@
1
+ ---
2
+ tracking:
3
+ promotion:
4
+ -
5
+ a: 1
6
+ ---
@@ -0,0 +1,6 @@
1
+ ---
2
+ tracking:
3
+ promotion:
4
+ -
5
+ b: 2
6
+ ---
@@ -10,6 +10,8 @@ describe Snippr::MetaData do
10
10
  TEST_CONTENT_WITH_INCLUDE = "---\n_include:\n - include/test\n - include/test2\ntest: main\n---"
11
11
  TEST_CONTENT_WITH_RELATIVE_INCLUDE = "---\n_include:\n - ./test\n - ./test2\ntest: main\n---"
12
12
 
13
+ TEST_CONTENT_WITH_MERGE = "---\n_include:\n - merge/array1\n - merge/array2\n---"
14
+
13
15
  describe '.extract' do
14
16
  it 'returns an array with 2 elements [contentstring, metahash]' do
15
17
  result = Snippr::MetaData.extract([:content], TEST_CONTENT)
@@ -44,21 +46,47 @@ describe Snippr::MetaData do
44
46
  expect(result.second).to eq({})
45
47
  end
46
48
 
47
- it "includes other front matter via the _include key" do
48
- result = Snippr::MetaData.extract([:content], TEST_CONTENT_WITH_INCLUDE)
49
- expect(result.second).to eq({"this_is_also"=>"included_from_include_test2_snippet", "test"=>"main", "this_is"=>"included_from_include_test_snippet", "_include"=>["include/test", "include/test2"]})
50
- end
49
+ context "_include" do
50
+ it "includes other front matter via the _include key" do
51
+ result = Snippr::MetaData.extract([:content], TEST_CONTENT_WITH_INCLUDE)
52
+ expect(result.second).to eq({"this_is_also"=>"included_from_include_test2_snippet", "test"=>"main", "this_is"=>"included_from_include_test_snippet", "_include"=>["include/test", "include/test2"]})
53
+ end
51
54
 
52
- it "includes metadata from relative include paths" do
53
- snippet = Snippr.load("include/main")
54
- result = Snippr::MetaData.extract([:content], TEST_CONTENT_WITH_RELATIVE_INCLUDE, snippet)
55
- expect(result.second).to eq({"this_is_also"=>"included_from_include_test2_snippet", "test"=>"main", "this_is"=>"included_from_include_test_snippet", "_include"=>["./test", "./test2"]})
56
- end
55
+ it "includes metadata from relative include paths" do
56
+ snippet = Snippr.load("include/main")
57
+ result = Snippr::MetaData.extract([:content], TEST_CONTENT_WITH_RELATIVE_INCLUDE, snippet)
58
+ expect(result.second).to eq({"this_is_also"=>"included_from_include_test2_snippet", "test"=>"main", "this_is"=>"included_from_include_test_snippet", "_include"=>["./test", "./test2"]})
59
+ end
57
60
 
58
- it "includes other front matter blocks but lets the main block win" do
59
- result = Snippr::MetaData.extract([:content], TEST_CONTENT_WITH_INCLUDE)
60
- expect(result.second).to eq({"this_is_also"=>"included_from_include_test2_snippet", "test"=>"main", "this_is"=>"included_from_include_test_snippet", "_include"=>["include/test", "include/test2"]})
61
+ it "includes other front matter blocks but lets the main block win" do
62
+ result = Snippr::MetaData.extract([:content], TEST_CONTENT_WITH_INCLUDE)
63
+ expect(result.second).to eq({"this_is_also"=>"included_from_include_test2_snippet", "test"=>"main", "this_is"=>"included_from_include_test_snippet", "_include"=>["include/test", "include/test2"]})
64
+ end
65
+
66
+ it "combines a hash containing an array into one" do
67
+ # this merges/adds an array to another array when the included hashes contain the same key:
68
+ # promotion:
69
+ # - a: 1
70
+ #
71
+ # and
72
+ #
73
+ # promotion:
74
+ # - b: 2
75
+ #
76
+ # will result in
77
+ # promotion:
78
+ # - a: 1
79
+ # - b: 2
80
+ #
81
+ # instead of letting the later argument win:
82
+ #
83
+ # promotion:
84
+ # - b: 2
85
+ result = Snippr::MetaData.extract([:content], TEST_CONTENT_WITH_MERGE)
86
+ expect(result.second).to eq({"tracking"=>{"promotion"=>[{"b"=>2}, {"a"=>1}]}, "_include"=>["merge/array1", "merge/array2"]})
87
+ end
61
88
  end
89
+
62
90
  end
63
91
 
64
92
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snippr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.2
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Harrington
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-08-18 00:00:00.000000000 Z
13
+ date: 2014-08-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: i18n
@@ -109,6 +109,7 @@ files:
109
109
  - ".gitignore"
110
110
  - ".rspec"
111
111
  - ".travis.yml"
112
+ - CHANGELOG.md
112
113
  - Gemfile
113
114
  - LICENSE
114
115
  - README.md
@@ -158,6 +159,8 @@ files:
158
159
  - spec/fixtures/include/main.snip
159
160
  - spec/fixtures/include/test.snip
160
161
  - spec/fixtures/include/test2.snip
162
+ - spec/fixtures/merge/array1.snip
163
+ - spec/fixtures/merge/array2.snip
161
164
  - spec/fixtures/meta/broken.snip
162
165
  - spec/fixtures/meta/onlyContent_de.snip
163
166
  - spec/fixtures/meta/withContent.snip
@@ -237,6 +240,8 @@ test_files:
237
240
  - spec/fixtures/include/main.snip
238
241
  - spec/fixtures/include/test.snip
239
242
  - spec/fixtures/include/test2.snip
243
+ - spec/fixtures/merge/array1.snip
244
+ - spec/fixtures/merge/array2.snip
240
245
  - spec/fixtures/meta/broken.snip
241
246
  - spec/fixtures/meta/onlyContent_de.snip
242
247
  - spec/fixtures/meta/withContent.snip