Sha256: 8c1e2f2325a832a61165955b380c26293e4204558fc24048b1c7d2876bbb9854

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

module Ahnnotate
  module Facet
    module Models
      class ResolveClassRelationships
        def to_proc
          method(:call).to_proc
        end

        def call(extracted_classes)
          object_space =
            extracted_classes
              .map(&method(:self_and_outer_class))
              .flatten
              .compact
              .uniq
              .map { |x| [x.class_name, x] }
              .to_h

          object_space[""] ||= ModuleNode.new(nil)

          object_space["::ActiveRecord::Base"] =
            ModuleNode.new(
              "ActiveRecord::Base",
              module_parent: object_space[""],
              abstract_class: nil
            )
          object_space["::ActiveRecord::Base"].is_active_record_base = true

          object_space.each do |class_name, extracted_class|
            possible_namespace_levels = class_name.split("::")[1..-2] || []

            if extracted_class.claimed_superclass == "" || extracted_class.claimed_superclass == nil
              next
            end

            (possible_namespace_levels.size + 1).times do
              class_uri_parts =
                possible_namespace_levels + [extracted_class.claimed_superclass]

              class_uri = "::#{class_uri_parts.join("::")}"

              if object_space[class_uri]
                extracted_class.superclass = object_space[class_uri]
                break
              end

              possible_namespace_levels.pop
            end
          end

          object_space
        end

        private

        def self_and_outer_class(extracted_class)
          if extracted_class.nil?
            return nil
          end

          [
            extracted_class,
            self_and_outer_class(extracted_class.parent)
          ]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ahnnotate-0.5.1 lib/ahnnotate/facet/models/resolve_class_relationships.rb
ahnnotate-0.5.0 lib/ahnnotate/facet/models/resolve_class_relationships.rb