Sha256: e961da8dc3810f88a2add4f182c910b76d195eb8c473a184f0548c2ca8637f63

Contents?: true

Size: 843 Bytes

Versions: 11

Compression:

Stored size: 843 Bytes

Contents

# typed: strict

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

      Target = type_member { {fixed: T::Boolean} }

      sig { override.params(type: T::Types::Base).returns(T::Boolean) }
      def used_for_type?(type)
        type == T::Utils.coerce(T::Boolean)
      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 T::Boolean.")) unless used_for_type?(type)

        if type.recursively_valid?(value)
          Success.new(value)
        elsif value == "true"
          Success.new(true)
        elsif value == "false"
          Success.new(false)
        else
          Failure.new(CoercionError.new)
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
sorbet-schema-0.9.2 lib/typed/coercion/boolean_coercer.rb
sorbet-schema-0.9.1 lib/typed/coercion/boolean_coercer.rb
sorbet-schema-0.9.0 lib/typed/coercion/boolean_coercer.rb
sorbet-schema-0.8.0 lib/typed/coercion/boolean_coercer.rb
sorbet-schema-0.7.2 lib/typed/coercion/boolean_coercer.rb
sorbet-schema-0.7.1 lib/typed/coercion/boolean_coercer.rb
sorbet-schema-0.7.0 lib/typed/coercion/boolean_coercer.rb
sorbet-schema-0.6.0 lib/typed/coercion/boolean_coercer.rb
sorbet-schema-0.5.1 lib/typed/coercion/boolean_coercer.rb
sorbet-schema-0.4.2 lib/typed/coercion/boolean_coercer.rb
sorbet-schema-0.4.1 lib/typed/coercion/boolean_coercer.rb