Module: Anthropic::Internal::Type::Enum Private

Includes:
Converter, Util::SorbetRuntimeSupport
Included in:
Helpers::InputSchema::EnumOf, Models::Base64ImageSource::MediaType, Models::Beta::BetaBase64ImageSource::MediaType, Models::Beta::BetaBashCodeExecutionToolResultError::ErrorCode, Models::Beta::BetaBashCodeExecutionToolResultErrorParam::ErrorCode, Models::Beta::BetaCacheControlEphemeral::TTL, Models::Beta::BetaCodeExecutionToolResultErrorCode, Models::Beta::BetaMessageParam::Role, Models::Beta::BetaServerToolUseBlock::Name, Models::Beta::BetaServerToolUseBlockParam::Name, Models::Beta::BetaStopReason, Models::Beta::BetaTextEditorCodeExecutionToolResultError::ErrorCode, Models::Beta::BetaTextEditorCodeExecutionToolResultErrorParam::ErrorCode, Models::Beta::BetaTextEditorCodeExecutionViewResultBlock::FileType, Models::Beta::BetaTextEditorCodeExecutionViewResultBlockParam::FileType, Models::Beta::BetaTool::Type, Models::Beta::BetaUsage::ServiceTier, Models::Beta::BetaWebFetchToolResultErrorCode, Models::Beta::BetaWebSearchToolResultErrorCode, Models::Beta::DeletedFile::Type, Models::Beta::MessageCreateParams::ServiceTier, Models::Beta::Messages::BatchCreateParams::Request::Params::ServiceTier, Models::Beta::Messages::BetaMessageBatch::ProcessingStatus, Models::CacheControlEphemeral::TTL, Models::MessageCreateParams::ServiceTier, Models::MessageParam::Role, Models::Messages::BatchCreateParams::Request::Params::ServiceTier, Models::Messages::MessageBatch::ProcessingStatus, Models::StopReason, Models::Tool::Type, Models::Usage::ServiceTier, Models::WebSearchToolRequestError::ErrorCode, Models::WebSearchToolResultError::ErrorCode
Defined in:
lib/anthropic/internal/type/enum.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.

A value from among a specified list of options. OpenAPI enum values map to Ruby values in the SDK as follows:

  1. boolean => true | false
  2. integer => Integer
  3. float => Float
  4. string => Symbol

We can therefore convert string values to Symbols, but can't convert other values safely.

Examples:

# `stop_reason` is a `Anthropic::StopReason`
case stop_reason
when Anthropic::StopReason::END_TURN
  # ...
when Anthropic::StopReason::MAX_TOKENS
  # ...
when Anthropic::StopReason::STOP_SEQUENCE
  # ...
else
  puts(stop_reason)
end
case stop_reason
in :end_turn
  # ...
in :max_tokens
  # ...
in :stop_sequence
  # ...
else
  puts(stop_reason)
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:



64
65
66
67
68
# File 'lib/anthropic/internal/type/enum.rb', line 64

def ==(other)
  # rubocop:disable Style/CaseEquality
  Anthropic::Internal::Type::Enum === other && other.values.to_set == values.to_set
  # rubocop:enable Style/CaseEquality
end

#===(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



57
# File 'lib/anthropic/internal/type/enum.rb', line 57

def ===(other) = values.include?(other)

#coerce(value, state:) ⇒ Symbol, 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.

Unlike with primitives, Enum additionally validates that the value is a member of the enum.

Parameters:

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

    .

    @option state [Boolean] :translate_names

    @option state [Boolean] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Class] :error

    @option state [Integer] :branched

Returns:

  • (Symbol, Object)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/anthropic/internal/type/enum.rb', line 95

def coerce(value, state:)
  exactness = state.fetch(:exactness)
  val = value.is_a?(String) ? value.to_sym : value

  if values.include?(val)
    exactness[:yes] += 1
    val
  elsif values.first&.class == val.class
    exactness[:maybe] += 1
    value
  else
    exactness[:no] += 1
    state[:error] = TypeError.new("#{value.class} can't be coerced into #{self}")
    value
  end
end

#dump(value, state: ) ⇒ Symbol, 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 (Symbol, Object)
  • state (Hash{Symbol=>Object}) (defaults to: )

    .

    @option state [Boolean] :can_retry

Returns:

  • (Symbol, Object)


# File 'lib/anthropic/internal/type/enum.rb', line 112


#hashInteger

Returns:

  • (Integer)


73
# File 'lib/anthropic/internal/type/enum.rb', line 73

def hash = values.to_set.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)


143
144
145
146
147
148
149
150
151
152
# File 'lib/anthropic/internal/type/enum.rb', line 143

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

  members = values.map { Anthropic::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)


126
127
128
129
130
131
132
133
134
135
136
# File 'lib/anthropic/internal/type/enum.rb', line 126

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

#valuesArray<NilClass, Boolean, Integer, Float, Symbol>

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 valid Symbol values for this enum.

Returns:

  • (Array<NilClass, Boolean, Integer, Float, Symbol>)


50
# File 'lib/anthropic/internal/type/enum.rb', line 50

def values = constants.map { const_get(_1) }