Sha256: a22c5770d76c79fe748c461da2cc3f1bc3a4e6591282ce9744127e515704c878

Contents?: true

Size: 1.63 KB

Versions: 17

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module ActiveModel
  # :stopdoc:
  module Type
    class Registry
      def initialize
        @registrations = []
      end

      def initialize_dup(other)
        @registrations = @registrations.dup
        super
      end

      def register(type_name, klass = nil, **options, &block)
        unless block_given?
          block = proc { |_, *args| klass.new(*args) }
          block.ruby2_keywords if block.respond_to?(:ruby2_keywords)
        end
        registrations << registration_klass.new(type_name, block, **options)
      end

      def lookup(symbol, *args)
        registration = find_registration(symbol, *args)

        if registration
          registration.call(self, symbol, *args)
        else
          raise ArgumentError, "Unknown type #{symbol.inspect}"
        end
      end
      ruby2_keywords(:lookup) if respond_to?(:ruby2_keywords, true)

      private
        attr_reader :registrations

        def registration_klass
          Registration
        end

        def find_registration(symbol, *args, **kwargs)
          registrations.find { |r| r.matches?(symbol, *args, **kwargs) }
        end
    end

    class Registration
      # Options must be taken because of https://bugs.ruby-lang.org/issues/10856
      def initialize(name, block, **)
        @name = name
        @block = block
      end

      def call(_registry, *args)
        block.call(*args)
      end
      ruby2_keywords(:call) if respond_to?(:ruby2_keywords, true)

      def matches?(type_name, *args, **kwargs)
        type_name == name
      end

      private
        attr_reader :name, :block
    end
  end
  # :startdoc:
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
activemodel-6.1.7.10 lib/active_model/type/registry.rb
activemodel-6.1.7.9 lib/active_model/type/registry.rb
activemodel-6.1.7.8 lib/active_model/type/registry.rb
activemodel-6.1.7.7 lib/active_model/type/registry.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activemodel-6.1.6.1/lib/active_model/type/registry.rb
activemodel-6.1.7.6 lib/active_model/type/registry.rb
activemodel-6.1.7.5 lib/active_model/type/registry.rb
activemodel-6.1.7.4 lib/active_model/type/registry.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activemodel-6.1.6.1/lib/active_model/type/registry.rb
activemodel-6.1.7.3 lib/active_model/type/registry.rb
activemodel-6.1.7.2 lib/active_model/type/registry.rb
activemodel-6.1.7.1 lib/active_model/type/registry.rb
activemodel-6.1.7 lib/active_model/type/registry.rb
activemodel-6.1.6.1 lib/active_model/type/registry.rb
activemodel-6.1.6 lib/active_model/type/registry.rb
activemodel-6.1.5.1 lib/active_model/type/registry.rb
activemodel-6.1.5 lib/active_model/type/registry.rb