Module: Whop_sdk::Internal::Types::Union
- Includes:
- Type
- Included in:
- AdGroups::Types::CreateAdGroupsRequestPlacements, AdGroups::Types::UpdateAdGroupsRequestPlacements, Affiliates::Overrides::Types::CreateOverridesRequestBody, Audiences::Types::CreateAudiencesResponse, CompanyTokenTransactions::Types::CreateCompanyTokenTransactionsRequest, Experiments::Types::ExposuresExperimentsResponse, Boolean, Whop_sdk::Invoices::Types::CreateInvoicesRequest, Memberships::Types::InviteMembershipsRequestBody, Partners::Businesses::Earnings::Types::ListEarningsResponseDataItemResource, SetupIntents::Types::CreateSetupIntentsRequest, Swaps::Types::CreateQuoteSwapsRequestFromChain, Swaps::Types::CreateQuoteSwapsRequestToChain, Swaps::Types::CreateSwapsRequestFromChain, Swaps::Types::CreateSwapsRequestToChain, Transfers::Types::CreateTransfersResponse, Transfers::Types::CreateTransfersResponseTransferDestination, Transfers::Types::CreateTransfersResponseTransferOrigin, Transfers::Types::ListRecipientsTransfersResponseDataItem, Transfers::Types::PostTransferCompletedPayloadDataDestination, Transfers::Types::PostTransferCompletedPayloadDataOrigin, Transfers::Types::PostTransferCreatedPayloadDataDestination, Transfers::Types::PostTransferCreatedPayloadDataOrigin, Transfers::Types::PostTransferFailedPayloadDataDestination, Transfers::Types::PostTransferFailedPayloadDataOrigin, Transfers::Types::RetrieveTransfersResponseDestination, Transfers::Types::RetrieveTransfersResponseOrigin, Types::AdGroupGeoLocationsBodyZipsItem, Types::AudienceEngagementRule, Types::ConversionEvent, Types::LedgerAccountOwner, Types::LedgerActivityAccount, Types::LedgerActivityResource, Types::LedgerActivityResourceOwnerOwner, Types::ListAPIKeysRequestCreatedAfter, Types::ListAPIKeysRequestCreatedBefore, Types::ListAppBuildsRequestCreatedAfter, Types::ListAppBuildsRequestCreatedBefore, Types::PaymentInstructions, Types::PaymentMethod, Types::PaymentMethodListItem, Types::PaymentNextAction, Types::PaymentRuleConditionValue, Types::TargetingOption, Verifications::Types::CreateVerificationsRequestBody, Verifications::Types::UpdateVerificationsRequestBody
- Defined in:
- lib/whop_sdk/internal/types/union.rb
Overview
Define a union between two types
Instance Method Summary collapse
- #coerce(value, strict: strict?) ) ⇒ Object
-
#discriminant(key) ⇒ void
Set the discriminant for this union.
-
#load(str) ⇒ Object
Parse JSON string and coerce to the correct union member type.
-
#member(type, key: nil) ⇒ void
Add a member to this union.
- #members ⇒ Object
- #type_member?(type) ⇒ Boolean
Methods included from Type
Methods included from JSON::Serializable
Instance Method Details
#coerce(value, strict: strict?) ) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/whop_sdk/internal/types/union.rb', line 122 def coerce(value, strict: strict?) type = resolve_member(value) unless type return value unless strict if discriminated? raise Errors::TypeError, "value of type `#{value.class}` not member of union #{self}" end raise Errors::TypeError, "could not resolve to member of union #{self}" end coerced = Utils.coerce(type, value, strict: strict) # For discriminated unions, store the discriminant info on the coerced instance # so it can be injected back during serialization (to_h) if discriminated? && value.is_a?(::Hash) && coerced.is_a?(Model) discriminant_value = value.fetch(@discriminant, nil) || value.fetch(@discriminant.to_s, nil) if discriminant_value coerced.instance_variable_set(:@_fern_union_discriminant_key, @discriminant.to_s) coerced.instance_variable_set(:@_fern_union_discriminant_value, discriminant_value) end end coerced end |
#discriminant(key) ⇒ void
This method returns an undefined value.
Set the discriminant for this union
32 33 34 |
# File 'lib/whop_sdk/internal/types/union.rb', line 32 def discriminant(key) @discriminant = key end |
#load(str) ⇒ Object
Parse JSON string and coerce to the correct union member type
155 156 157 |
# File 'lib/whop_sdk/internal/types/union.rb', line 155 def load(str) coerce(::JSON.parse(str, symbolize_names: true)) end |
#member(type, key: nil) ⇒ void
This method returns an undefined value.
Add a member to this union
19 20 21 22 |
# File 'lib/whop_sdk/internal/types/union.rb', line 19 def member(type, key: nil) members.push([key, Utils.wrap_type(type)]) self end |
#members ⇒ Object
10 11 12 |
# File 'lib/whop_sdk/internal/types/union.rb', line 10 def members @members ||= [] end |
#type_member?(type) ⇒ Boolean
24 25 26 |
# File 'lib/whop_sdk/internal/types/union.rb', line 24 def type_member?(type) members.any? { |_key, type_fn| type == type_fn.call } end |