Module: Orb::Internal::Type::Union Private

Includes:
Converter, Util::SorbetRuntimeSupport
Included in:
Models::AdjustmentInterval::Adjustment, Models::Beta::ExternalPlanIDCreatePlanVersionParams::AddAdjustment::Adjustment, Models::Beta::ExternalPlanIDCreatePlanVersionParams::AddPrice::Price, Models::Beta::ExternalPlanIDCreatePlanVersionParams::ReplaceAdjustment::Adjustment, Models::Beta::ExternalPlanIDCreatePlanVersionParams::ReplacePrice::Price, Models::BetaCreatePlanVersionParams::AddAdjustment::Adjustment, Models::BetaCreatePlanVersionParams::AddPrice::Price, Models::BetaCreatePlanVersionParams::ReplaceAdjustment::Adjustment, Models::BetaCreatePlanVersionParams::ReplacePrice::Price, Models::Coupon::Discount, Models::CouponCreateParams::Discount, Models::CustomerCreateParams::TaxConfiguration, Models::CustomerUpdateByExternalIDParams::TaxConfiguration, Models::CustomerUpdateParams::TaxConfiguration, Models::Customers::Credits::LedgerCreateEntryByExternalIDParams::InvoiceSettings::InvoiceDate, Models::Customers::Credits::LedgerCreateEntryByExternalIDResponse, Models::Customers::Credits::LedgerCreateEntryParams::InvoiceSettings::InvoiceDate, Models::Customers::Credits::LedgerCreateEntryResponse, Models::Customers::Credits::LedgerListByExternalIDResponse, Models::Customers::Credits::LedgerListResponse, Models::Discount, Models::EvaluatePriceGroup::GroupingValue, Models::Invoice::LineItem::Adjustment, Models::Invoice::LineItem::SubLineItem, Models::InvoiceFetchUpcomingResponse::LineItem::Adjustment, Models::InvoiceFetchUpcomingResponse::LineItem::SubLineItem, Models::InvoiceLevelDiscount, Models::InvoiceLineItemCreateResponse::Adjustment, Models::InvoiceLineItemCreateResponse::SubLineItem, Models::MutatedSubscription::DiscountInterval, Models::NewFloatingBPSPrice::ConversionRateConfig, Models::NewFloatingBulkBPSPrice::ConversionRateConfig, Models::NewFloatingBulkPrice::ConversionRateConfig, Models::NewFloatingBulkWithProrationPrice::ConversionRateConfig, Models::NewFloatingCumulativeGroupedBulkPrice::ConversionRateConfig, Models::NewFloatingGroupedAllocationPrice::ConversionRateConfig, Models::NewFloatingGroupedTieredPackagePrice::ConversionRateConfig, Models::NewFloatingGroupedTieredPrice::ConversionRateConfig, Models::NewFloatingGroupedWithMeteredMinimumPrice::ConversionRateConfig, Models::NewFloatingGroupedWithProratedMinimumPrice::ConversionRateConfig, Models::NewFloatingMatrixPrice::ConversionRateConfig, Models::NewFloatingMatrixWithAllocationPrice::ConversionRateConfig, Models::NewFloatingMatrixWithDisplayNamePrice::ConversionRateConfig, Models::NewFloatingMaxGroupTieredPackagePrice::ConversionRateConfig, Models::NewFloatingPackagePrice::ConversionRateConfig, Models::NewFloatingPackageWithAllocationPrice::ConversionRateConfig, Models::NewFloatingScalableMatrixWithTieredPricingPrice::ConversionRateConfig, Models::NewFloatingScalableMatrixWithUnitPricingPrice::ConversionRateConfig, Models::NewFloatingThresholdTotalAmountPrice::ConversionRateConfig, Models::NewFloatingTieredBPSPrice::ConversionRateConfig, Models::NewFloatingTieredPackagePrice::ConversionRateConfig, Models::NewFloatingTieredPackageWithMinimumPrice::ConversionRateConfig, Models::NewFloatingTieredPrice::ConversionRateConfig, Models::NewFloatingTieredWithMinimumPrice::ConversionRateConfig, Models::NewFloatingTieredWithProrationPrice::ConversionRateConfig, Models::NewFloatingUnitPrice::ConversionRateConfig, Models::NewFloatingUnitWithPercentPrice::ConversionRateConfig, Models::NewFloatingUnitWithProrationPrice::ConversionRateConfig, Models::NewPlanBPSPrice::ConversionRateConfig, Models::NewPlanBulkBPSPrice::ConversionRateConfig, Models::NewPlanBulkPrice::ConversionRateConfig, Models::NewPlanBulkWithProrationPrice::ConversionRateConfig, Models::NewPlanCumulativeGroupedBulkPrice::ConversionRateConfig, Models::NewPlanGroupedAllocationPrice::ConversionRateConfig, Models::NewPlanGroupedTieredPackagePrice::ConversionRateConfig, Models::NewPlanGroupedTieredPrice::ConversionRateConfig, Models::NewPlanGroupedWithMeteredMinimumPrice::ConversionRateConfig, Models::NewPlanGroupedWithProratedMinimumPrice::ConversionRateConfig, Models::NewPlanMatrixPrice::ConversionRateConfig, Models::NewPlanMatrixWithAllocationPrice::ConversionRateConfig, Models::NewPlanMatrixWithDisplayNamePrice::ConversionRateConfig, Models::NewPlanMaxGroupTieredPackagePrice::ConversionRateConfig, Models::NewPlanPackagePrice::ConversionRateConfig, Models::NewPlanPackageWithAllocationPrice::ConversionRateConfig, Models::NewPlanScalableMatrixWithTieredPricingPrice::ConversionRateConfig, Models::NewPlanScalableMatrixWithUnitPricingPrice::ConversionRateConfig, Models::NewPlanThresholdTotalAmountPrice::ConversionRateConfig, Models::NewPlanTierWithProrationPrice::ConversionRateConfig, Models::NewPlanTieredBPSPrice::ConversionRateConfig, Models::NewPlanTieredPackagePrice::ConversionRateConfig, Models::NewPlanTieredPackageWithMinimumPrice::ConversionRateConfig, Models::NewPlanTieredPrice::ConversionRateConfig, Models::NewPlanTieredWithMinimumPrice::ConversionRateConfig, Models::NewPlanUnitPrice::ConversionRateConfig, Models::NewPlanUnitWithPercentPrice::ConversionRateConfig, Models::NewPlanUnitWithProrationPrice::ConversionRateConfig, Models::NewSubscriptionBPSPrice::ConversionRateConfig, Models::NewSubscriptionBulkBPSPrice::ConversionRateConfig, Models::NewSubscriptionBulkPrice::ConversionRateConfig, Models::NewSubscriptionBulkWithProrationPrice::ConversionRateConfig, Models::NewSubscriptionCumulativeGroupedBulkPrice::ConversionRateConfig, Models::NewSubscriptionGroupedAllocationPrice::ConversionRateConfig, Models::NewSubscriptionGroupedTieredPackagePrice::ConversionRateConfig, Models::NewSubscriptionGroupedTieredPrice::ConversionRateConfig, Models::NewSubscriptionGroupedWithMeteredMinimumPrice::ConversionRateConfig, Models::NewSubscriptionGroupedWithProratedMinimumPrice::ConversionRateConfig, Models::NewSubscriptionMatrixPrice::ConversionRateConfig, Models::NewSubscriptionMatrixWithAllocationPrice::ConversionRateConfig, Models::NewSubscriptionMatrixWithDisplayNamePrice::ConversionRateConfig, Models::NewSubscriptionMaxGroupTieredPackagePrice::ConversionRateConfig, Models::NewSubscriptionPackagePrice::ConversionRateConfig, Models::NewSubscriptionPackageWithAllocationPrice::ConversionRateConfig, Models::NewSubscriptionScalableMatrixWithTieredPricingPrice::ConversionRateConfig, Models::NewSubscriptionScalableMatrixWithUnitPricingPrice::ConversionRateConfig, Models::NewSubscriptionThresholdTotalAmountPrice::ConversionRateConfig, Models::NewSubscriptionTierWithProrationPrice::ConversionRateConfig, Models::NewSubscriptionTieredBPSPrice::ConversionRateConfig, Models::NewSubscriptionTieredPackagePrice::ConversionRateConfig, Models::NewSubscriptionTieredPackageWithMinimumPrice::ConversionRateConfig, Models::NewSubscriptionTieredPrice::ConversionRateConfig, Models::NewSubscriptionTieredWithMinimumPrice::ConversionRateConfig, Models::NewSubscriptionUnitPrice::ConversionRateConfig, Models::NewSubscriptionUnitWithPercentPrice::ConversionRateConfig, Models::NewSubscriptionUnitWithProrationPrice::ConversionRateConfig, Models::Plan::Adjustment, Models::PlanCreateParams::Adjustment::Adjustment, Models::PlanCreateParams::Price::Price, Models::PlanVersion::Adjustment, Models::Price, Models::Price::BPS::ConversionRateConfig, Models::Price::Bulk::ConversionRateConfig, Models::Price::BulkBPS::ConversionRateConfig, Models::Price::BulkWithProration::ConversionRateConfig, Models::Price::CumulativeGroupedBulk::ConversionRateConfig, Models::Price::GroupedAllocation::ConversionRateConfig, Models::Price::GroupedTiered::ConversionRateConfig, Models::Price::GroupedTieredPackage::ConversionRateConfig, Models::Price::GroupedWithMeteredMinimum::ConversionRateConfig, Models::Price::GroupedWithMinMaxThresholds::ConversionRateConfig, Models::Price::GroupedWithProratedMinimum::ConversionRateConfig, Models::Price::Matrix::ConversionRateConfig, Models::Price::MatrixWithAllocation::ConversionRateConfig, Models::Price::MatrixWithDisplayName::ConversionRateConfig, Models::Price::MaxGroupTieredPackage::ConversionRateConfig, Models::Price::Package::ConversionRateConfig, Models::Price::PackageWithAllocation::ConversionRateConfig, Models::Price::ScalableMatrixWithTieredPricing::ConversionRateConfig, Models::Price::ScalableMatrixWithUnitPricing::ConversionRateConfig, Models::Price::ThresholdTotalAmount::ConversionRateConfig, Models::Price::Tiered::ConversionRateConfig, Models::Price::TieredBPS::ConversionRateConfig, Models::Price::TieredPackage::ConversionRateConfig, Models::Price::TieredPackageWithMinimum::ConversionRateConfig, Models::Price::TieredWithMinimum::ConversionRateConfig, Models::Price::TieredWithProration::ConversionRateConfig, Models::Price::Unit::ConversionRateConfig, Models::Price::UnitWithPercent::ConversionRateConfig, Models::Price::UnitWithProration::ConversionRateConfig, Models::PriceCreateParams::ConversionRateConfig, Models::PriceEvaluateMultipleParams::PriceEvaluation::Price, Models::PriceEvaluatePreviewEventsParams::PriceEvaluation::Price, Models::Subscription::DiscountInterval, Models::SubscriptionCreateParams::AddAdjustment::Adjustment, Models::SubscriptionCreateParams::AddPrice::Price, Models::SubscriptionCreateParams::ReplaceAdjustment::Adjustment, Models::SubscriptionCreateParams::ReplacePrice::Price, Models::SubscriptionPriceIntervalsParams::Add::Discount, Models::SubscriptionPriceIntervalsParams::Add::EndDate, Models::SubscriptionPriceIntervalsParams::Add::Price, Models::SubscriptionPriceIntervalsParams::Add::StartDate, Models::SubscriptionPriceIntervalsParams::AddAdjustment::Adjustment, Models::SubscriptionPriceIntervalsParams::AddAdjustment::EndDate, Models::SubscriptionPriceIntervalsParams::AddAdjustment::StartDate, Models::SubscriptionPriceIntervalsParams::Edit::EndDate, Models::SubscriptionPriceIntervalsParams::Edit::StartDate, Models::SubscriptionPriceIntervalsParams::EditAdjustment::EndDate, Models::SubscriptionPriceIntervalsParams::EditAdjustment::StartDate, Models::SubscriptionSchedulePlanChangeParams::AddAdjustment::Adjustment, Models::SubscriptionSchedulePlanChangeParams::AddPrice::Price, Models::SubscriptionSchedulePlanChangeParams::ReplaceAdjustment::Adjustment, Models::SubscriptionSchedulePlanChangeParams::ReplacePrice::Price, Models::SubscriptionUpdateTrialParams::TrialEndDate, Models::SubscriptionUsage
Defined in:
lib/orb/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:

# `discount` is a `Orb::Discount`
case discount
when Orb::PercentageDiscount
  puts(discount.discount_type)
when Orb::TrialDiscount
  puts(discount.applies_to_price_ids)
when Orb::UsageDiscount
  puts(discount.usage_discount)
else
  puts(discount)
end
case discount
in {
  discount_type: :percentage,
  percentage_discount: percentage_discount,
  applies_to_price_ids: applies_to_price_ids,
  filters: filters
}
  puts(percentage_discount)
in {discount_type: :trial, applies_to_price_ids: applies_to_price_ids, filters: filters, reason: reason}
  puts(applies_to_price_ids)
in {
  discount_type: :usage,
  usage_discount: usage_discount,
  applies_to_price_ids: applies_to_price_ids,
  filters: filters
}
  puts(usage_discount)
else
  puts(discount)
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:



144
145
146
# File 'lib/orb/internal/type/union.rb', line 144

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

#===(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



133
134
135
136
137
# File 'lib/orb/internal/type/union.rb', line 133

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)


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
203
204
205
206
207
208
209
210
211
212
# File 'lib/orb/internal/type/union.rb', line 175

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


223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/orb/internal/type/union.rb', line 223

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

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

  super
end

#hashInteger

Returns:

  • (Integer)


151
# File 'lib/orb/internal/type/union.rb', line 151

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)


259
260
261
262
263
264
265
266
267
268
# File 'lib/orb/internal/type/union.rb', line 259

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

  members = variants.map { Orb::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)


239
240
241
242
243
244
245
246
247
248
249
# File 'lib/orb/internal/type/union.rb', line 239

def to_sorbet_type
  types = variants.map { Orb::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>)


63
# File 'lib/orb/internal/type/union.rb', line 63

def variants = derefed_variants.map { _2 }