text-gen 0.13.3 → 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: 76b0dea6048d936bc09ab210403b77972ff3308e3b5e7de9a6e799f842d39649
4
- data.tar.gz: 1256216ab450d78408515eab81cd351a8e421d935c4a3d5e26f49fafbab2a147
3
+ metadata.gz: 2cd85012d5325af6f44024e4daad366ef314984fd424d8f3d640d47a5532fe82
4
+ data.tar.gz: 22568b81be8920c10cf3892faf2da9ababc70623295d6e70a5d4437f0360bc1b
5
5
  SHA512:
6
- metadata.gz: 43164ff0dd4db90302168a1dc577962812d1c58b75fbd96e278455c20e44647d270b94f88e6025548457efd53dc05b22785f336bd7626e7b7cc9f18a73ccc05e
7
- data.tar.gz: ffe458f004ca8fe129ff6c9df4dd06f6acf36908dd17b8abbeaae071a4e746d60b402f2cd0d7d03525f406d3357940f6161223fb7e87491644f23d580cfb44df
6
+ metadata.gz: 86b79b0b2c861175fca0fd380064df1317ee44031577bb80292e3ccca01fceabaed964d766d39243675917e748dfee149a38c84cdeaf2d8a19463471bca0a5d0
7
+ data.tar.gz: 56cbb70e4a223f39de5f3457b7fd34067a10ccbff03195483df105b404931a7480e33b8f4b59c9bb5512c6ccf3442fb9a91a272e72fc109899bc785e2ef219e6
@@ -60,6 +60,10 @@ module Text
60
60
  @keys.last
61
61
  end
62
62
 
63
+ def current_key_without_category
64
+ current_key.split(">")&.last
65
+ end
66
+
63
67
  def current_strategy
64
68
  @strategy_stack.last&.first || "weighted"
65
69
  end
@@ -69,12 +73,12 @@ module Text
69
73
  end
70
74
 
71
75
  def remember(result)
72
- @remembered << [current_key, result]
76
+ @remembered << [current_key_without_category, result]
73
77
  result
74
78
  end
75
79
 
76
80
  def remembered(key = nil)
77
- key ||= current_key
81
+ key ||= current_key_without_category
78
82
  @remembered.select { |k, _r| k == key }.map(&:last)
79
83
  end
80
84
 
@@ -6,9 +6,9 @@ module Text
6
6
  # Return nil if the text value has already been generated
7
7
  class Distinct < Base
8
8
  def result(context, result)
9
- return result if key != context.current_key
9
+ return result if key != context.current_key && key != context.current_key_without_category
10
10
 
11
- previous = context.remembered(key)
11
+ previous = context.remembered
12
12
  return if previous.any? { |r| r.text.strip.downcase == result.text.strip.downcase }
13
13
 
14
14
  result
@@ -7,7 +7,7 @@ module Text
7
7
  def result(context, result)
8
8
  return result if key != context.current_key && key != context.current_key_without_category
9
9
 
10
- previous = context.remembered(key)
10
+ previous = context.remembered
11
11
  return previous.sample unless previous.nil? || previous.empty?
12
12
 
13
13
  result
@@ -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.3"
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.3
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