text-gen 0.8.0 → 0.9.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: 60294b39f992c96f317ad35a502145577250616b6955133b1502d4671af29517
4
- data.tar.gz: f9fd5e105682af4c36f11632c1603425b977a59171d6c0597428761e397ce443
3
+ metadata.gz: d1db9182d0f834336babdf875075db0088edf50fd0e395f6a2c65a836d302910
4
+ data.tar.gz: 5d6e8d334f03687fcadf0b97530454eee5989d8507eb80e1f60e40d41ca7eb19
5
5
  SHA512:
6
- metadata.gz: 84dec457f1b0c2853f014f6e75f1136baa40afffebe3ac8c5e15932fd1ab8938367e30509bb26dd65cb4f50c2be9e18149d7c14a61a876a381ee632e2f9573e2
7
- data.tar.gz: 7d7bb543d5fe6d6582746f807cf1d31d1e82211749ec54f8abdade318f97cb069960d3d3ef327bdef9f830a19a450506b11cc446c48f8c88218997c4d6d77b6b
6
+ metadata.gz: '0852fb8a21dfdc559f8f917f5dca8ea32258cac9a7d2a13241aa10bfdd01272bbb7e09068943cb4f8651deb1b980f25d2ad9fd35c34d92827e02a68189b0dac6'
7
+ data.tar.gz: daf86349ff266645e55de15aa61507a3bacf2e47f588a2b18de83992b63927168ba53da40558b155168e420e6b3cfc47aeede89f0e9bf92302c8412069cdd1e8
@@ -31,25 +31,27 @@ module Text
31
31
  SINGLE = Set.new(%w[a an the this his her its my your our that their])
32
32
 
33
33
  def apply(result)
34
- transform_text(result, pluralize(result.text))
34
+ transform_text(result, pluralize(result.text, result.multiplier))
35
35
  end
36
36
 
37
37
  private
38
38
 
39
- def pluralize(str)
39
+ def pluralize(str, multiplier = 1)
40
40
  return str if str.empty?
41
41
 
42
42
  arr = str.split(/\s+/)
43
43
  return str if arr.length < 2
44
44
 
45
45
  idx = arr.rindex { |s| to_num(s) }
46
- return str if idx.nil? || idx >= arr.length - 1
47
46
 
48
- num = to_num(arr[idx])
49
- if num && num > 1
50
- dc = arr[-1].downcase
51
- arr[-1] = exceptions(dc) || single_letter(dc) || others(dc) || ends_with_y(dc) || simple(dc)
52
- end
47
+ # Use the multiplier if available, otherwise parse from text
48
+ num = multiplier > 1 ? multiplier : (idx ? to_num(arr[idx]) : nil)
49
+
50
+ return str if num.nil? || num <= 1
51
+ return str if idx && idx >= arr.length - 1
52
+
53
+ dc = arr[-1].downcase
54
+ arr[-1] = exceptions(dc) || single_letter(dc) || others(dc) || ends_with_y(dc) || simple(dc)
53
55
  arr.join(" ")
54
56
  end
55
57
 
@@ -166,6 +166,38 @@ module Text
166
166
 
167
167
  filters.select { |f| f["type"] == type }
168
168
  end
169
+
170
+ # Returns a hash of all available filters with their parameters
171
+ # Filters that require explicit parameters will have an array of parameter names
172
+ # Filters that don't require parameters will have an empty array
173
+ def available_filters
174
+ result_filters = RESULT_FILTER_CLASSES.keys.each_with_object({}) do |name, hash|
175
+ hash[name] = filter_parameters(name)
176
+ end
177
+
178
+ item_filters = ITEM_FILTER_CLASSES.keys.each_with_object({}) do |name, hash|
179
+ hash[name] = filter_parameters(name)
180
+ end
181
+
182
+ result_filters.merge(item_filters)
183
+ end
184
+
185
+ private
186
+
187
+ # Define which filters require explicit parameters
188
+ FILTERS_WITH_PARAMETERS = {
189
+ "swap" => ["key", "value"],
190
+ "meta" => ["key", "value"],
191
+ "censor" => ["key", "value"],
192
+ "match" => ["key", "value"],
193
+ "exclude" => ["key", "value"],
194
+ "select" => ["key", "value"],
195
+ "reject" => ["key", "value"]
196
+ }.freeze
197
+
198
+ def filter_parameters(filter_name)
199
+ FILTERS_WITH_PARAMETERS.fetch(filter_name, [])
200
+ end
169
201
  end
170
202
  end
171
203
  end
@@ -47,6 +47,13 @@ module Text
47
47
  visit_and_recurse(key, 0).sort.uniq
48
48
  end
49
49
 
50
+ # Returns a hash of all available filters with their parameters.
51
+ # Filters requiring explicit parameters will have an array of parameter names.
52
+ # Filters without parameters will have an empty array.
53
+ def available_filters
54
+ Filter.available_filters
55
+ end
56
+
50
57
  def visit_and_recurse(key, depth)
51
58
  depth += 1
52
59
  raise MaxRecursionError if depth > max_recursion
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Text
4
4
  module Gen
5
- VERSION = "0.8.0"
5
+ VERSION = "0.9.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.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - G Palmer