Module: ModerationAPI::Internal::Type::Union Private
- Includes:
- Converter, Util::SorbetRuntimeSupport
- Included in:
- Models::ContentSubmitParams::Content, Models::ContentSubmitParams::Content::Object::Data, Models::ContentSubmitParams::Policy, Models::ContentSubmitResponse::Content::Modified, Models::ContentSubmitResponse::Content::Modified::ModifiedNestedObjectContent, Models::ContentSubmitResponse::Insight, Models::ContentSubmitResponse::Policy, Models::WebhookEvent, Models::WebhookEvent::QueueItemAction::Data::Object::Item::Content, Models::WebhookEvent::QueueItemAction::Data::Object::Item::Content::Object::Data, Models::WebhookEvent::QueueItemAllowed::Data::Object::Item::Content, Models::WebhookEvent::QueueItemAllowed::Data::Object::Item::Content::Object::Data, Models::WebhookEvent::QueueItemRejected::Data::Object::Item::Content, Models::WebhookEvent::QueueItemRejected::Data::Object::Item::Content::Object::Data, Models::WebhookEvent::QueueItemResolved::Data::Object::Item::Content, Models::WebhookEvent::QueueItemResolved::Data::Object::Item::Content::Object::Data
- Defined in:
- lib/moderation_api/internal/type/union.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Instance Method Summary collapse
- #==(other) ⇒ Boolean
- #===(other) ⇒ Boolean
-
#coerce(value, state:) ⇒ Object
private
Tries to efficiently coerce the given value to one of the known variants.
- #dump(value, state:) ⇒ Object private
- #hash ⇒ Integer
- #inspect(depth: 0) ⇒ String private
- #to_sorbet_type ⇒ Object private
-
#variants ⇒ Array<Object>
private
All of the specified variants for this union.
Methods included from Util::SorbetRuntimeSupport
#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, to_sorbet_type
Methods included from Converter
coerce, dump, inspect, meta_info, new_coerce_state, type_info
Instance Method Details
#==(other) ⇒ Boolean
134 135 136 |
# File 'lib/moderation_api/internal/type/union.rb', line 134 def ==(other) ModerationAPI::Internal::Type::Union === other && other.derefed_variants == derefed_variants end |
#===(other) ⇒ Boolean
123 124 125 126 127 |
# File 'lib/moderation_api/internal/type/union.rb', line 123 def ===(other) known_variants.any? do |_, variant_fn| variant_fn.call === other end end |
#coerce(value, state:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Tries to efficiently coerce the given value to one of the known variants.
If the value cannot match any of the known variants, the coercion is considered non-viable and returns the original value.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/moderation_api/internal/type/union.rb', line 165 def coerce(value, state:) if (target = resolve_variant(value)) return ModerationAPI::Internal::Type::Converter.coerce(target, value, state: state) end strictness = state.fetch(:strictness) exactness = state.fetch(:exactness) alternatives = [] known_variants.each do |_, variant_fn| target = variant_fn.call exact = state[:exactness] = {yes: 0, no: 0, maybe: 0} state[:branched] += 1 coerced = ModerationAPI::Internal::Type::Converter.coerce(target, value, state: state) yes, no, maybe = exact.values if (no + maybe).zero? || (!strictness && yes.positive?) exact.each { exactness[_1] += _2 } state[:exactness] = exactness return coerced elsif maybe.positive? alternatives << [[-yes, -maybe, no], exact, coerced] end end case alternatives.sort_by!(&:first) in [] exactness[:no] += 1 state[:error] = ArgumentError.new("no matching variant for #{value.inspect}") value in [[_, exact, coerced], *] exact.each { exactness[_1] += _2 } coerced end .tap { state[:exactness] = exactness } ensure state[:strictness] = strictness end |
#dump(value, state:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/moderation_api/internal/type/union.rb', line 213 def dump(value, state:) if (target = resolve_variant(value)) return ModerationAPI::Internal::Type::Converter.dump(target, value, state: state) end known_variants.each do target = _2.call if target === value return ModerationAPI::Internal::Type::Converter.dump( target, value, state: state ) end end super end |
#hash ⇒ Integer
141 |
# File 'lib/moderation_api/internal/type/union.rb', line 141 def hash = variants.hash |
#inspect(depth: 0) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
255 256 257 258 259 260 261 262 263 264 |
# File 'lib/moderation_api/internal/type/union.rb', line 255 def inspect(depth: 0) if depth.positive? return is_a?(Module) ? super() : self.class.name end members = variants.map { ModerationAPI::Internal::Type::Converter.inspect(_1, depth: depth.succ) } prefix = is_a?(Module) ? name : self.class.name "#{prefix}[#{members.join(' | ')}]" end |
#to_sorbet_type ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/moderation_api/internal/type/union.rb', line 235 def to_sorbet_type types = variants.map { ModerationAPI::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1) }.uniq case types in [] T.noreturn in [type] type else T.any(*types) end end |
#variants ⇒ Array<Object>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
All of the specified variants for this union.
53 |
# File 'lib/moderation_api/internal/type/union.rb', line 53 def variants = derefed_variants.map { _2 } |