field_struct 0.15.0 → 1.0.0.pre1

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.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/.claude-plugin/marketplace.json +13 -0
  3. data/.claude-plugin/plugin.json +9 -0
  4. data/AGENTS.md +47 -54
  5. data/CHANGELOG.md +53 -640
  6. data/LICENSE.txt +21 -0
  7. data/README.md +33 -628
  8. data/USAGE.md +771 -715
  9. data/docs/getting_started.md +139 -325
  10. data/lib/field_struct/base.rb +241 -1149
  11. data/lib/field_struct/class_macros.rb +163 -0
  12. data/lib/field_struct/describer.rb +167 -0
  13. data/lib/field_struct/errors.rb +182 -62
  14. data/lib/field_struct/exceptions.rb +40 -0
  15. data/lib/field_struct/field.rb +69 -135
  16. data/lib/field_struct/field_builder.rb +110 -0
  17. data/lib/field_struct/field_checks.rb +50 -0
  18. data/lib/field_struct/introspection.rb +199 -0
  19. data/lib/field_struct/json_codec.rb +240 -0
  20. data/lib/field_struct/metadata.rb +65 -260
  21. data/lib/field_struct/model_name.rb +27 -27
  22. data/lib/field_struct/option_checker.rb +121 -0
  23. data/lib/field_struct/partial.rb +49 -0
  24. data/lib/field_struct/presence.rb +45 -0
  25. data/lib/field_struct/rbs.rb +151 -271
  26. data/lib/field_struct/registered_option.rb +113 -0
  27. data/lib/field_struct/registry.rb +40 -50
  28. data/lib/field_struct/restrictions.rb +90 -0
  29. data/lib/field_struct/scaffold.rb +274 -301
  30. data/lib/field_struct/suggestion.rb +33 -0
  31. data/lib/field_struct/types/array.rb +147 -126
  32. data/lib/field_struct/types/base.rb +113 -124
  33. data/lib/field_struct/types/big_decimal.rb +59 -55
  34. data/lib/field_struct/types/binary.rb +18 -25
  35. data/lib/field_struct/types/boolean.rb +74 -97
  36. data/lib/field_struct/types/builtin.rb +32 -0
  37. data/lib/field_struct/types/date.rb +23 -137
  38. data/lib/field_struct/types/datetime.rb +30 -139
  39. data/lib/field_struct/types/element_options.rb +51 -0
  40. data/lib/field_struct/types/email.rb +27 -38
  41. data/lib/field_struct/types/float.rb +58 -56
  42. data/lib/field_struct/types/format_presets.rb +30 -0
  43. data/lib/field_struct/types/hash.rb +144 -0
  44. data/lib/field_struct/types/immutable_string.rb +16 -9
  45. data/lib/field_struct/types/integer.rb +74 -102
  46. data/lib/field_struct/types/nested.rb +57 -58
  47. data/lib/field_struct/types/preset_resolver.rb +9 -24
  48. data/lib/field_struct/types/rfc3339_format.rb +70 -153
  49. data/lib/field_struct/types/string.rb +48 -46
  50. data/lib/field_struct/types/symbol.rb +23 -20
  51. data/lib/field_struct/types/temporal.rb +232 -0
  52. data/lib/field_struct/types/time.rb +32 -145
  53. data/lib/field_struct/types/union.rb +111 -65
  54. data/lib/field_struct/types/union_pick.rb +139 -0
  55. data/lib/field_struct/types/url.rb +24 -35
  56. data/lib/field_struct/types/uuid.rb +24 -35
  57. data/lib/field_struct/types/value.rb +14 -9
  58. data/lib/field_struct/types/zone.rb +118 -0
  59. data/lib/field_struct/version.rb +3 -3
  60. data/lib/field_struct.rb +102 -122
  61. data/sig/field_struct.rbs +1180 -2300
  62. data/sig/manifest.yaml +6 -0
  63. data/skills/field-struct/SKILL.md +73 -159
  64. metadata +37 -28
  65. data/CODE_OF_CONDUCT.md +0 -132
  66. data/UPGRADING.md +0 -743
  67. data/lib/field_struct/error.rb +0 -48
  68. data/lib/field_struct/types/temporal_parser.rb +0 -93
  69. data/lib/field_struct/types/time_format_resolver.rb +0 -54
  70. data/lib/field_struct/types/zone_option.rb +0 -267
@@ -1,60 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'date'
4
- require 'time'
5
-
6
3
  module FieldStruct
7
4
  module Types
8
- # RFC 3339 — the profile of ISO 8601 that "ISO-8601" means in an API
9
- # contract, and what JSON Schema's +date-time+ refers to. This is what the
10
- # +:iso8601+ preset resolves to for {Date}, {Time} and {DateTime}.
11
- #
12
- # FieldStruct parses and renders it itself, for two reasons.
13
- #
14
- # **It cannot be written as a strftime string.** +%z+ renders +\+0000+,
15
- # which RFC 3339 rejects — so the preset named +iso8601+ used to emit a
16
- # value its own documented format would refuse. And no strftime string can
17
- # express "optional fractional seconds", which most JSON APIs emit.
18
- #
19
- # **The stdlib helpers disagree with each other.** Routing +Time+ through
20
- # +Time.iso8601+ and +DateTime+ through +DateTime.iso8601+ would give the
21
- # same declared preset two different contracts:
22
- #
23
- # Time.iso8601('2026-02-30T10:30:00Z') # => 2026-03-02 10:30 UTC
24
- # DateTime.iso8601('2026-02-30T10:30:00Z') # => refused
25
- # Time.iso8601('2026-07-03T10:30Z') # => refused
26
- # DateTime.iso8601('2026-07-03T10:30Z') # => accepted
27
- # DateTime.iso8601('10:30:00Z') # => TODAY at 10:30
28
- #
29
- # +Time.iso8601+ reading 30 February as 2 March is the exact
30
- # silently-wrong-day defect this release exists to remove, so it cannot be
31
- # the thing that implements the strict preset.
32
- #
33
- # The pattern is deliberately narrow: fixed-width fields, a mandatory
34
- # offset, and no week dates, ordinal dates or basic (unseparated) forms.
35
- # That narrowness is the point — it is what lets a caller replace a
36
- # hand-rolled +/\A\d{4}-\d{2}-\d{2}\z/+ with a declared format.
5
+ # INTERNAL. The RFC 3339 reader and writer behind +format: :iso8601+, in a date variant and a timestamp
6
+ # variant. Strict by design (FS-B90): the stdlib's ISO 8601 parsers disagree with each other and one rolls 30 February forward.
37
7
  class Rfc3339Format
38
- # Full-width calendar date. No sign, no week or ordinal forms.
39
- # @return [::Regexp]
40
- DATE = /\A(\d{4})-(\d{2})-(\d{2})\z/
41
-
42
- # Date, +T+, time to whole seconds, optional fraction, mandatory offset.
43
- # +±hhmm+ (no colon) is accepted on input because that is what FieldStruct
44
- # v0.9.0 emitted, and its own output has to keep parsing.
45
- #
46
- # @return [::Regexp]
47
- TIMESTAMP = /\A(\d{4})-(\d{2})-(\d{2})[Tt](\d{2}):(\d{2}):(\d{2})(\.\d+)?([Zz]|[+-]\d{2}:?\d{2})\z/
48
-
49
- # @return [Rfc3339Format] the date-only form, for {Types::Date}
50
- def self.date
51
- @date ||= new(:date)
52
- end
53
-
54
- # @return [Rfc3339Format] the timestamp form, for {Types::Time} and {Types::DateTime}
55
- def self.timestamp
56
- @timestamp ||= new(:timestamp)
57
- end
8
+ DATE_PATTERN = /\A([0-9]{4})-([0-9]{2})-([0-9]{2})\z/
9
+ TIMESTAMP_PATTERN = /
10
+ \A([0-9]{4})-([0-9]{2})-([0-9]{2})
11
+ [Tt]([0-9]{2}):([0-9]{2}):([0-9]{2})
12
+ (?:\.([0-9]+))?
13
+ (?:([Zz])|([+-])([0-9]{2}):?([0-9]{2}))\z
14
+ /x
58
15
 
59
16
  # @return [::Symbol] +:date+ or +:timestamp+
60
17
  attr_reader :kind
@@ -65,132 +22,92 @@ module FieldStruct
65
22
  freeze
66
23
  end
67
24
 
68
- # @return [::Symbol] the preset name this format implements
69
- def name
70
- :iso8601
25
+ # The date-only variant.
26
+ DATE = new(:date)
27
+ # The timestamp variant.
28
+ TIMESTAMP = new(:timestamp)
29
+
30
+ # @param text [::String]
31
+ # @return [::Hash{::Symbol=>::Object}] components: +year mon mday+ and, for a timestamp, +hour min sec subsec offset+
32
+ # (+offset+ in seconds, +Z+ being a fixed 0)
33
+ # @raise [ArgumentError] with the documented messages
34
+ def parse(text)
35
+ match = (kind == :date ? DATE_PATTERN : TIMESTAMP_PATTERN).match(text)
36
+ raise ::ArgumentError, "expected an RFC 3339 #{kind}" unless match
37
+
38
+ components = kind == :date ? date_components(match) : timestamp_components(match)
39
+ check_day(components)
40
+ components
71
41
  end
72
42
 
73
- # The accepted shape, as a regular expression whose source is also valid
74
- # as an ECMA pattern — so a schema or documentation generator can publish
75
- # the enforced contract rather than a prose approximation of it.
76
- #
77
- # @return [::Regexp]
78
- def pattern
79
- kind == :date ? DATE : TIMESTAMP
80
- end
43
+ # @param value [::Date, ::DateTime, ::Time]
44
+ # @return [::String] +YYYY-MM-DD+ or +YYYY-MM-DDThh:mm:ss[.f]Z|+-hh:mm+
45
+ def render(value)
46
+ date_only = kind == :date || (value.is_a?(::Date) && !value.is_a?(::DateTime))
47
+ return padded_render(value, date_only) if value.year.negative?
48
+ return value.strftime('%Y-%m-%d') if date_only
81
49
 
82
- # @return [::String] the format's name, for error messages and inspection
83
- def to_s
84
- 'iso8601 (RFC 3339)'
50
+ "#{value.strftime('%Y-%m-%dT%H:%M:%S')}#{fraction_text(value)}#{offset_text(value)}"
85
51
  end
86
52
 
87
- # Parse a String into date/time fragments.
88
- #
89
- # @param value [::String] the raw string
90
- # @return [::Hash{::Symbol => Object}] +:year+, +:mon+, +:mday+ and, for a
91
- # timestamp, +:hour+, +:min+, +:sec+, +:sec_fraction+, +:offset+
92
- # @raise [ArgumentError] when the string is not RFC 3339, or names a day,
93
- # hour, minute or second that cannot exist
94
- def parse(value)
95
- match = pattern.match(value)
96
- # The value is not echoed: FieldStruct wraps this as "could not be
97
- # coerced: <message>" and that string reaches API responses.
98
- raise ArgumentError, "expected an RFC 3339 #{kind}" if match.nil?
99
-
100
- kind == :date ? date_parts(match) : timestamp_parts(match)
101
- end
53
+ private
102
54
 
103
- # Render a coerced value back to RFC 3339.
104
- #
105
- # @param value [::Date, ::Time, ::DateTime] the coerced value
106
- # @return [::String]
107
- def render(value)
108
- # Date#iso8601, never Date#xmlschema: ActiveSupport redefines the
109
- # latter to emit a full timestamp, which would make output depend on
110
- # whether the host loaded Rails.
111
- return value.iso8601 if kind == :date
55
+ # strftime pads a negative year to four digits after the sign; +format+ counts the sign as one of the four.
56
+ def padded_render(value, date_only)
57
+ date = format('%<y>04d-%<m>02d-%<d>02d', y: value.year, m: value.mon, d: value.mday)
58
+ return date if date_only
112
59
 
113
- "#{value.strftime("%Y-%m-%dT%H:%M:%S")}#{fraction_of(value)}#{offset_of(value)}"
60
+ "#{date}T#{format('%<h>02d:%<i>02d:%<s>02d', h: value.hour, i: value.min, s: value.sec)}" \
61
+ "#{fraction_text(value)}#{offset_text(value)}"
114
62
  end
115
63
 
116
- private
64
+ def date_components(match)
65
+ { year: match[1].to_i, mon: match[2].to_i, mday: match[3].to_i }
66
+ end
117
67
 
118
- # @param match [::MatchData] a {DATE} match
119
- # @return [::Hash{::Symbol => ::Integer}]
120
- def date_parts(match)
121
- year, month, day = match.captures.map { |capture| Integer(capture, 10) }
122
- TemporalParser.ensure_real_day!({year: year, mon: month, mday: day})
68
+ def timestamp_components(match)
69
+ hour = match[4].to_i
70
+ min = match[5].to_i
71
+ sec = match[6].to_i
72
+ raise ::ArgumentError, 'invalid time of day' if hour > 23 || min > 59 || sec > 59
123
73
 
124
- {year: year, mon: month, mday: day}
74
+ { year: match[1].to_i, mon: match[2].to_i, mday: match[3].to_i, hour: hour, min: min, sec: sec,
75
+ subsec: match[7] ? Rational(match[7].to_i, 10**match[7].length) : 0, offset: offset_seconds(match) }
125
76
  end
126
77
 
127
- # @param match [::MatchData] a {TIMESTAMP} match
128
- # @return [::Hash{::Symbol => Object}]
129
- def timestamp_parts(match)
130
- year, month, day, hour, minute, second = match.captures[0, 6].map { |c| Integer(c, 10) }
131
- TemporalParser.ensure_real_day!({year: year, mon: month, mday: day})
132
- # RFC 3339 permits a leap second, but Time rolls :60 into the next
133
- # minute and DateTime clamps it to :59 — the two stdlib classes give
134
- # different answers, so the strict preset refuses it rather than
135
- # picking one silently. Hour 24 is not RFC 3339 at all.
136
- raise ArgumentError, 'invalid time of day' if hour > 23 || minute > 59 || second > 59
137
-
138
- {
139
- year: year, mon: month, mday: day, hour: hour, min: minute, sec: second,
140
- sec_fraction: fraction_from(match.captures[6]),
141
- offset: offset_from(match.captures[7])
142
- }
143
- end
78
+ def offset_seconds(match)
79
+ return 0 if match[8]
144
80
 
145
- # @param text [::String, nil] the +.digits+ capture, if any
146
- # @return [::Rational] seconds after the whole second, exact
147
- def fraction_from(text)
148
- return 0r if text.nil?
81
+ hours = match[10].to_i
82
+ minutes = match[11].to_i
83
+ raise ::ArgumentError, 'invalid UTC offset' if hours > 23 || minutes > 59
149
84
 
150
- ::Kernel.Rational(text[1..], 10**(text.length - 1))
85
+ (match[9] == '-' ? -1 : 1) * ((hours * 3600) + (minutes * 60))
151
86
  end
152
87
 
153
- # @return [::Integer] seconds east of UTC
154
- # @raise [ArgumentError] when the offset names an hour or minute that
155
- # cannot exist. Validated here rather than left to the constructors,
156
- # because they disagree: +Time.new+ refuses a +25:00 offset while
157
- # +DateTime.new+ accepts it and silently drops it to +00:00. A preset
158
- # that means two different things depending on the field's type is the
159
- # defect this class exists to avoid.
160
- # @param text [::String] the offset capture: +Z+ or +±hh:mm+ / +±hhmm+
161
- def offset_from(text)
162
- return 0 if text.casecmp('Z').zero?
163
-
164
- sign = text.start_with?('-') ? -1 : 1
165
- digits = text[1..].delete(':')
166
- hours = Integer(digits[0, 2], 10)
167
- minutes = Integer(digits[2, 2], 10)
168
- raise ArgumentError, 'invalid UTC offset' if hours > 23 || minutes > 59
169
-
170
- sign * ((hours * 3600) + (minutes * 60))
88
+ def check_day(components)
89
+ return if ::Date.valid_date?(components[:year], components[:mon], components[:mday])
90
+
91
+ raise ::ArgumentError, 'invalid date'
171
92
  end
172
93
 
173
- # @param value [::Time, ::DateTime]
174
- # @return [::String] +".123"+, or empty when the value is on a whole second
175
- def fraction_of(value)
176
- subsec = value.is_a?(::Time) ? value.subsec : value.sec_fraction
177
- return '' if subsec.zero?
94
+ def fraction_text(value)
95
+ digits = Temporal.fraction_digits(value)
96
+ return '' if digits.empty?
178
97
 
179
- # Exact digits from the Rational; %f would round to six places and
180
- # invent precision the value never carried.
181
- digits = (subsec.to_r * 1_000_000_000).to_i.to_s.rjust(9, '0').sub(/0+\z/, '')
182
98
  ".#{digits}"
183
99
  end
184
100
 
185
- # @param value [::Time, ::DateTime]
186
- # @return [::String] +"Z"+ for UTC, otherwise +"+HH:MM"+
187
- def offset_of(value)
188
- seconds = value.is_a?(::Time) ? value.utc_offset : (value.offset * 86_400).to_i
101
+ def offset_text(value)
102
+ seconds = value.is_a?(::DateTime) ? (value.offset * 86_400).to_i : value.utc_offset
189
103
  return 'Z' if seconds.zero?
190
104
 
191
- sign = seconds.negative? ? '-' : '+'
192
- magnitude = seconds.abs
193
- format('%s%02d:%02d', sign, magnitude / 3600, (magnitude % 3600) / 60)
105
+ minutes = seconds.abs / 60
106
+ "#{seconds.negative? ? '-' : '+'}#{two_digits(minutes / 60)}:#{two_digits(minutes % 60)}"
107
+ end
108
+
109
+ def two_digits(number)
110
+ number.to_s.rjust(2, '0')
194
111
  end
195
112
  end
196
113
  end
@@ -1,72 +1,74 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'base'
4
-
5
3
  module FieldStruct
6
4
  module Types
7
- # String type. Coerces any non-nil value via +to_s+; treats nil, empty,
8
- # and whitespace-only strings as missing. A field declared +strict: true+
9
- # accepts only scalars (String, Symbol, Integer) and objects
10
- # responding to +to_str+, so a Hash or Array is refused rather than stored
11
- # as its +inspect+ text.
5
+ # Text. Coerces any non-nil value with +to_s+; +strict: true+ narrows what is accepted.
12
6
  class String < Base
13
- # Native options: a +format:+ Regexp (or Symbol preset, used by the
14
- # +:uuid+ / +:url+ / +:email+ subtypes), an +enum:+ Array of
15
- # allowed values, and a +strict:+ Boolean.
16
- #
17
- # @return [::Hash{::Symbol => ::Hash{::Symbol => Object}}]
18
- def self.own_option_schema
19
- {
20
- format: option(type: [::Regexp, ::Symbol]),
21
- enum: option(type: [::Array]),
22
- strict: option(type: [::TrueClass, ::FalseClass])
23
- }
7
+ # What +String#strip+ removes: NUL, tab, LF, VT, FF, CR and space (no other Unicode whitespace).
8
+ BLANK = /\A[\0\t\n\v\f\r ]*\z/
9
+ private_constant :BLANK
10
+
11
+ class << self
12
+ # @return [::Hash{::Symbol=>::Hash{::Symbol=>::Object}}]
13
+ def own_option_schema
14
+ {
15
+ format: option(type: [::Regexp, ::Symbol]),
16
+ enum: option(type: ::Array),
17
+ strict: option(type: [::TrueClass, ::FalseClass]),
18
+ length: option(type: ::Range)
19
+ }
20
+ end
24
21
  end
25
22
 
26
- # @param value [#to_s, nil] anything stringable (everything in
27
- # Ruby responds to +to_s+); +nil+ stays +nil+
28
- # @param strict [Boolean, nil] when true, refuse anything but a String,
29
- # Symbol, Integer or +to_str+ object (a Float, BigDecimal or Rational
30
- # is refused: its +to_s+ is +"0.12e2"+ / +"1/2"+, not the caller's text). Strict never changes
31
- # what an accepted value becomes — it is still +value.to_s+.
32
- # @return [::String, nil] +nil+ when input is +nil+, otherwise +value.to_s+
33
- # @raise [TypeError] under +strict:+, for any other value
23
+ # @param value [::Object]
24
+ # @param strict [Boolean, nil] accept only a String, Symbol, Integer or #to_str responder
25
+ # @return [::String, nil]
26
+ # @raise [TypeError] under +strict: true+ for any other class
34
27
  def coerce(value, strict: false, **)
35
- return nil if value.nil?
36
-
37
- refuse_non_scalar!(value) if strict
38
- value.to_s
28
+ coerce_with(value, strict)
39
29
  end
40
30
 
41
- # Missing if nil, empty, or whitespace-only.
31
+ # Internal: {#coerce} with the options as one Hash, no keyword splat (see Types::Base.positional_coerce?).
42
32
  #
43
- # @param value [Object]
44
- # @return [Boolean]
33
+ # @param value [::Object]
34
+ # @param options [::Hash{::Symbol=>::Object}]
35
+ # @return [::String, nil]
36
+ # @api private
37
+ def coerce_positional(value, options)
38
+ coerce_with(value, options[:strict])
39
+ end
40
+
41
+ # @param value [::Object]
42
+ # @return [Boolean] true for nil, empty and whitespace-only text
45
43
  def missing?(value)
46
44
  return true if value.nil?
47
- return true if value.respond_to?(:strip) && value.strip.empty?
45
+ # Broken bytes skip the fast path so strip decides what is raised, as it always did.
46
+ return value.match?(BLANK) if value.instance_of?(::String) && value.encoding.ascii_compatible? && value.valid_encoding?
48
47
 
49
- false
48
+ value.respond_to?(:strip) && value.strip.empty?
50
49
  end
51
50
 
52
- # @return [Class] the top-level +::String+ class
51
+ # @return [::Class]
53
52
  def ruby_type
54
53
  ::String
55
54
  end
56
55
 
57
56
  private
58
57
 
59
- # @param value [Object] a non-nil value
60
- # @return [void]
61
- # @raise [TypeError] unless +value+ is a String, Symbol, Integer, or
62
- # responds to +to_str+; the message names the class, never the value
63
- # (see Types::Time#coerce)
64
- def refuse_non_scalar!(value)
65
- return if value.is_a?(::String) || value.is_a?(::Symbol) || value.is_a?(::Integer)
66
- return if value.respond_to?(:to_str)
58
+ def coerce_with(value, strict)
59
+ return nil if value.nil?
60
+
61
+ refuse_strict(value) if strict && !strict_input?(value)
62
+ value.to_s
63
+ end
64
+
65
+ def strict_input?(value)
66
+ value.is_a?(::String) || value.is_a?(::Symbol) || value.is_a?(::Integer) || value.respond_to?(:to_str)
67
+ end
67
68
 
68
- raise ::TypeError, 'String fields with strict: true expect a String, Symbol, Integer ' \
69
- "or an object responding to #to_str, got a #{value.class}"
69
+ def refuse_strict(value)
70
+ raise ::TypeError, 'String fields with strict: true expect a String, Symbol, Integer or an object ' \
71
+ "responding to #to_str, got #{class_phrase(value)}"
70
72
  end
71
73
  end
72
74
  end
@@ -1,35 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'base'
4
-
5
3
  module FieldStruct
6
4
  module Types
7
- # Symbol type. Accepts an existing +Symbol+ or a +String+. Anything else
8
- # raises +TypeError+, which the parent's +coercion_policy+ handles.
9
- #
10
- # Empty symbols (+:""+) are unusual; like other scalar non-string types,
11
- # +missing?+ is nil-only — an empty symbol counts as present.
5
+ # Symbols. Strings are interned with +to_sym+; anything else is refused.
12
6
  class Symbol < Base
13
- # Native options: an +enum:+ Array of allowed values (symbols are
14
- # string-like, so they share +enum:+ with {Types::String}).
15
- #
16
- # @return [::Hash{::Symbol => ::Hash{::Symbol => Object}}]
17
- def self.own_option_schema
18
- {enum: option(type: [::Array])}
7
+ class << self
8
+ # @return [::Hash{::Symbol=>::Hash{::Symbol=>::Object}}]
9
+ def own_option_schema
10
+ { enum: option(type: ::Array) }
11
+ end
19
12
  end
20
13
 
21
- # @param value [::Symbol, ::String, nil] anything else raises TypeError
14
+ # @param value [::Object]
22
15
  # @return [::Symbol, nil]
23
- # @raise [TypeError] when input is not nil/Symbol/String
16
+ # @raise [TypeError] for anything but a Symbol or String
24
17
  def coerce(value, **)
25
- return nil if value.nil?
26
- return value if value.is_a?(::Symbol)
27
- return value.to_sym if value.is_a?(::String)
18
+ case value
19
+ when nil, ::Symbol then value
20
+ when ::String then value.to_sym
21
+ else raise ::TypeError, "expected Symbol or String, got #{value.class}"
22
+ end
23
+ end
28
24
 
29
- raise ::TypeError, "expected Symbol or String, got #{value.class}"
25
+ # Internal: {#coerce} with the options as one Hash, no keyword splat (see Types::Base.positional_coerce?).
26
+ #
27
+ # @param value [::Object]
28
+ # @param _options [::Hash{::Symbol=>::Object}] unused: this type takes no options
29
+ # @return [::Symbol, nil]
30
+ # @api private
31
+ def coerce_positional(value, _options)
32
+ coerce(value)
30
33
  end
31
34
 
32
- # @return [Class] the top-level +::Symbol+ class
35
+ # @return [::Class]
33
36
  def ruby_type
34
37
  ::Symbol
35
38
  end