Module: Imagekitio::Internal::Type::Union Private

Includes:
Converter, Util::SorbetRuntimeSupport
Included in:
Models::Accounts::OriginRequest, Models::Accounts::OriginResponse, Models::Accounts::URLEndpointRequest::URLRewriter, Models::Accounts::URLEndpointResponse::URLRewriter, Models::AssetListResponseItem, Models::Beta::V2::FileUploadParams::Transformation::Post, Models::Beta::V2::FileUploadResponse::SelectedFieldsSchema::DefaultValue, Models::Beta::V2::FileUploadResponse::SelectedFieldsSchema::DefaultValue::Mixed, Models::Beta::V2::FileUploadResponse::SelectedFieldsSchema::MaxValue, Models::Beta::V2::FileUploadResponse::SelectedFieldsSchema::MinValue, Models::Beta::V2::FileUploadResponse::SelectedFieldsSchema::SelectOption, Models::CustomMetadataField::Schema::DefaultValue, Models::CustomMetadataField::Schema::DefaultValue::Mixed, Models::CustomMetadataField::Schema::MaxValue, Models::CustomMetadataField::Schema::MinValue, Models::CustomMetadataField::Schema::SelectOption, Models::CustomMetadataFieldCreateParams::Schema::DefaultValue, Models::CustomMetadataFieldCreateParams::Schema::DefaultValue::Mixed, Models::CustomMetadataFieldCreateParams::Schema::MaxValue, Models::CustomMetadataFieldCreateParams::Schema::MinValue, Models::CustomMetadataFieldCreateParams::Schema::SelectOption, Models::CustomMetadataFieldUpdateParams::Schema::DefaultValue, Models::CustomMetadataFieldUpdateParams::Schema::DefaultValue::Mixed, Models::CustomMetadataFieldUpdateParams::Schema::MaxValue, Models::CustomMetadataFieldUpdateParams::Schema::MinValue, Models::CustomMetadataFieldUpdateParams::Schema::SelectOption, Models::ExtensionItem, Models::File::SelectedFieldsSchema::DefaultValue, Models::File::SelectedFieldsSchema::DefaultValue::Mixed, Models::File::SelectedFieldsSchema::MaxValue, Models::File::SelectedFieldsSchema::MinValue, Models::File::SelectedFieldsSchema::SelectOption, Models::FileUploadParams::Transformation::Post, Models::FileUploadResponse::SelectedFieldsSchema::DefaultValue, Models::FileUploadResponse::SelectedFieldsSchema::DefaultValue::Mixed, Models::FileUploadResponse::SelectedFieldsSchema::MaxValue, Models::FileUploadResponse::SelectedFieldsSchema::MinValue, Models::FileUploadResponse::SelectedFieldsSchema::SelectOption, Models::Overlay, Models::OverlayPosition::X, Models::OverlayPosition::Y, Models::OverlayTiming::Duration, Models::OverlayTiming::End, Models::OverlayTiming::Start, Models::SolidColorOverlayTransformation::Gradient, Models::SolidColorOverlayTransformation::Height, Models::SolidColorOverlayTransformation::Radius, Models::SolidColorOverlayTransformation::Width, Models::TextOverlayTransformation::FontSize, Models::TextOverlayTransformation::LineHeight, Models::TextOverlayTransformation::Padding, Models::TextOverlayTransformation::Radius, Models::TextOverlayTransformation::Rotation, Models::TextOverlayTransformation::Width, Models::Transformation::AIDropShadow, Models::Transformation::AspectRatio, Models::Transformation::Duration, Models::Transformation::EndOffset, Models::Transformation::Gradient, Models::Transformation::Height, Models::Transformation::Page, Models::Transformation::Radius, Models::Transformation::Rotation, Models::Transformation::Shadow, Models::Transformation::Sharpen, Models::Transformation::StartOffset, Models::Transformation::Trim, Models::Transformation::UnsharpMask, Models::Transformation::Width, Models::Transformation::X, Models::Transformation::XCenter, Models::Transformation::Y, Models::Transformation::YCenter, Models::UnsafeUnwrapWebhookEvent, Models::UnwrapWebhookEvent, Models::UpdateFileRequest, Models::UpdateFileRequest::UpdateFileDetails::RemoveAITags, Models::UploadPreTransformSuccessEvent::Data::SelectedFieldsSchema::DefaultValue, Models::UploadPreTransformSuccessEvent::Data::SelectedFieldsSchema::DefaultValue::Mixed, Models::UploadPreTransformSuccessEvent::Data::SelectedFieldsSchema::MaxValue, Models::UploadPreTransformSuccessEvent::Data::SelectedFieldsSchema::MinValue, Models::UploadPreTransformSuccessEvent::Data::SelectedFieldsSchema::SelectOption
Defined in:
lib/imagekitio/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.

Examples:

# `overlay` is a `Imagekitio::Overlay`
case overlay
when Imagekitio::TextOverlay
  # ...
when Imagekitio::ImageOverlay
  # ...
when Imagekitio::VideoOverlay
  # ...
else
  puts(overlay)
end

Instance Method Summary collapse

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

Parameters:

  • other (Object)

Returns:



122
123
124
# File 'lib/imagekitio/internal/type/union.rb', line 122

def ==(other)
  Imagekitio::Internal::Type::Union === other && other.derefed_variants == derefed_variants
end

#===(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



111
112
113
114
115
# File 'lib/imagekitio/internal/type/union.rb', line 111

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.

Parameters:

  • value (Object)
  • state (Hash{Symbol=>Object})

    .

    @option state [Boolean] :translate_names

    @option state [Boolean] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Class<StandardError>] :error

    @option state [Integer] :branched

Returns:

  • (Object)


153
154
155
156
157
158
159
160
161
162
163
164
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
# File 'lib/imagekitio/internal/type/union.rb', line 153

def coerce(value, state:)
  if (target = resolve_variant(value))
    return Imagekitio::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 = Imagekitio::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.

Parameters:

  • value (Object)
  • state (Hash{Symbol=>Object})

    .

    @option state [Boolean] :can_retry

Returns:

  • (Object)


201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/imagekitio/internal/type/union.rb', line 201

def dump(value, state:)
  if (target = resolve_variant(value))
    return Imagekitio::Internal::Type::Converter.dump(target, value, state: state)
  end

  known_variants.each do
    target = _2.call
    return Imagekitio::Internal::Type::Converter.dump(target, value, state: state) if target === value
  end

  super
end

#hashInteger

Returns:

  • (Integer)


129
# File 'lib/imagekitio/internal/type/union.rb', line 129

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.

Parameters:

  • depth (Integer) (defaults to: 0)

Returns:

  • (String)


237
238
239
240
241
242
243
244
245
246
# File 'lib/imagekitio/internal/type/union.rb', line 237

def inspect(depth: 0)
  if depth.positive?
    return is_a?(Module) ? super() : self.class.name
  end

  members = variants.map { Imagekitio::Internal::Type::Converter.inspect(_1, depth: depth.succ) }
  prefix = is_a?(Module) ? name : self.class.name

  "#{prefix}[#{members.join(' | ')}]"
end

#to_sorbet_typeObject

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.

Returns:

  • (Object)


217
218
219
220
221
222
223
224
225
226
227
# File 'lib/imagekitio/internal/type/union.rb', line 217

def to_sorbet_type
  types = variants.map { Imagekitio::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1) }.uniq
  case types
  in []
    T.noreturn
  in [type]
    type
  else
    T.any(*types)
  end
end

#variantsArray<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.

Returns:

  • (Array<Object>)


41
# File 'lib/imagekitio/internal/type/union.rb', line 41

def variants = derefed_variants.map { _2 }