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.
- checksums.yaml +4 -4
- data/.claude-plugin/marketplace.json +13 -0
- data/.claude-plugin/plugin.json +9 -0
- data/AGENTS.md +47 -54
- data/CHANGELOG.md +53 -640
- data/LICENSE.txt +21 -0
- data/README.md +33 -628
- data/USAGE.md +771 -715
- data/docs/getting_started.md +139 -325
- data/lib/field_struct/base.rb +241 -1149
- data/lib/field_struct/class_macros.rb +163 -0
- data/lib/field_struct/describer.rb +167 -0
- data/lib/field_struct/errors.rb +182 -62
- data/lib/field_struct/exceptions.rb +40 -0
- data/lib/field_struct/field.rb +69 -135
- data/lib/field_struct/field_builder.rb +110 -0
- data/lib/field_struct/field_checks.rb +50 -0
- data/lib/field_struct/introspection.rb +199 -0
- data/lib/field_struct/json_codec.rb +240 -0
- data/lib/field_struct/metadata.rb +65 -260
- data/lib/field_struct/model_name.rb +27 -27
- data/lib/field_struct/option_checker.rb +121 -0
- data/lib/field_struct/partial.rb +49 -0
- data/lib/field_struct/presence.rb +45 -0
- data/lib/field_struct/rbs.rb +151 -271
- data/lib/field_struct/registered_option.rb +113 -0
- data/lib/field_struct/registry.rb +40 -50
- data/lib/field_struct/restrictions.rb +90 -0
- data/lib/field_struct/scaffold.rb +274 -301
- data/lib/field_struct/suggestion.rb +33 -0
- data/lib/field_struct/types/array.rb +147 -126
- data/lib/field_struct/types/base.rb +113 -124
- data/lib/field_struct/types/big_decimal.rb +59 -55
- data/lib/field_struct/types/binary.rb +18 -25
- data/lib/field_struct/types/boolean.rb +74 -97
- data/lib/field_struct/types/builtin.rb +32 -0
- data/lib/field_struct/types/date.rb +23 -137
- data/lib/field_struct/types/datetime.rb +30 -139
- data/lib/field_struct/types/element_options.rb +51 -0
- data/lib/field_struct/types/email.rb +27 -38
- data/lib/field_struct/types/float.rb +58 -56
- data/lib/field_struct/types/format_presets.rb +30 -0
- data/lib/field_struct/types/hash.rb +144 -0
- data/lib/field_struct/types/immutable_string.rb +16 -9
- data/lib/field_struct/types/integer.rb +74 -102
- data/lib/field_struct/types/nested.rb +57 -58
- data/lib/field_struct/types/preset_resolver.rb +9 -24
- data/lib/field_struct/types/rfc3339_format.rb +70 -153
- data/lib/field_struct/types/string.rb +48 -46
- data/lib/field_struct/types/symbol.rb +23 -20
- data/lib/field_struct/types/temporal.rb +232 -0
- data/lib/field_struct/types/time.rb +32 -145
- data/lib/field_struct/types/union.rb +111 -65
- data/lib/field_struct/types/union_pick.rb +139 -0
- data/lib/field_struct/types/url.rb +24 -35
- data/lib/field_struct/types/uuid.rb +24 -35
- data/lib/field_struct/types/value.rb +14 -9
- data/lib/field_struct/types/zone.rb +118 -0
- data/lib/field_struct/version.rb +3 -3
- data/lib/field_struct.rb +102 -122
- data/sig/field_struct.rbs +1180 -2300
- data/sig/manifest.yaml +6 -0
- data/skills/field-struct/SKILL.md +73 -159
- metadata +37 -28
- data/CODE_OF_CONDUCT.md +0 -132
- data/UPGRADING.md +0 -743
- data/lib/field_struct/error.rb +0 -48
- data/lib/field_struct/types/temporal_parser.rb +0 -93
- data/lib/field_struct/types/time_format_resolver.rb +0 -54
- 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
|
|
9
|
-
#
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
#
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
#
|
|
74
|
-
#
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
#
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
"#{
|
|
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
|
-
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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:
|
|
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
|
-
|
|
128
|
-
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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
|
-
|
|
85
|
+
(match[9] == '-' ? -1 : 1) * ((hours * 3600) + (minutes * 60))
|
|
151
86
|
end
|
|
152
87
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
186
|
-
|
|
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
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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
|
-
#
|
|
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
|
-
#
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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 [
|
|
27
|
-
#
|
|
28
|
-
# @
|
|
29
|
-
#
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
refuse_non_scalar!(value) if strict
|
|
38
|
-
value.to_s
|
|
28
|
+
coerce_with(value, strict)
|
|
39
29
|
end
|
|
40
30
|
|
|
41
|
-
#
|
|
31
|
+
# Internal: {#coerce} with the options as one Hash, no keyword splat (see Types::Base.positional_coerce?).
|
|
42
32
|
#
|
|
43
|
-
# @param value [Object]
|
|
44
|
-
# @
|
|
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
|
-
|
|
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
|
-
|
|
48
|
+
value.respond_to?(:strip) && value.strip.empty?
|
|
50
49
|
end
|
|
51
50
|
|
|
52
|
-
# @return [Class]
|
|
51
|
+
# @return [::Class]
|
|
53
52
|
def ruby_type
|
|
54
53
|
::String
|
|
55
54
|
end
|
|
56
55
|
|
|
57
56
|
private
|
|
58
57
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
69
|
-
|
|
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
|
-
#
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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 [::
|
|
14
|
+
# @param value [::Object]
|
|
22
15
|
# @return [::Symbol, nil]
|
|
23
|
-
# @raise [TypeError]
|
|
16
|
+
# @raise [TypeError] for anything but a Symbol or String
|
|
24
17
|
def coerce(value, **)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
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]
|
|
35
|
+
# @return [::Class]
|
|
33
36
|
def ruby_type
|
|
34
37
|
::Symbol
|
|
35
38
|
end
|