Sha256: 29b8ca4a0338e286d7b32d45da7e4b002a1d076e919b3d9b3969843a442ee890
Contents?: true
Size: 1.5 KB
Versions: 9
Compression:
Stored size: 1.5 KB
Contents
# frozen_string_literal: true 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) } block.ruby2_keywords if block.respond_to?(:ruby2_keywords) 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) 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 private attr_reader :name, :block end end # :startdoc: end
Version data entries
9 entries across 9 versions & 1 rubygems