Sha256: 61076b0dc2e6f525d4676c023589382155a50ef56ff0466ec6b960aed15ade09

Contents?: true

Size: 1.67 KB

Versions: 10

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module Orthoses
  class Mixin
    module Hook
      def include(*modules)
        super
      end
      def extend(*modules)
        super
      end
      def prepend(*modules)
        super
      end
    end

    def initialize(loader, if: nil)
      @loader = loader
      @if = binding.local_variable_get(:if)
    end

    def call
      ::Module.prepend(Hook)

      include = CallTracer.new
      extend = CallTracer.new
      prepend = CallTracer.new

      store = include.trace(Hook.instance_method(:include)) do
        extend.trace(Hook.instance_method(:extend)) do
          prepend.trace(Hook.instance_method(:prepend)) do
            @loader.call
          end
        end
      end

      collect_definitions(store, include, :include)
      collect_definitions(store, extend, :extend)
      collect_definitions(store, prepend, :prepend)

      store
    end

    private

    def collect_definitions(store, call_tracer, how)
      call_tracer.captures.each do |capture|
        base_mod = capture.method.receiver
        next unless base_mod.kind_of?(Module)

        base_mod_name = Utils.module_name(base_mod)
        next unless base_mod_name

        content = store[base_mod_name]
        capture.argument[:modules].each do |mod|
          mod_name = Utils.module_name(mod)
          next unless mod_name

          next if mod_name.start_with?("Orthoses")

          known_type_params = Utils.known_type_params(mod)
          next unless known_type_params.nil? || known_type_params.empty?

          next unless @if.nil? || @if.call(base_mod, how, mod)

          store[mod_name]
          content << "#{how} #{mod_name}"
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
orthoses-1.7.0 lib/orthoses/mixin.rb
orthoses-1.6.0 lib/orthoses/mixin.rb
orthoses-1.5.0 lib/orthoses/mixin.rb
orthoses-1.4.0 lib/orthoses/mixin.rb
orthoses-1.3.0 lib/orthoses/mixin.rb
orthoses-1.2.0 lib/orthoses/mixin.rb
orthoses-1.1.0 lib/orthoses/mixin.rb
orthoses-1.0.0 lib/orthoses/mixin.rb
orthoses-0.11.0 lib/orthoses/mixin.rb
orthoses-0.10.0 lib/orthoses/mixin.rb