Sha256: d6ce81152ccb003a5daee0e3874c2d963d7ce1c3fcbfa6bf36f3d463e3bf65d4

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

module Ahnnotate
  module Facet
    module Models
      class ResolveClassRelationships
        include ProcParty

        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",
              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

3 entries across 3 versions & 1 rubygems

Version Path
ahnnotate-0.4.0 lib/ahnnotate/facet/models/resolve_class_relationships.rb
ahnnotate-0.3.0 lib/ahnnotate/facet/models/resolve_class_relationships.rb
ahnnotate-0.2.0 lib/ahnnotate/facet/models/resolve_class_relationships.rb