text-gen 0.9.1 → 0.10.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 +4 -4
- data/lib/text/gen/filter/item/locale.rb +29 -0
- data/lib/text/gen/filter.rb +11 -18
- data/lib/text/gen/runner.rb +2 -5
- data/lib/text/gen/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fa9bc639bc212fcea40f4d4203eb6c1efe3d8a306d1731da627e2361949c6316
|
|
4
|
+
data.tar.gz: 67e2819d32009c0ed84f8dcfdf4c166ac970167716234b5744d534dd6e9f6ee3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2012f28cd368ca0415c936424371b40dff77c59c7eccd9b2ed958daca83ba598afb6c8dfe480e7e194fec9801237580dc44fac831b6220363740ecce007e3abc
|
|
7
|
+
data.tar.gz: 56892e85ef8c3641437a8ad5d3a0ef4167ce50bae8090cf05e20ea32f374bc9bcade4c26cdb89ca7b7d023eb2684b40b75adb51763d70e06cd19a8c2631eedd6
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../item_filter"
|
|
4
|
+
|
|
5
|
+
module Text
|
|
6
|
+
module Gen
|
|
7
|
+
module Filter
|
|
8
|
+
module Item
|
|
9
|
+
class Locale < ItemFilter
|
|
10
|
+
def apply(items)
|
|
11
|
+
items.map { |item| locale_item(item) || item }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def locale_item(item)
|
|
17
|
+
meta = item["meta"]
|
|
18
|
+
return if meta.nil? || meta.empty?
|
|
19
|
+
|
|
20
|
+
locale_text = meta[key.downcase]&.sample
|
|
21
|
+
return unless locale_text
|
|
22
|
+
|
|
23
|
+
Filter.constant_item(locale_text, item)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/text/gen/filter.rb
CHANGED
|
@@ -18,6 +18,7 @@ require_relative "filter/result/titleize"
|
|
|
18
18
|
require_relative "filter/result/upcase"
|
|
19
19
|
|
|
20
20
|
# Item filters
|
|
21
|
+
require_relative "filter/item/locale"
|
|
21
22
|
require_relative "filter/item/select"
|
|
22
23
|
require_relative "filter/item/reject"
|
|
23
24
|
|
|
@@ -41,6 +42,7 @@ module Text
|
|
|
41
42
|
}.freeze
|
|
42
43
|
|
|
43
44
|
ITEM_FILTER_CLASSES = {
|
|
45
|
+
"locale" => Filter::Item::Locale,
|
|
44
46
|
"select" => Filter::Item::Select,
|
|
45
47
|
"reject" => Filter::Item::Reject
|
|
46
48
|
}.freeze
|
|
@@ -60,6 +62,14 @@ module Text
|
|
|
60
62
|
result
|
|
61
63
|
end
|
|
62
64
|
|
|
65
|
+
def filter_locale(items, filters)
|
|
66
|
+
locale_filters = filters_by_type(filters, "locale")
|
|
67
|
+
return items if locale_filters.empty?
|
|
68
|
+
|
|
69
|
+
filter_instance = ITEM_FILTER_CLASSES["locale"].new(locale_filters.first)
|
|
70
|
+
filter_instance.apply(items)
|
|
71
|
+
end
|
|
72
|
+
|
|
63
73
|
def filter_select(items, filters)
|
|
64
74
|
select_filters = filters_by_type(filters, "select")
|
|
65
75
|
return items if select_filters.empty?
|
|
@@ -129,24 +139,6 @@ module Text
|
|
|
129
139
|
citem
|
|
130
140
|
end
|
|
131
141
|
|
|
132
|
-
# Replace locale is used on an item; if the item is selected and has
|
|
133
|
-
# associated meta value of "locale" and the request is using that locale value
|
|
134
|
-
# then replace the item text with the value of the meta. If there is
|
|
135
|
-
# more than one value, select at random.
|
|
136
|
-
# Returns a new item with locale text and preserved value/multiplier, or nil
|
|
137
|
-
def replace_locale(item, locale)
|
|
138
|
-
return if locale.nil?
|
|
139
|
-
return if item.nil?
|
|
140
|
-
|
|
141
|
-
meta = item["meta"]
|
|
142
|
-
return if meta.nil? || meta.empty?
|
|
143
|
-
|
|
144
|
-
locale_text = meta[locale.downcase]&.sample
|
|
145
|
-
return unless locale_text
|
|
146
|
-
|
|
147
|
-
constant_item(locale_text, item)
|
|
148
|
-
end
|
|
149
|
-
|
|
150
142
|
def separator(filters)
|
|
151
143
|
return "" unless filters
|
|
152
144
|
|
|
@@ -186,6 +178,7 @@ module Text
|
|
|
186
178
|
|
|
187
179
|
# Define which filters require explicit parameters
|
|
188
180
|
FILTERS_WITH_PARAMETERS = {
|
|
181
|
+
"locale" => ["key"],
|
|
189
182
|
"swap" => ["key", "value"],
|
|
190
183
|
"meta" => ["key", "value"],
|
|
191
184
|
"censor" => ["key", "value"],
|
data/lib/text/gen/runner.rb
CHANGED
|
@@ -8,12 +8,11 @@ module Text
|
|
|
8
8
|
# Runner generates results from the builder that is found with the given key.
|
|
9
9
|
class Runner
|
|
10
10
|
attr_reader :key, :lookup, :max_attempts, :max_recursion
|
|
11
|
-
attr_accessor :unique, :
|
|
11
|
+
attr_accessor :unique, :store
|
|
12
12
|
|
|
13
13
|
def initialize(key:, lookup:, max_recursion: 10, max_attempts: 10, request_filters: [])
|
|
14
14
|
@key = key.to_s.downcase
|
|
15
15
|
@lookup = lookup
|
|
16
|
-
@locale = nil
|
|
17
16
|
@store = Store.new
|
|
18
17
|
@unique = false
|
|
19
18
|
@request_filters = request_filters
|
|
@@ -153,9 +152,6 @@ module Text
|
|
|
153
152
|
end
|
|
154
153
|
|
|
155
154
|
def run_item(key, item, depth)
|
|
156
|
-
locale_item = Filter.replace_locale(item, locale)
|
|
157
|
-
item = locale_item || item
|
|
158
|
-
|
|
159
155
|
results = item["segments"].map { |seg| run_segment(seg, depth) }
|
|
160
156
|
result = Result.merge(results,
|
|
161
157
|
value: item["value"],
|
|
@@ -218,6 +214,7 @@ module Text
|
|
|
218
214
|
end
|
|
219
215
|
|
|
220
216
|
def apply_item_filters(items, filters)
|
|
217
|
+
items = Filter.filter_locale(items, @request_filters + filters)
|
|
221
218
|
items = Filter.filter_select(items, filters)
|
|
222
219
|
items = Filter.filter_reject(items, filters)
|
|
223
220
|
raise FilterError if items.empty?
|
data/lib/text/gen/version.rb
CHANGED
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.
|
|
4
|
+
version: 0.10.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- G Palmer
|
|
@@ -43,6 +43,7 @@ files:
|
|
|
43
43
|
- lib/text/gen.rb
|
|
44
44
|
- lib/text/gen/filter.rb
|
|
45
45
|
- lib/text/gen/filter/base.rb
|
|
46
|
+
- lib/text/gen/filter/item/locale.rb
|
|
46
47
|
- lib/text/gen/filter/item/reject.rb
|
|
47
48
|
- lib/text/gen/filter/item/select.rb
|
|
48
49
|
- lib/text/gen/filter/item_filter.rb
|