Sha256: 5085ee4819de385c1e34d68e905cbd8d94852671e70de66f20236cbff3abe797

Contents?: true

Size: 1.94 KB

Versions: 2

Compression:

Stored size: 1.94 KB

Contents

# typed: strict
# frozen_string_literal: true

begin
  require "active_support/core_ext/class/attribute"
rescue LoadError
  return
end

module Tapioca
  module Dsl
    module Compilers
      # `Tapioca::Dsl::Compilers::MixedInClassAttributes` generates RBI files for modules that dynamically use
      # `class_attribute` on classes.
      #
      # For example, given the following concern
      #
      # ~~~rb
      # module Taggeable
      #   extend ActiveSupport::Concern
      #
      #   included do
      #     class_attribute :tag
      #   end
      # end
      # ~~~
      #
      # this compiler will produce the RBI file `taggeable.rbi` with the following content:
      #
      # ~~~rbi
      # # typed: strong
      #
      # module Taggeable
      #   include GeneratedInstanceMethods
      #
      #   mixes_in_class_methods GeneratedClassMethods
      #
      #   module GeneratedClassMethods
      #     def tag; end
      #     def tag=(value); end
      #     def tag?; end
      #   end
      #
      #   module GeneratedInstanceMethods
      #     def tag; end
      #     def tag=(value); end
      #     def tag?; end
      #   end
      # end
      # ~~~
      class MixedInClassAttributes < Compiler
        extend T::Sig

        ConstantType = type_member(fixed: Module)

        sig { override.void }
        def decorate
          mixin_compiler = Runtime::DynamicMixinCompiler.new(constant)
          return if mixin_compiler.empty_attributes?

          root.create_path(constant) do |mod|
            mixin_compiler.compile_class_attributes(mod)
          end
        end

        sig { override.returns(T::Enumerable[Module]) }
        def self.gather_constants
          # Select all non-anonymous modules that have overridden Module.included
          all_modules.select do |mod|
            !mod.is_a?(Class) && name_of(mod) && Runtime::Reflection.method_of(mod, :included).owner != Module
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tapioca-0.7.1 lib/tapioca/dsl/compilers/mixed_in_class_attributes.rb
tapioca-0.7.0 lib/tapioca/dsl/compilers/mixed_in_class_attributes.rb