bookshop-array-structures 2.0.0.pre.alpha.7 → 2.0.0.pre.alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd2df4cf6c4e92bfd97e35c225f46e023741544e663ad394ec6a460d0960d43b
4
- data.tar.gz: 150ce500ea36bfc8f901b8c07ae73e805cf02c0ee4e4a842924abdbf38609860
3
+ metadata.gz: 22677855f6b324f9afb59568c281564392c3fdd50777dfe1b90067aedbc11391
4
+ data.tar.gz: 2e16193303b823509f81735a4f9c77ea1a88a49c9f77954f218dd3136ab8d5a0
5
5
  SHA512:
6
- metadata.gz: f830c97c0ba625a1cd7900a771bf0db02dff11a9e52bdd1b88bbcba9ac38e7c1e8a6e72fa166a26249c32f46bf1b3b1ddefa812e7024d189a2d4f89a2f178760
7
- data.tar.gz: '08d21dd06e49f51718253ccbbd2484e67987e62297c7d2a08dcd901857ffde83484f8b59c2b20a39307fc191ad9deb322ccbe60e3818e77f078e8738808036ee'
6
+ metadata.gz: c58f22533b9570e8fd2a1b5026e57efae6633cadb20eb735bfbc6d2fd99276c36bd8b0ab01ec87e236fdf9d80521bada5ebd9395b50b8b84099422cf6294064d
7
+ data.tar.gz: ad50d2713c9c761e30b5e486b865663f503e5458e48b768bbd005a4ccd3f4a893eddca731728890e877fee166c17ae4f3b121ce63f0fea3b99941314735c8bf3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bookshop-array-structures (2.0.0.pre.alpha.3)
4
+ bookshop-array-structures (2.0.0.pre.alpha.7)
5
5
  dry-inflector (>= 0.1, < 1.0)
6
6
  jekyll (>= 3.7, < 5.0)
7
7
  toml-rb (>= 2.0, < 3.0)
@@ -16,7 +16,7 @@ GEM
16
16
  builder (3.2.4)
17
17
  citrus (3.0.2)
18
18
  colorator (1.1.0)
19
- concurrent-ruby (1.1.8)
19
+ concurrent-ruby (1.1.9)
20
20
  dry-inflector (0.2.0)
21
21
  em-websocket (0.5.2)
22
22
  eventmachine (>= 0.12.9)
@@ -171,7 +171,103 @@ module Bookshop
171
171
  result["array_structures"].push("bookshop_components")
172
172
  end
173
173
  result.delete("_hidden") unless result["_hidden"].nil?
174
- return result
174
+ results = [result]
175
+ if result["_template"]
176
+ results.push(transform_template_component(result))
177
+ end
178
+ result.delete("_template")
179
+ return results
180
+ end
181
+
182
+ def self.transform_template_component(result)
183
+ schema_result = Marshal.load(Marshal.dump(result))
184
+ unwrap_structure_template(schema_result)
185
+ schema_result["value"] = templatize_values(schema_result["value"])
186
+ schema_result["_comments"] = templatize_comments(schema_result["_comments"])
187
+ schema_result["value"]["_bookshop_name"] = "#{schema_result["value"]["_bookshop_name"]}.__template"
188
+ schema_result["label"] = "Templated #{schema_result["label"]}"
189
+ schema_result["_array_structures"] = {}
190
+ schema_result["_select_data"] = {}
191
+ schema_result.delete("_template")
192
+ schema_result
193
+ end
194
+
195
+ # Breadth-first search through the structure, looking for keys
196
+ # that will match array structure or comments and flattening them
197
+ # into the root structure
198
+ def self.unwrap_structure_template(structure)
199
+ flattened_keys = {}
200
+ structure["value"].each_pair do |base_key, base_value|
201
+ flattened_keys[base_key] = base_value if base_key.start_with? "_"
202
+ end
203
+ flatten_hash(structure["value"]).each do |flat_key|
204
+ cascade_key = flat_key.split(".").last
205
+ matched_comment = structure.dig("_comments", cascade_key)
206
+ matched_substructure = structure.dig("_array_structures", cascade_key, "values", 0)
207
+
208
+ if matched_substructure
209
+ unwrap_structure_template(matched_substructure)
210
+ matched_substructure["value"].each_pair do |subkey, subvalue|
211
+ # Pull substructure's flat keys into this structure
212
+ flattened_keys["#{flat_key}.#{subkey}"] = subvalue
213
+ end
214
+ matched_substructure["_comments"].each_pair do |subkey, subcomment|
215
+ # Pull substructure's comments into this structure
216
+ structure["_comments"]["#{flat_key}.#{subkey}"] = subcomment
217
+ end
218
+ # Mark this key as an array so the include plugin knows to return
219
+ # a value and not a string
220
+ flattened_keys["#{flat_key}.__array_template"] = "{{#{cascade_key}}}"
221
+ if matched_comment
222
+ structure["_comments"].delete(cascade_key)
223
+ structure["_comments"]["#{flat_key}.__array_template"] = matched_comment
224
+ end
225
+ else
226
+ flattened_keys[flat_key] = ""
227
+ if matched_comment
228
+ structure["_comments"].delete(cascade_key)
229
+ structure["_comments"][flat_key] = matched_comment
230
+ end
231
+ end
232
+ end
233
+ structure["value"] = flattened_keys
234
+ end
235
+
236
+ # Recursively convert a hash into flat dot-notation keys
237
+ def self.flatten_hash(hash)
238
+ keys = [];
239
+ hash.each_pair do |k, v|
240
+ next if k.start_with? "_"
241
+ if v.is_a? Hash
242
+ flatten_hash(v).each do |ik|
243
+ keys.push("#{k}.#{ik}")
244
+ end
245
+ else
246
+ keys.push(k)
247
+ end
248
+ end
249
+ keys
250
+ end
251
+
252
+ def self.templatize_values(hash)
253
+ templated_hash = hash.dup
254
+ hash.each_key do |k|
255
+ next if k.start_with? "_"
256
+ next if k.end_with? "__array_template"
257
+ templated_hash.delete(k)
258
+ templated_hash["#{k}.__template"] = "{{#{k.split('.').last}}}"
259
+ end
260
+ templated_hash
261
+ end
262
+
263
+ def self.templatize_comments(hash)
264
+ templated_hash = hash.dup
265
+ hash.each_pair do |k, comment|
266
+ next if k.end_with? "__array_template"
267
+ templated_hash.delete(k)
268
+ templated_hash["#{k}.__template"] = comment
269
+ end
270
+ templated_hash
175
271
  end
176
272
 
177
273
  def self.transform_legacy_component(path, component, site)
@@ -192,7 +288,7 @@ module Bookshop
192
288
  result["array_structures"].push("components")
193
289
  end
194
290
  result.delete("_hidden") unless result["_hidden"].nil?
195
- return result
291
+ return [result]
196
292
  end
197
293
 
198
294
  def self.rewrite_bookshop_toml(content)
@@ -235,17 +331,19 @@ module Bookshop
235
331
  puts exception
236
332
  next
237
333
  end
238
- transformed_component = transform_component(f, component, site)
239
- array_structures = transformed_component.delete("array_structures")
240
- array_structures.each{|key|
241
- begin
242
- site.config["_array_structures"][key] ||= {}
243
- site.config["_array_structures"][key]["values"] ||= []
244
- site.config["_array_structures"][key]["values"].push(transformed_component)
245
- rescue => exception
246
- puts "❌ Error Adding Story to Array Structures: " + f
247
- puts "🤔 Maybe your current _config.yml has conflicting array structures?"
248
- end
334
+ transformed_components = transform_component(f, component, site)
335
+ transformed_components.each{|transformed_component|
336
+ array_structures = transformed_component.delete("array_structures")
337
+ array_structures.each{|key|
338
+ begin
339
+ site.config["_array_structures"][key] ||= {}
340
+ site.config["_array_structures"][key]["values"] ||= []
341
+ site.config["_array_structures"][key]["values"].push(transformed_component)
342
+ rescue => exception
343
+ puts " Error Adding Story to Array Structures: " + f
344
+ puts "🤔 Maybe your current _config.yml has conflicting array structures?"
345
+ end
346
+ }
249
347
  }
250
348
  end
251
349
  end
@@ -1,5 +1,5 @@
1
1
  module Bookshop
2
2
  module Arraystructures
3
- VERSION = "2.0.0.pre.alpha.7"
3
+ VERSION = "2.0.0.pre.alpha.8"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bookshop-array-structures
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre.alpha.7
4
+ version: 2.0.0.pre.alpha.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tate
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-02 00:00:00.000000000 Z
11
+ date: 2021-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll