Class: DynamicMixinCompiler

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Tapioca::Reflection
Defined in:
lib/tapioca/compilers/dynamic_mixin_compiler.rb

Overview

typed: strict frozen_string_literal: true

Constant Summary

Constants included from Tapioca::Reflection

Tapioca::Reflection::ANCESTORS_METHOD, Tapioca::Reflection::CLASS_METHOD, Tapioca::Reflection::CONSTANTS_METHOD, Tapioca::Reflection::EQUAL_METHOD, Tapioca::Reflection::METHOD_METHOD, Tapioca::Reflection::NAME_METHOD, Tapioca::Reflection::OBJECT_ID_METHOD, Tapioca::Reflection::PRIVATE_INSTANCE_METHODS_METHOD, Tapioca::Reflection::PROTECTED_INSTANCE_METHODS_METHOD, Tapioca::Reflection::PUBLIC_INSTANCE_METHODS_METHOD, Tapioca::Reflection::SINGLETON_CLASS_METHOD, Tapioca::Reflection::SUPERCLASS_METHOD

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tapioca::Reflection

#ancestors_of, #are_equal?, #class_of, #constants_of, #descendants_of, #inherited_ancestors_of, #method_of, #name_of, #name_of_type, #object_id_of, #private_instance_methods_of, #protected_instance_methods_of, #public_instance_methods_of, #qualified_name_of, #signature_of, #singleton_class_of, #superclass_of

Constructor Details

#initialize(constant) ⇒ DynamicMixinCompiler

Returns a new instance of DynamicMixinCompiler.


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
# File 'lib/tapioca/compilers/dynamic_mixin_compiler.rb', line 18

def initialize(constant)
  @constant = constant
  mixins_from_modules = {}.compare_by_identity
  class_attribute_readers = T.let([], T::Array[Symbol])
  class_attribute_writers = T.let([], T::Array[Symbol])
  class_attribute_predicates = T.let([], T::Array[Symbol])

  instance_attribute_readers = T.let([], T::Array[Symbol])
  instance_attribute_writers = T.let([], T::Array[Symbol])
  instance_attribute_predicates = T.let([], T::Array[Symbol])

  Class.new do
    # Override the `self.include` method
    define_singleton_method(:include) do |mod|
      # Take a snapshot of the list of singleton class ancestors
      # before the actual include
      before = singleton_class.ancestors
      # Call the actual `include` method with the supplied module
      super(mod).tap do
        # Take a snapshot of the list of singleton class ancestors
        # after the actual include
        after = singleton_class.ancestors
        # The difference is the modules that are added to the list
        # of ancestors of the singleton class. Those are all the
        # modules that were `extend`ed due to the `include` call.
        #
        # We record those modules on our lookup table keyed by
        # the included module with the values being all the modules
        # that that module pulls into the singleton class.
        #
        # We need to reverse the order, since the extend order should
        # be the inverse of the ancestor order. That is, earlier
        # extended modules would be later in the ancestor chain.
        mixins_from_modules[mod] = (after - before).reverse!
      end
    rescue Exception # rubocop:disable Lint/RescueException
      # this is a best effort, bail if we can't perform this
    end

    define_singleton_method(:class_attribute) do |*attrs, **kwargs|
      class_attribute_readers.concat(attrs)
      class_attribute_writers.concat(attrs)

      instance_predicate = kwargs.fetch(:instance_predicate, true)
      instance_accessor = kwargs.fetch(:instance_accessor, true)
      instance_reader = kwargs.fetch(:instance_reader, instance_accessor)
      instance_writer = kwargs.fetch(:instance_writer, instance_accessor)

      if instance_reader
        instance_attribute_readers.concat(attrs)
      end

      if instance_writer
        instance_attribute_writers.concat(attrs)
      end

      if instance_predicate
        class_attribute_predicates.concat(attrs)

        if instance_reader
          instance_attribute_predicates.concat(attrs)
        end
      end

      super(*attrs, **kwargs) if defined?(super)
    end

    # rubocop:disable Style/MissingRespondToMissing
    T::Sig::WithoutRuntime.sig { params(symbol: Symbol, args: T.untyped).returns(T.untyped) }
    def method_missing(symbol, *args)
      # We need this here so that we can handle any random instance
      # method calls on the fake including class that may be done by
      # the included module during the `self.included` hook.
    end

    class << self
      extend T::Sig

      T::Sig::WithoutRuntime.sig { params(symbol: Symbol, args: T.untyped).returns(T.untyped) }
      def method_missing(symbol, *args)
        # Similarly, we need this here so that we can handle any
        # random class method calls on the fake including class
        # that may be done by the included module during the
        # `self.included` hook.
      end
    end
    # rubocop:enable Style/MissingRespondToMissing
  end.include(constant)

  # The value that corresponds to the original included constant
  # is the list of all dynamically extended modules because of that
  # constant. We grab that value by deleting the key for the original
  # constant.
  @dynamic_extends = T.let(mixins_from_modules.delete(constant) || [], T::Array[Module])

  # Since we deleted the original constant from the list of keys, all
  # the keys that remain are the ones that are dynamically included modules
  # during the include of the original constant.
  @dynamic_includes = T.let(mixins_from_modules.keys, T::Array[Module])

  @class_attribute_readers = T.let(class_attribute_readers, T::Array[Symbol])
  @class_attribute_writers = T.let(class_attribute_writers, T::Array[Symbol])
  @class_attribute_predicates = T.let(class_attribute_predicates, T::Array[Symbol])

  @instance_attribute_readers = T.let(instance_attribute_readers, T::Array[Symbol])
  @instance_attribute_writers = T.let(instance_attribute_writers, T::Array[Symbol])
  @instance_attribute_predicates = T.let(instance_attribute_predicates, T::Array[Symbol])
end

Instance Attribute Details

#class_attribute_predicatesObject (readonly)

Returns the value of attribute class_attribute_predicates.


12
13
14
# File 'lib/tapioca/compilers/dynamic_mixin_compiler.rb', line 12

def class_attribute_predicates
  @class_attribute_predicates
end

#class_attribute_readersObject (readonly)

Returns the value of attribute class_attribute_readers.


12
13
14
# File 'lib/tapioca/compilers/dynamic_mixin_compiler.rb', line 12

def class_attribute_readers
  @class_attribute_readers
end

#class_attribute_writersObject (readonly)

Returns the value of attribute class_attribute_writers.


12
13
14
# File 'lib/tapioca/compilers/dynamic_mixin_compiler.rb', line 12

def class_attribute_writers
  @class_attribute_writers
end

#dynamic_extendsObject (readonly)

Returns the value of attribute dynamic_extends.


9
10
11
# File 'lib/tapioca/compilers/dynamic_mixin_compiler.rb', line 9

def dynamic_extends
  @dynamic_extends
end

#dynamic_includesObject (readonly)

Returns the value of attribute dynamic_includes.


9
10
11
# File 'lib/tapioca/compilers/dynamic_mixin_compiler.rb', line 9

def dynamic_includes
  @dynamic_includes
end

#instance_attribute_predicatesObject (readonly)

Returns the value of attribute instance_attribute_predicates.


15
16
17
# File 'lib/tapioca/compilers/dynamic_mixin_compiler.rb', line 15

def instance_attribute_predicates
  @instance_attribute_predicates
end

#instance_attribute_readersObject (readonly)

Returns the value of attribute instance_attribute_readers.


15
16
17
# File 'lib/tapioca/compilers/dynamic_mixin_compiler.rb', line 15

def instance_attribute_readers
  @instance_attribute_readers
end

#instance_attribute_writersObject (readonly)

Returns the value of attribute instance_attribute_writers.


15
16
17
# File 'lib/tapioca/compilers/dynamic_mixin_compiler.rb', line 15

def instance_attribute_writers
  @instance_attribute_writers
end

Instance Method Details

#compile_class_attributes(tree) ⇒ Object


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/tapioca/compilers/dynamic_mixin_compiler.rb', line 133

def compile_class_attributes(tree)
  return if empty_attributes?

  # Create a synthetic module to hold the generated class methods
  tree << RBI::Module.new("GeneratedClassMethods") do |mod|
    class_attribute_readers.each do |attribute|
      mod << RBI::Method.new(attribute.to_s)
    end

    class_attribute_writers.each do |attribute|
      mod << RBI::Method.new("#{attribute}=") do |method|
        method << RBI::Param.new("value")
      end
    end

    class_attribute_predicates.each do |attribute|
      mod << RBI::Method.new("#{attribute}?")
    end
  end

  # Create a synthetic module to hold the generated instance methods
  tree << RBI::Module.new("GeneratedInstanceMethods") do |mod|
    instance_attribute_readers.each do |attribute|
      mod << RBI::Method.new(attribute.to_s)
    end

    instance_attribute_writers.each do |attribute|
      mod << RBI::Method.new("#{attribute}=") do |method|
        method << RBI::Param.new("value")
      end
    end

    instance_attribute_predicates.each do |attribute|
      mod << RBI::Method.new("#{attribute}?")
    end
  end

  # Add a mixes_in_class_methods and include for the generated modules
  tree << RBI::MixesInClassMethods.new("GeneratedClassMethods")
  tree << RBI::Include.new("GeneratedInstanceMethods")
end

#compile_mixes_in_class_methods(tree) ⇒ Object


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/tapioca/compilers/dynamic_mixin_compiler.rb', line 176

def compile_mixes_in_class_methods(tree)
  includes = dynamic_includes.select { |mod| (name = name_of(mod)) && !name.start_with?("T::") }
  includes.each do |mod|
    qname = qualified_name_of(mod)
    tree << RBI::Include.new(T.must(qname))
  end

  # If we can generate multiple mixes_in_class_methods, then we want to use all dynamic extends that are not the
  # constant itself
  mixed_in_class_methods = dynamic_extends.select { |mod| mod != @constant }
  return [[], []] if mixed_in_class_methods.empty?

  mixed_in_class_methods.each do |mod|
    qualified_name = qualified_name_of(mod)
    next if qualified_name.nil? || qualified_name.empty?
    tree << RBI::MixesInClassMethods.new(qualified_name)
  end

  [mixed_in_class_methods, includes]
rescue
  [[], []] # silence errors
end

#empty_attributes?Boolean

Returns:

  • (Boolean)

128
129
130
# File 'lib/tapioca/compilers/dynamic_mixin_compiler.rb', line 128

def empty_attributes?
  @class_attribute_readers.empty? && @class_attribute_writers.empty?
end