Sha256: 6ca6bc0783ae7ab94adcc5e0826daf53ac9eabc46f9ab97c3f3cf1c28a463c71

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

# typed: strict

require "singleton"

module Typed
  module Coercion
    class CoercerRegistry
      extend T::Sig

      include Singleton

      Registry = T.type_alias { T::Array[T.class_of(Coercer)] }

      DEFAULT_COERCERS = T.let(
        [
          StringCoercer,
          SymbolCoercer,
          BooleanCoercer,
          IntegerCoercer,
          FloatCoercer,
          DateCoercer,
          DateTimeCoercer,
          EnumCoercer,
          StructCoercer,
          TypedArrayCoercer,
          TypedHashCoercer
        ],
        Registry
      )

      sig { void }
      def initialize
        @available = T.let(DEFAULT_COERCERS.clone, Registry)
      end

      sig { params(coercer: T.class_of(Coercer)).void }
      def register(coercer)
        @available.prepend(coercer)
      end

      sig { void }
      def reset!
        @available = DEFAULT_COERCERS.clone
      end

      sig { params(type: T::Types::Base).returns(T.nilable(T.class_of(Coercer))) }
      def select_coercer_by(type:)
        @available.find { |coercer| coercer.new.used_for_type?(type) }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sorbet-schema-0.9.2 lib/typed/coercion/coercer_registry.rb
sorbet-schema-0.9.1 lib/typed/coercion/coercer_registry.rb
sorbet-schema-0.9.0 lib/typed/coercion/coercer_registry.rb
sorbet-schema-0.8.0 lib/typed/coercion/coercer_registry.rb
sorbet-schema-0.7.2 lib/typed/coercion/coercer_registry.rb