Sha256: 41ae182b253e220f3341f0bf1256f291dcbd243c6928c228617c31902c00491f

Contents?: true

Size: 797 Bytes

Versions: 8

Compression:

Stored size: 797 Bytes

Contents

# typed: strict

module Typed
  module Coercion
    class SymbolCoercer < Coercer
      extend T::Generic

      Target = type_member { {fixed: Symbol} }

      sig { override.params(type: T::Types::Base).returns(T::Boolean) }
      def used_for_type?(type)
        type == T::Utils.coerce(Symbol)
      end

      sig { override.params(type: T::Types::Base, value: Value).returns(Result[Target, CoercionError]) }
      def coerce(type:, value:)
        return Failure.new(CoercionError.new("Type must be a Symbol.")) unless used_for_type?(type)

        if value.respond_to?(:to_sym)
          Success.new(value.to_sym)
        else
          Failure.new(CoercionError.new("Value cannot be coerced into Symbol. Consider adding a #to_sym implementation."))
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
sorbet-schema-0.9.2 lib/typed/coercion/symbol_coercer.rb
sorbet-schema-0.9.1 lib/typed/coercion/symbol_coercer.rb
sorbet-schema-0.9.0 lib/typed/coercion/symbol_coercer.rb
sorbet-schema-0.8.0 lib/typed/coercion/symbol_coercer.rb
sorbet-schema-0.7.2 lib/typed/coercion/symbol_coercer.rb
sorbet-schema-0.7.1 lib/typed/coercion/symbol_coercer.rb
sorbet-schema-0.7.0 lib/typed/coercion/symbol_coercer.rb
sorbet-schema-0.6.0 lib/typed/coercion/symbol_coercer.rb