text-gen 0.1.0 → 0.2.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.rb +27 -10
- data/lib/text/gen/result_accumulator.rb +42 -0
- data/lib/text/gen/runner.rb +17 -94
- data/lib/text/gen/version.rb +1 -1
- data/lib/text/gen.rb +1 -0
- 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: a974f3f11c14b16da1838a5a8c53395b41c33e08c7adfc32c36306ea79f9d602
|
|
4
|
+
data.tar.gz: 3b389b465cb6b135fae0c11060e42a1bd45e26e484c946180ae2b54452a4dba7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9354feb07dec71c9f35a149c1d3ed6a7b82db54a91d8bcdf77d044659cc1907539358bae8e78befcb44c7ee139bd5edee90c07901a3b991049301aab83c4e1ad
|
|
7
|
+
data.tar.gz: df9160a73bddb71420d441e376da8882cc42c42beccac551c21b4a994b7ea8bd42892c8acee82f177ef0eb03bf6c81b7a2109bb9baa0129dc719a4e390d4584d
|
data/lib/text/gen/filter.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
module Text
|
|
2
2
|
module Gen
|
|
3
3
|
class Filter
|
|
4
|
+
SEPARATORS = { "tab" => "\t", "newline" => "\n", "space" => " " }.freeze
|
|
4
5
|
class << self
|
|
5
6
|
def functions(result, filters)
|
|
6
7
|
return result if filters.nil? || filters.empty?
|
|
@@ -63,6 +64,31 @@ module Text
|
|
|
63
64
|
nil
|
|
64
65
|
end
|
|
65
66
|
|
|
67
|
+
# Create a builder that always returns a constant value
|
|
68
|
+
def constant_builder(key, str)
|
|
69
|
+
{
|
|
70
|
+
"key" => key,
|
|
71
|
+
"items" => [
|
|
72
|
+
{
|
|
73
|
+
"segments" => [
|
|
74
|
+
{
|
|
75
|
+
"type" => "constant",
|
|
76
|
+
"text" => str,
|
|
77
|
+
"meta" => {},
|
|
78
|
+
"filters" => []
|
|
79
|
+
}
|
|
80
|
+
],
|
|
81
|
+
"value" => 0,
|
|
82
|
+
"multiplier" => 1,
|
|
83
|
+
"filters" => [],
|
|
84
|
+
"meta" => {}
|
|
85
|
+
}
|
|
86
|
+
],
|
|
87
|
+
"meta" => {},
|
|
88
|
+
"filters" => []
|
|
89
|
+
}
|
|
90
|
+
end
|
|
91
|
+
|
|
66
92
|
# Replace locale is used on an item; if the item is selected and has
|
|
67
93
|
# associated meta value of "locale" and the request is using that locale value
|
|
68
94
|
# then replace the item text with the value of the meta. If there is
|
|
@@ -93,16 +119,7 @@ module Text
|
|
|
93
119
|
return "" unless separator_filter
|
|
94
120
|
return separator_filter["value"] if separator_filter["key"] == "string"
|
|
95
121
|
|
|
96
|
-
|
|
97
|
-
when "tab"
|
|
98
|
-
"\t"
|
|
99
|
-
when "space"
|
|
100
|
-
" "
|
|
101
|
-
when "newline"
|
|
102
|
-
"\n"
|
|
103
|
-
else
|
|
104
|
-
""
|
|
105
|
-
end
|
|
122
|
+
SEPARATORS.fetch(separator_filter["key"], "")
|
|
106
123
|
end
|
|
107
124
|
|
|
108
125
|
def filter_by_type(filters, type)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module Text
|
|
2
|
+
module Gen
|
|
3
|
+
# ResultAccumulator handles accumulating results with attempt tracking
|
|
4
|
+
class ResultAccumulator
|
|
5
|
+
class << self
|
|
6
|
+
def accumulate(unique:, count:, max_attempts:, &block)
|
|
7
|
+
new(unique: unique, count: count, max_attempts: max_attempts).accumulate(&block)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def initialize(unique:, count:, max_attempts:)
|
|
12
|
+
@unique = unique
|
|
13
|
+
@count = count
|
|
14
|
+
@max_attempts = max_attempts
|
|
15
|
+
@results = unique ? {} : []
|
|
16
|
+
@attempts = max_attempts * count
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def accumulate(&block)
|
|
20
|
+
while @results.size < @count
|
|
21
|
+
result = block.call
|
|
22
|
+
add_result(result) if result
|
|
23
|
+
|
|
24
|
+
@attempts -= 1
|
|
25
|
+
raise MaxAttemptsError if @attempts.zero?
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
@results.map { |_, v| v }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def add_result(result)
|
|
34
|
+
if @unique
|
|
35
|
+
@results[result.text] = result
|
|
36
|
+
else
|
|
37
|
+
@results << [result.text, result]
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
data/lib/text/gen/runner.rb
CHANGED
|
@@ -5,103 +5,35 @@ require "json"
|
|
|
5
5
|
|
|
6
6
|
module Text
|
|
7
7
|
module Gen
|
|
8
|
-
# ResultAccumulator handles accumulating results with attempt tracking
|
|
9
|
-
class ResultAccumulator
|
|
10
|
-
class << self
|
|
11
|
-
def accumulate(unique:, count:, max_attempts:, &block)
|
|
12
|
-
new(unique: unique, count: count, max_attempts: max_attempts).accumulate(&block)
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def initialize(unique:, count:, max_attempts:)
|
|
17
|
-
@unique = unique
|
|
18
|
-
@count = count
|
|
19
|
-
@max_attempts = max_attempts
|
|
20
|
-
@results = unique ? {} : []
|
|
21
|
-
@attempts = max_attempts * count
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def accumulate(&block)
|
|
25
|
-
while @results.size < @count
|
|
26
|
-
result = block.call
|
|
27
|
-
add_result(result) if result
|
|
28
|
-
|
|
29
|
-
@attempts -= 1
|
|
30
|
-
raise MaxAttemptsError if @attempts.zero?
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
@results.map { |_, v| v }
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
private
|
|
37
|
-
|
|
38
|
-
def add_result(result)
|
|
39
|
-
if @unique
|
|
40
|
-
@results[result.text] = result
|
|
41
|
-
else
|
|
42
|
-
@results << [result.text, result]
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
# AltLookup handles alternative filter lookups
|
|
48
|
-
class AltLookup
|
|
49
|
-
def initialize(request_filters)
|
|
50
|
-
@filters = {}
|
|
51
|
-
populate(request_filters)
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def key?(key)
|
|
55
|
-
@filters.key?(key)
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def [](key)
|
|
59
|
-
@filters[key]
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
private
|
|
63
|
-
|
|
64
|
-
def populate(request_filters)
|
|
65
|
-
request_filters.each do |f|
|
|
66
|
-
next unless f["type"] == "alt"
|
|
67
|
-
|
|
68
|
-
k = f["key"]
|
|
69
|
-
v = f["value"]
|
|
70
|
-
raise FilterError, "Invalid alt filter" if k.nil? || v.nil? || k.empty?
|
|
71
|
-
|
|
72
|
-
(@filters[k.to_s.downcase] ||= []) << v
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
|
|
77
8
|
# Runner generates results from the builder that is found with the given key.
|
|
78
9
|
class Runner
|
|
79
|
-
attr_reader :key, :
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
lookup:,
|
|
84
|
-
locale: nil,
|
|
85
|
-
unique: false,
|
|
86
|
-
max_recursion: 10,
|
|
87
|
-
max_attempts: 10,
|
|
88
|
-
request_filters: []
|
|
89
|
-
)
|
|
10
|
+
attr_reader :key, :lookup, :max_attempts, :max_recursion
|
|
11
|
+
attr_accessor :unique, :locale, :store
|
|
12
|
+
|
|
13
|
+
def initialize(key:, lookup:, max_recursion: 10, max_attempts: 10, request_filters: [])
|
|
90
14
|
@key = key.to_s.downcase
|
|
91
15
|
@lookup = lookup
|
|
92
|
-
@locale =
|
|
16
|
+
@locale = nil
|
|
93
17
|
@store = Store.new
|
|
94
|
-
@unique =
|
|
18
|
+
@unique = false
|
|
95
19
|
@request_filters = request_filters
|
|
96
20
|
@max_attempts = max_attempts
|
|
97
21
|
@max_recursion = max_recursion
|
|
98
|
-
|
|
22
|
+
populate_replace
|
|
99
23
|
end
|
|
100
24
|
|
|
101
25
|
def unique?
|
|
102
26
|
@unique
|
|
103
27
|
end
|
|
104
28
|
|
|
29
|
+
# A filter of type "replace:key:value" creates a temporary builder
|
|
30
|
+
# that always returns the given value.
|
|
31
|
+
def populate_replace
|
|
32
|
+
@request_filters.each do |f|
|
|
33
|
+
store.add(f["key"], Filter.constant_builder(f["key"], f["value"])) if f["type"] == "replace"
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
105
37
|
def run(count: 1)
|
|
106
38
|
builder = fetch_builder(key)
|
|
107
39
|
|
|
@@ -111,8 +43,8 @@ module Text
|
|
|
111
43
|
end
|
|
112
44
|
|
|
113
45
|
def fetch_builder(key)
|
|
114
|
-
builder = store.find(key) || lookup.call(key)
|
|
115
|
-
store.add(key, builder)
|
|
46
|
+
builder = @store.find(key) || lookup.call(key)
|
|
47
|
+
@store.add(key, builder)
|
|
116
48
|
builder
|
|
117
49
|
end
|
|
118
50
|
|
|
@@ -121,9 +53,6 @@ module Text
|
|
|
121
53
|
depth += 1
|
|
122
54
|
raise MaxRecursionError if depth > max_recursion
|
|
123
55
|
|
|
124
|
-
alt_result = alt_result_for(key, builder)
|
|
125
|
-
return alt_result if alt_result
|
|
126
|
-
|
|
127
56
|
current_filters = merge_filters(builder, filters)
|
|
128
57
|
current_items = apply_item_filters(builder["items"], current_filters)
|
|
129
58
|
|
|
@@ -233,12 +162,6 @@ module Text
|
|
|
233
162
|
items
|
|
234
163
|
end
|
|
235
164
|
|
|
236
|
-
def alt_result_for(key, builder)
|
|
237
|
-
return nil unless @alt_lookup.key?(builder["key"])
|
|
238
|
-
|
|
239
|
-
Result.new(text: @alt_lookup[key], type: :alt, meta: builder["meta"])
|
|
240
|
-
end
|
|
241
|
-
|
|
242
165
|
def locale_result_for(item)
|
|
243
166
|
text = Filter.replace_locale(item["meta"], locale)
|
|
244
167
|
return nil unless text
|
data/lib/text/gen/version.rb
CHANGED
data/lib/text/gen.rb
CHANGED
|
@@ -6,6 +6,7 @@ require_relative "gen/lookup_error"
|
|
|
6
6
|
require_relative "gen/max_attempts_error"
|
|
7
7
|
require_relative "gen/max_recursion_error"
|
|
8
8
|
|
|
9
|
+
require_relative "gen/result_accumulator"
|
|
9
10
|
require_relative "gen/titleizer"
|
|
10
11
|
require_relative "gen/result"
|
|
11
12
|
require_relative "gen/filter"
|
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.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- G Palmer
|
|
@@ -45,6 +45,7 @@ files:
|
|
|
45
45
|
- lib/text/gen/max_attempts_error.rb
|
|
46
46
|
- lib/text/gen/max_recursion_error.rb
|
|
47
47
|
- lib/text/gen/result.rb
|
|
48
|
+
- lib/text/gen/result_accumulator.rb
|
|
48
49
|
- lib/text/gen/runner.rb
|
|
49
50
|
- lib/text/gen/store.rb
|
|
50
51
|
- lib/text/gen/titleizer.rb
|