-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Circular dependencies #4 #5
Changes from all commits
da30e67
1321398
85658c8
6039ca5
96ef96b
8f951f8
ef9dbde
5da88e5
9f4bcbf
973e4f2
a77a93e
39d88e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
tmp/ | ||
pkg/ | ||
.DS_Store | ||
.DS_Store | ||
.idea | ||
.byebug_history |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ruby: | ||
config_file: .rubocop.yml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
source "https://rubygems.org" | ||
source 'https://rubygems.org' | ||
gemspec |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
require "bundler/gem_tasks" | ||
require "rake/testtask" | ||
require 'bundler/gem_tasks' | ||
|
||
Rake::TestTask.new(:test) do |t| | ||
t.libs << "test" | ||
t.libs << "lib" | ||
t.test_files = FileList['test/**/*_test.rb'] | ||
task :s do | ||
ruby './bin/rubrowser' | ||
end | ||
|
||
task default: :test | ||
begin | ||
require 'rspec/core/rake_task' | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
task default: :spec | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class A | ||
def initialize | ||
B.do | ||
end | ||
|
||
def self.do; end | ||
end | ||
|
||
class B | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing top-level class documentation comment. |
||
def initialize | ||
C.do | ||
end | ||
|
||
def self.do; end | ||
end | ||
|
||
class C | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing top-level class documentation comment. |
||
def initialize | ||
A.do | ||
end | ||
|
||
def self.do; end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,72 @@ | ||
require 'rubrowser/parser/factory' | ||
require 'tsort' | ||
|
||
module Rubrowser | ||
class Data | ||
attr_reader :definitions, :relations | ||
|
||
def initialize(files) | ||
@files = files | ||
@parsed = false | ||
parse | ||
end | ||
|
||
def definitions | ||
@_definitions ||= parsers.map(&:definitions).reduce(:+).to_a | ||
def mark_circular_dependencies | ||
mark_circular_components(components) | ||
end | ||
|
||
def mark_circular_components(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 components.include?(relation.namespace.to_s) | ||
end | ||
end | ||
|
||
def 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 relations | ||
@_relations ||= parsers.map(&:relations).reduce(:+).to_a | ||
def find_coupled_components(graph) | ||
graph | ||
.strongly_connected_components | ||
.select { |c| c.length > 1 } | ||
.flatten | ||
.to_set | ||
end | ||
|
||
private | ||
|
||
class Graph < Hash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing top-level class documentation comment. |
||
include TSort | ||
|
||
alias tsort_each_node each_key | ||
|
||
def tsort_each_child(node, &block) | ||
fetch(node) { [] }.each(&block) | ||
end | ||
end | ||
|
||
attr_reader :files, :parsed | ||
alias parsed? parsed | ||
|
||
def parse | ||
return if parsed? | ||
parsers.each(&:parse) | ||
@parsed = true | ||
|
||
@definitions ||= parsers.map(&:definitions).reduce(:+).to_a | ||
@relations ||= parsers.map(&:relations).reduce(:+).to_a | ||
|
||
mark_circular_dependencies | ||
end | ||
|
||
def parsers | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing top-level class documentation comment.
Missing magic comment # frozen_string_literal: true.