text-gen 0.5.1 → 0.6.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: 3dc7468b283cc400df93c020c98c28092c549a7a35bf85be19a727a6972a5f1e
4
- data.tar.gz: ea546fb4e9ab881b5be55e8210de6e41cbca14703e5aa4a7596a58ba88475d44
3
+ metadata.gz: bb11c37e477733c8e2110df029bb47bf31f391d168437fd6a3cceef35ab9f4bb
4
+ data.tar.gz: 6fe3b7da82c39bd823487cc3f59a8c2d62128eba64383a7ffb5cc08d59a1e491
5
5
  SHA512:
6
- metadata.gz: 19295892ca9f132030ada3db189a49c50d7944bcae4d52ac0bdea34e5cecc88ab284795ac8b0eb5568b18b1434120561bf95fca3f752ca0dc8bfc1048fc6f2cc
7
- data.tar.gz: 1e96d78d0dd190b354ffcfb0d170e4541fcc08b37fc7968abc966608a2347f8e915ae7efc2fa088ecc66122070da2f6ba96c9941acfc7422d8a1f5b24b8d62ee
6
+ metadata.gz: 8832f397da93e04f7172cad9db64e6ddcde531a47535c0f9a3832f9fe6202e78dd444970d407c31edc37b597bfe23afd713ce5b6b99f3c21fb789f3c3e605aa6
7
+ data.tar.gz: a74fc34f4b9b08c91dc22518187851b41f3fafab67dd923e6f7f883ceaa5d3cf1b3b0647ef38ed303f4c74aec2ef8877da1d775d1ff7dada1d54ac95ae3ec1a7
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,76 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2026-01-24 21:41:12 UTC using RuboCop version 1.82.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 2
10
+ # This cop supports safe autocorrection (--autocorrect).
11
+ # Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods, NotImplementedExceptions.
12
+ # NotImplementedExceptions: NotImplementedError
13
+ Lint/UnusedMethodArgument:
14
+ Exclude:
15
+ - 'spec/examples/dwarf_spec.rb'
16
+ - 'spec/examples/metadata_spec.rb'
17
+
18
+ # Offense count: 3
19
+ # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
20
+ Metrics/AbcSize:
21
+ Max: 39
22
+
23
+ # Offense count: 23
24
+ # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
25
+ # AllowedMethods: refine
26
+ Metrics/BlockLength:
27
+ Max: 265
28
+
29
+ # Offense count: 2
30
+ # Configuration parameters: CountComments, CountAsOne.
31
+ Metrics/ClassLength:
32
+ Max: 172
33
+
34
+ # Offense count: 3
35
+ # Configuration parameters: AllowedMethods, AllowedPatterns.
36
+ Metrics/CyclomaticComplexity:
37
+ Max: 12
38
+
39
+ # Offense count: 7
40
+ # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
41
+ Metrics/MethodLength:
42
+ Max: 31
43
+
44
+ # Offense count: 1
45
+ # Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
46
+ Metrics/ParameterLists:
47
+ Max: 6
48
+
49
+ # Offense count: 1
50
+ # Configuration parameters: AllowedMethods, AllowedPatterns.
51
+ Metrics/PerceivedComplexity:
52
+ Max: 11
53
+
54
+ # Offense count: 10
55
+ # Configuration parameters: AllowedConstants.
56
+ Style/Documentation:
57
+ Exclude:
58
+ - 'spec/**/*'
59
+ - 'test/**/*'
60
+ - 'lib/text/gen/filter.rb'
61
+ - 'lib/text/gen/function/capitalizer.rb'
62
+ - 'lib/text/gen/function/pluralizer.rb'
63
+ - 'lib/text/gen/function/swapper.rb'
64
+ - 'lib/text/gen/function/titleizer.rb'
65
+ - 'lib/text/gen/segment/constant.rb'
66
+ - 'lib/text/gen/segment/dice.rb'
67
+ - 'lib/text/gen/segment/number.rb'
68
+ - 'lib/text/gen/segment/parser.rb'
69
+ - 'lib/text/gen/segment/reference.rb'
70
+
71
+ # Offense count: 1
72
+ # This cop supports safe autocorrection (--autocorrect).
73
+ # Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, AllowRBSInlineAnnotation, AllowCopDirectives, AllowedPatterns, SplitStrings.
74
+ # URISchemes: http, https
75
+ Layout/LineLength:
76
+ Max: 123
@@ -1,5 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "function/capitalizer"
4
+ require_relative "function/pluralizer"
5
+ require_relative "function/swapper"
6
+ require_relative "function/titleizer"
7
+
3
8
  module Text
4
9
  module Gen
5
10
  class Filter
@@ -11,15 +16,18 @@ module Text
11
16
  filters.each do |filter|
12
17
  case filter["type"]
13
18
  when "capitalize"
14
- result = Result.from(text: result.text.capitalize, type: :function, result:)
19
+ text = Function::Capitalizer.capitalize(result.text)
20
+ result = Result.from(text:, type: :function, result:)
15
21
  when "downcase"
16
22
  result = Result.from(text: result.text.downcase, type: :function, result:)
17
23
  when "upcase"
18
24
  result = Result.from(text: result.text.upcase, type: :function, result:)
19
25
  when "titleize"
20
- result = Result.from(text: Function::Titleizer.titleize(result.text), type: :function, result:)
26
+ text = Function::Titleizer.titleize(result.text)
27
+ result = Result.from(text:, type: :function, result:)
21
28
  when "pluralize"
22
- result = Result.from(text: Function::Pluralizer.pluralize(result.text), type: :function, result:)
29
+ text = Function::Pluralizer.pluralize(result.text)
30
+ result = Result.from(text:, type: :function, result:)
23
31
  when "clear"
24
32
  result = Result.from(text: result.text, type: :function, result:)
25
33
  result.clear_meta(filter["key"], filter["value"])
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Text
4
+ module Gen
5
+ module Function
6
+ class Capitalizer
7
+ class << self
8
+ def capitalize(str)
9
+ str.capitalize
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -55,9 +55,9 @@ module Text
55
55
  end
56
56
 
57
57
  def single_letter(str)
58
- if str =~ /^[a-zA-Z0-9]$/
59
- "#{str}'s"
60
- end
58
+ return unless str =~ /^[a-zA-Z0-9]$/
59
+
60
+ "#{str}'s"
61
61
  end
62
62
 
63
63
  def others(str)
@@ -5,8 +5,8 @@ module Text
5
5
  # ResultAccumulator handles accumulating results with attempt tracking
6
6
  class ResultAccumulator
7
7
  class << self
8
- def accumulate(unique:, count:, max_attempts:, &block)
9
- new(unique: unique, count: count, max_attempts: max_attempts).accumulate(&block)
8
+ def accumulate(unique:, count:, max_attempts:, &)
9
+ new(unique: unique, count: count, max_attempts: max_attempts).accumulate(&)
10
10
  end
11
11
  end
12
12
 
@@ -36,6 +36,7 @@ module Text
36
36
 
37
37
  def run(count: 1)
38
38
  builder = fetch_builder(key)
39
+ return Array.new(count, error_result("{builder `#{key}` not found}")) unless builder
39
40
 
40
41
  ResultAccumulator.accumulate(unique: unique?, count: count, max_attempts: max_attempts) do
41
42
  run_builder(key, builder, @request_filters, 0)
@@ -56,13 +57,17 @@ module Text
56
57
  current_filters = merge_filters(builder, filters)
57
58
  current_items = apply_item_filters(builder["items"], current_filters)
58
59
  result = run_items(builder["strategy"], current_items, current_filters, builder["meta"], depth)
59
- return unless result
60
+ return error_result("{run items empty}") unless result
60
61
 
61
62
  result.merge_meta(builder["meta"])
62
63
  result = apply_result_function(result, current_filters)
63
64
  apply_result_filters(result, current_filters)
64
65
  end
65
66
 
67
+ def error_result(text = "{error}")
68
+ Result.new(text:, type: :error)
69
+ end
70
+
66
71
  def run_items(strategy, items, filters, meta, depth)
67
72
  case strategy
68
73
  when "sequence"
@@ -72,6 +77,8 @@ module Text
72
77
  else
73
78
  run_random_item(strategy, items, meta, depth)
74
79
  end
80
+ rescue StandardError => e
81
+ error_result("{#{e.class.name}:#{e.message}}")
75
82
  end
76
83
 
77
84
  def random_item(strategy, items)
@@ -80,14 +87,14 @@ module Text
80
87
  else
81
88
  total, count = random_from_dice(strategy)
82
89
  total -= count # make the roll 0-indexed
90
+ raise NoItemMatched("roll #{total} exceeds #{items.length}") if total >= items.length
91
+
83
92
  items[total]
84
93
  end
85
94
  end
86
95
 
87
96
  def run_random_item(strategy, items, meta, depth)
88
97
  item = random_item(strategy, items)
89
- return unless item
90
-
91
98
  result = run_item(item, depth)
92
99
  result.merge_meta(meta)
93
100
  result
@@ -106,7 +113,7 @@ module Text
106
113
  current_weight += [item.fetch("weight", 1).to_i, 1].max
107
114
  current_weight > rand_weight
108
115
  end
109
- return unless item
116
+ raise NoItemMatched("weight #{rand_weight} exceeds #{total_weight}") unless item
110
117
 
111
118
  result = run_item(item, depth)
112
119
  result.merge_meta(meta)
@@ -12,7 +12,8 @@ module Text
12
12
 
13
13
  {
14
14
  "type" => "constant",
15
- "text" => str
15
+ "text" => str,
16
+ "match" => str
16
17
  }
17
18
  end
18
19
  end
@@ -12,7 +12,8 @@ module Text
12
12
 
13
13
  {
14
14
  "type" => "dice",
15
- "text" => scanner[1]
15
+ "text" => scanner[1],
16
+ "match" => str
16
17
  }
17
18
  end
18
19
  end
@@ -4,7 +4,7 @@ module Text
4
4
  module Gen
5
5
  module Segment
6
6
  class Reference
7
- REFERENCE_MATCHER = /\[\s*([a-z0-9-]+(?::[a-z0-9i]+)*)\s*\]/
7
+ REFERENCE_MATCHER = /\[\s*([a-z0-9-]+(?::[a-z0-9-]+)*)\s*\]/
8
8
  class << self
9
9
  def scan(scanner)
10
10
  str = scanner.scan(REFERENCE_MATCHER)
@@ -13,6 +13,7 @@ module Text
13
13
  {
14
14
  "type" => "reference",
15
15
  "text" => scanner[1],
16
+ "match" => str,
16
17
  "filters" => [],
17
18
  "meta" => {}
18
19
  }
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Text
4
4
  module Gen
5
- VERSION = "0.5.1"
5
+ VERSION = "0.6.0"
6
6
  end
7
7
  end
data/lib/text/gen.rb CHANGED
@@ -1,14 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "gen/version"
4
- require_relative "gen/filter_error"
5
- require_relative "gen/lookup_error"
6
- require_relative "gen/max_attempts_error"
7
- require_relative "gen/max_recursion_error"
8
4
 
9
- require_relative "gen/function/titleizer"
10
- require_relative "gen/function/pluralizer"
11
- require_relative "gen/function/swapper"
12
5
  require_relative "gen/result_accumulator"
13
6
  require_relative "gen/result"
14
7
  require_relative "gen/segment/parser"
@@ -18,7 +11,10 @@ require_relative "gen/runner"
18
11
 
19
12
  module Text
20
13
  module Gen
21
- class Error < StandardError; end
22
- # Your code goes here...
14
+ class MaxRecursionError < StandardError; end
15
+ class MaxAttemptsError < StandardError; end
16
+ class NoItemMatched < StandardError; end
17
+ class FilterError < StandardError; end
18
+ class LookupError < StandardError; end
23
19
  end
24
20
  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.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - G Palmer
@@ -34,6 +34,7 @@ extensions: []
34
34
  extra_rdoc_files: []
35
35
  files:
36
36
  - ".claude/settings.local.json"
37
+ - ".rubocop_todo.yml"
37
38
  - CHANGELOG.md
38
39
  - CODE_OF_CONDUCT.md
39
40
  - LICENSE.txt
@@ -41,13 +42,10 @@ files:
41
42
  - Rakefile
42
43
  - lib/text/gen.rb
43
44
  - lib/text/gen/filter.rb
44
- - lib/text/gen/filter_error.rb
45
+ - lib/text/gen/function/capitalizer.rb
45
46
  - lib/text/gen/function/pluralizer.rb
46
47
  - lib/text/gen/function/swapper.rb
47
48
  - lib/text/gen/function/titleizer.rb
48
- - lib/text/gen/lookup_error.rb
49
- - lib/text/gen/max_attempts_error.rb
50
- - lib/text/gen/max_recursion_error.rb
51
49
  - lib/text/gen/result.rb
52
50
  - lib/text/gen/result_accumulator.rb
53
51
  - lib/text/gen/runner.rb
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Text
4
- module Gen
5
- class FilterError < StandardError; end
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Text
4
- module Gen
5
- class LookupError < StandardError; end
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Text
4
- module Gen
5
- class MaxAttemptsError < StandardError; end
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Text
4
- module Gen
5
- class MaxRecursionError < StandardError; end
6
- end
7
- end