aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/type/registry.rb
blob: 8070a4bd383489cc215d7b26ad4a10ea5f063921 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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 = registrations
          .select { |r| r.matches?(symbol, *args) }
          .max

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

      def add_modifier(options, klass, **args)
        registrations << decoration_registration_klass.new(options, klass, **args)
      end

      protected

      attr_reader :registrations
      
      private
      
      def registration_klass
        Registration
      end
      
      def decoration_registration_klass
        DecorationRegistration
      end
    end

    class Registration
      def initialize(name, block, override: nil)
        @name = name
        @block = block
        @override = override
      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# && matches_adapter?(**kwargs)
      end

      def <=>(other)
        # if conflicts_with?(other)
        #   raise TypeConflictError.new("Type #{name} was registered for all
        #                               adapters, but shadows a native type with
        #                               the same name for #{other.adapter}".squish)
        # end
        priority <=> other.priority
      end

      protected

      attr_reader :name, :block, :override

      def priority
        result = 0
        # if adapter
        #   result |= 1
        # end
        if override
          result |= 2
        end
        result
      end

      # def priority_except_adapter
      #   priority & 0b111111100
      # end

      private

      # def matches_adapter?(adapter: nil, **)
      #   (self.adapter.nil? || adapter == self.adapter)
      # end

      # def conflicts_with?(other)
      #   same_priority_except_adapter?(other) &&
      #     has_adapter_conflict?(other)
      # end

      # def same_priority_except_adapter?(other)
      #   priority_except_adapter == other.priority_except_adapter
      # end

      # def has_adapter_conflict?(other)
      #   (override.nil? && other.adapter) ||
      #     (adapter && other.override.nil?)
      # end
    end

    class DecorationRegistration < Registration
      def initialize(options, klass)
        @options = options
        @klass = klass
        # @adapter = adapter
      end

      def call(registry, *args, **kwargs)
        subtype = registry.lookup(*args, **kwargs.except(*options.keys))
        klass.new(subtype)
      end

      def matches?(*args, **kwargs)
        matches_options?(**kwargs)
      end

      def priority
        super | 4
      end

      protected

      attr_reader :options, :klass

      private

      def matches_options?(**kwargs)
        options.all? do |key, value|
          kwargs[key] == value
        end
      end
    end
  end

  class TypeConflictError < StandardError
  end

  # :startdoc:
end