Sha256: f34417a9113d793f36e6f6d8292763dc8a53842d6a0da4be8d76177aafed15c0

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

module Society

  class ReferenceProcessor

    attr_reader :classes, :references, :unresolved_references

    def initialize(classes)
      @classes = classes
      @references = []
      @unresolved_references = []
      process
    end

    private

    def process
      classes.each do |klass|
        klass.constants.each do |const|
          if assigned_constant(const, klass)
            next
          elsif target = perfect_match_for(const)
            @references << Edge.new(from: klass,
                                    to: target,
                                    meta: {
                                      type: :perfect,
                                      entity: const})
          elsif target = partial_match_for(const)
            @references << Edge.new(from: klass,
                                    to: target,
                                    meta: {
                                      type: :partial,
                                      entity: const})
          else
            @unresolved_references << { class: klass,
                                        target_name: const.full_name,
                                        constant: const }
          end
        end
      end
    end

    def perfect_match_for(const)
      classes.detect { |klass| klass.full_name == const.name }
    end

    def partial_match_for(const)
      partial_matches = classes.select do |klass|
        klass.full_name.include? const.full_name
      end
      partial_matches.first if partial_matches.size == 1
    end

    def assigned_constant(const, klass)
      klass.constant_assignments.map(&:name).include? const.name
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
society-1.1.1 lib/society/reference_processor.rb
society-1.1.0 lib/society/reference_processor.rb
society-1.0.0 lib/society/reference_processor.rb