Sha256: 331aaf42c5305a392bcef0d52810d4b67ffa4cbe486d940f64626707f5a76a7d

Contents?: true

Size: 1010 Bytes

Versions: 1

Compression:

Stored size: 1010 Bytes

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,
          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: Field::Type).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

1 entries across 1 versions & 1 rubygems

Version Path
sorbet-schema-0.4.0 lib/typed/coercion/coercer_registry.rb