text-gen 0.13.4 → 0.14.0

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: a89e9918a7f7feb924839507c942523f6a765faf4374c2e8c68919c87e98be62
4
- data.tar.gz: 63dc2c142d41e7c3e83b07bfaf1549ef348651ec29ddb2c06516878bed7e4780
3
+ metadata.gz: 2cd85012d5325af6f44024e4daad366ef314984fd424d8f3d640d47a5532fe82
4
+ data.tar.gz: 22568b81be8920c10cf3892faf2da9ababc70623295d6e70a5d4437f0360bc1b
5
5
  SHA512:
6
- metadata.gz: d56e45920ff31deff8c73c86f4c4f845028268863a18a4f86501ecaf5023998d702c7d4948076af5ce5bec8f4a948bb3d63f0387c368c1e041756c8a13ae70d5
7
- data.tar.gz: 8f452deb1b88360ca9f2f7e83ab202192d822ef3b87a6811ce5229c40aeff37262f000a63b004a8d772900888368b1db41083f290082a83cda681e4beedc478a
6
+ metadata.gz: 86b79b0b2c861175fca0fd380064df1317ee44031577bb80292e3ccca01fceabaed964d766d39243675917e748dfee149a38c84cdeaf2d8a19463471bca0a5d0
7
+ data.tar.gz: 56cbb70e4a223f39de5f3457b7fd34067a10ccbff03195483df105b404931a7480e33b8f4b59c9bb5512c6ccf3442fb9a91a272e72fc109899bc785e2ef219e6
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Text
4
+ module Gen
5
+ module Filter
6
+ # Store appends a meta value based on the current historic stack of the
7
+ # result. The key names a component in the result's component tree (a
8
+ # builder key such as `nation-name`, with or without a category prefix)
9
+ # and the value is the meta key that the component's text is appended to.
10
+ #
11
+ # For example `store:nation-name:short-name` on a `nation` builder that
12
+ # generates "The Kingdom of Boring" appends "Boring" to
13
+ # `result.meta["short-name"]` so it can be referenced later with
14
+ # `match`/`select` or read from the final result.
15
+ class Store < Base
16
+ def result(context, result)
17
+ return result if @depth && context.depth != @depth
18
+ return result if key.nil? || value.nil?
19
+
20
+ component = find_component(result)
21
+ return result if component.nil?
22
+
23
+ new_meta = Text::Gen::Meta.append_kv(result.meta.dup, value, component.text)
24
+ return result if new_meta == result.meta
25
+
26
+ Result.from(result, type: component_key, meta: new_meta)
27
+ end
28
+
29
+ private
30
+
31
+ # Depth-first search of the result and its components for the first
32
+ # component whose type matches the key
33
+ def find_component(result)
34
+ return result if type_matches?(result.type)
35
+
36
+ result.components.each do |component|
37
+ found = find_component(component)
38
+ return found if found
39
+ end
40
+
41
+ nil
42
+ end
43
+
44
+ def type_matches?(type)
45
+ return false if type.nil?
46
+
47
+ type == key || type.split(">").last == key
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Text
4
4
  module Gen
5
- VERSION = "0.13.4"
5
+ VERSION = "0.14.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: text-gen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.4
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - G Palmer
@@ -58,6 +58,7 @@ files:
58
58
  - lib/text/gen/filter/remember.rb
59
59
  - lib/text/gen/filter/replace.rb
60
60
  - lib/text/gen/filter/select.rb
61
+ - lib/text/gen/filter/store.rb
61
62
  - lib/text/gen/filter/swap.rb
62
63
  - lib/text/gen/filter/titleize.rb
63
64
  - lib/text/gen/filter/upcase.rb