Sha256: a9fea0f79ebedd0fe7566baf1d29c73bb1a9e7695b97bc729ea7373b93ab91d9

Contents?: true

Size: 1.66 KB

Versions: 24

Compression:

Stored size: 1.66 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, **kwargs)
        registration = find_registration(symbol, *args, **kwargs)

        if registration
          registration.call(self, symbol, *args, **kwargs)
        else
          raise ArgumentError, "Unknown type #{symbol.inspect}"
        end
      end

      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, **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

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

Version data entries

24 entries across 24 versions & 5 rubygems

Version Path
activemodel-6.1.4.7 lib/active_model/type/registry.rb
activemodel-6.1.4.6 lib/active_model/type/registry.rb
activemodel-6.1.4.5 lib/active_model/type/registry.rb
ric-0.14.2 vendor/bundle/ruby/2.7.0/gems/activemodel-6.1.4.4/lib/active_model/type/registry.rb
ric-0.14.1 vendor/bundle/ruby/2.7.0/gems/activemodel-6.1.4.4/lib/active_model/type/registry.rb
ric-0.14.0 vendor/bundle/ruby/2.7.0/gems/activemodel-6.1.4.4/lib/active_model/type/registry.rb
activemodel-6.1.4.4 lib/active_model/type/registry.rb
activemodel-6.1.4.3 lib/active_model/type/registry.rb
activemodel-6.1.4.2 lib/active_model/type/registry.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activemodel-6.1.4.1/lib/active_model/type/registry.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activemodel-6.1.4.1/lib/active_model/type/registry.rb
activemodel-6.1.4.1 lib/active_model/type/registry.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/activemodel-6.1.4/lib/active_model/type/registry.rb
activemodel-6.1.4 lib/active_model/type/registry.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/activemodel-6.1.3.1/lib/active_model/type/registry.rb
activemodel-6.1.3.2 lib/active_model/type/registry.rb
activemodel-6.1.3.1 lib/active_model/type/registry.rb
activemodel-6.1.3 lib/active_model/type/registry.rb
activemodel-6.1.2.1 lib/active_model/type/registry.rb
activemodel-6.1.2 lib/active_model/type/registry.rb