Sha256: 4cca45552d4ccece4e759f9a00cd8e13f8bee30113bc03e84672ebcd302400b1

Contents?: true

Size: 1.51 KB

Versions: 13

Compression:

Stored size: 1.51 KB

Contents

require 'rubrowser/parser/factory'
require 'rubrowser/graph'

module Rubrowser
  class Data
    attr_reader :definitions, :relations

    def initialize(files)
      @files = files
      parse
    end

    private

    def parse
      parsers.each(&:parse)

      @definitions ||= parsers.map(&:definitions).reduce(:+).to_a
      @relations ||= parsers.map(&:relations).reduce(:+).to_a

      mark_circular_dependencies
    end

    def parsers
      @_parsers ||= files.map do |file|
        Rubrowser::Parser::Factory.build(file)
      end
    end

    def mark_circular_dependencies
      components = make_components
      @definitions.each do |definition|
        if components.include?(definition.namespace.first.to_s)
          definition.set_circular
        end
      end

      @relations.each do |relation|
        relation.set_circular if circular_relation?(components, relation)
      end
    end

    def make_components
      graph = Graph.new { |h, k| h[k] = [] }

      @relations.each do |relation|
        graph[relation.caller_namespace.to_s] <<
          relation.resolve(definitions).to_s
      end

      find_coupled_components(graph)
    end

    def find_coupled_components(graph)
      graph
        .strongly_connected_components
        .select { |c| c.length > 1 }
        .flatten
        .to_set
    end

    def circular_relation?(components, relation)
      components.include?(relation.namespace.to_s) &&
        components.include?(relation.caller_namespace.to_s)
    end

    attr_reader :files
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rubrowser-2.9.0 lib/rubrowser/data.rb
rubrowser-2.8.0 lib/rubrowser/data.rb
rubrowser-2.7.1 lib/rubrowser/data.rb
rubrowser-2.7.0 lib/rubrowser/data.rb
rubrowser-2.6.0 lib/rubrowser/data.rb
rubrowser-2.5.0 lib/rubrowser/data.rb
rubrowser-2.4.0 lib/rubrowser/data.rb
rubrowser-2.3.0 lib/rubrowser/data.rb
rubrowser-2.2.0 lib/rubrowser/data.rb
rubrowser-2.1.0 lib/rubrowser/data.rb
rubrowser-2.0.0 lib/rubrowser/data.rb
rubrowser-0.3.1 lib/rubrowser/data.rb
rubrowser-0.3.0 lib/rubrowser/data.rb