class Anthropic::Internal::Type::HashOf

Hash of items of a given type.
@generic Elem
@abstract
@api private

def self.[](type_info, spec = {}) = new(type_info, spec)

Options Hash: (**spec)
  • :"nil?" (Boolean) --
  • :union (Proc) --
  • :enum (Proc) --
  • :const (NilClass, TrueClass, FalseClass, Integer, Float, Symbol) --

Parameters:
  • spec (Hash{Symbol=>Object}) -- .
  • type_info (Hash{Symbol=>Object}, Proc, Anthropic::Internal::Type::Converter, Class) --
def self.[](type_info, spec = {}) = new(type_info, spec)

def ==(other)

Returns:
  • (Boolean) -

Parameters:
  • other (Object) --
def ==(other)
  # rubocop:disable Layout/LineLength
  other.is_a?(Anthropic::Internal::Type::HashOf) && other.nilable? == nilable? && other.item_type == item_type
  # rubocop:enable Layout/LineLength
end

def ===(other)

Returns:
  • (Boolean) -

Parameters:
  • other (Object) --
def ===(other)
  type = item_type
  case other
  in Hash
    other.all? do |key, val|
      case [key, val]
      in [Symbol | String, ^type]
        true
      else
        false
      end
    end
  else
    false
  end
end

def coerce(value, state:)

Returns:
  • (Hash{Symbol=>Object}, Object) -

Options Hash: (**state)
  • :branched (Integer) --
  • :exactness (Hash{Symbol=>Object}) --
  • :strictness (Boolean, :strong) --

Parameters:
  • state (Hash{Symbol=>Object}) -- .
  • value (Hash{Object=>Object}, Object) --

Other tags:
    Api: - private
def coerce(value, state:)
  exactness = state.fetch(:exactness)
  unless value.is_a?(Hash)
    exactness[:no] += 1
    return value
  end
  target = item_type
  exactness[:yes] += 1
  value
    .to_h do |key, val|
      k = key.is_a?(String) ? key.to_sym : key
      v =
        case [nilable?, val]
        in [true, nil]
          exactness[:yes] += 1
          nil
        else
          Anthropic::Internal::Type::Converter.coerce(target, val, state: state)
        end
      exactness[:no] += 1 unless k.is_a?(Symbol)
      [k, v]
    end
end

def dump(value, state:)

Returns:
  • (Hash{Symbol=>Object}, Object) -

Options Hash: (**state)
  • :can_retry (Boolean) --

Parameters:
  • state (Hash{Symbol=>Object}) -- .
  • value (Hash{Object=>Object}, Object) --

Other tags:
    Api: - private
def dump(value, state:)
  target = item_type
  if value.is_a?(Hash)
    value.transform_values do
      Anthropic::Internal::Type::Converter.dump(target, _1, state: state)
    end
  else
    super
  end
end

def initialize(type_info, spec = {})

Options Hash: (**spec)
  • :"nil?" (Boolean) --
  • :union (Proc) --
  • :enum (Proc) --
  • :const (NilClass, TrueClass, FalseClass, Integer, Float, Symbol) --

Parameters:
  • spec (Hash{Symbol=>Object}) -- .
  • type_info (Hash{Symbol=>Object}, Proc, Anthropic::Internal::Type::Converter, Class) --

Other tags:
    Api: - private
def initialize(type_info, spec = {})
  @item_type_fn = Anthropic::Internal::Type::Converter.type_info(type_info || spec)
  @nilable = spec[:nil?]
end

def item_type = @item_type_fn.call

Returns:
  • (generic) -

Other tags:
    Api: - private
def item_type = @item_type_fn.call

def nilable? = @nilable

Returns:
  • (Boolean) -

Other tags:
    Api: - private
def nilable? = @nilable