Sha256: c409e2856e082274083b9d2aae21a9ab0aab9fc16321b82cf7255b76c950e90c

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 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,
          BooleanCoercer,
          IntegerCoercer,
          FloatCoercer,
          DateCoercer,
          EnumCoercer,
          StructCoercer,
          TypedArrayCoercer
        ],
        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

3 entries across 3 versions & 1 rubygems

Version Path
sorbet-schema-0.5.1 lib/typed/coercion/coercer_registry.rb
sorbet-schema-0.4.2 lib/typed/coercion/coercer_registry.rb
sorbet-schema-0.4.1 lib/typed/coercion/coercer_registry.rb