Sha256: ecd36ce5659192f1e701e92d99b99745cee4a599db4843ecb0b36d7751615984

Contents?: true

Size: 1.38 KB

Versions: 38

Compression:

Stored size: 1.38 KB

Contents

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

      def register(type_name, klass = nil, **options, &block)
        block ||= proc { |_, *args| klass.new(*args) }
        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

      protected

      attr_reader :registrations

      private

      def registration_klass
        Registration
      end

      def find_registration(symbol, *args)
        registrations.find { |r| r.matches?(symbol, *args) }
      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, **kwargs)
        if kwargs.any? # https://bugs.ruby-lang.org/issues/10856
          block.call(*args, **kwargs)
        else
          block.call(*args)
        end
      end

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

      protected

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

Version data entries

38 entries across 37 versions & 5 rubygems

Version Path
activemodel-5.0.7.2 lib/active_model/type/registry.rb
activemodel-5.0.7.1 lib/active_model/type/registry.rb
activemodel-5.0.7 lib/active_model/type/registry.rb
activemodel-5.0.6 lib/active_model/type/registry.rb
activemodel-5.0.6.rc1 lib/active_model/type/registry.rb
activemodel-5.0.5 lib/active_model/type/registry.rb
activemodel-5.0.5.rc2 lib/active_model/type/registry.rb
activemodel-5.0.5.rc1 lib/active_model/type/registry.rb
tdiary-5.0.5 vendor/bundle/gems/activemodel-5.0.2/lib/active_model/type/registry.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/activemodel-5.0.2/lib/active_model/type/registry.rb
activemodel-5.0.4 lib/active_model/type/registry.rb
activemodel-5.0.4.rc1 lib/active_model/type/registry.rb
activemodel-5.0.3 lib/active_model/type/registry.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activemodel-5.0.2/lib/active_model/type/registry.rb
tdiary-5.0.4 vendor/bundle/gems/activemodel-5.0.2/lib/active_model/type/registry.rb
activemodel-5.0.2 lib/active_model/type/registry.rb
activemodel-5.0.2.rc1 lib/active_model/type/registry.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activemodel-5.0.1/lib/active_model/type/registry.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activemodel-5.0.1/lib/active_model/type/registry.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activemodel-5.0.1/lib/active_model/type/registry.rb