lib/codependency/graph.rb in codependency-0.1.0 vs lib/codependency/graph.rb in codependency-0.2.0
- old
+ new
@@ -1,28 +1,31 @@
module Codependency
class Graph
- def initialize( filenames )
- @nodes = Hash[ filenames.map { |f| [ f, Node.new( f ) ] } ]
+ def initialize( start, options={} )
+ @options = options
+ @nodes = Hash.new { |h, k| h[ k ] = Node.new( k, parser ) }
+ @start = @nodes[ start ]
end
- attr_reader :nodes
def files
- nodes.reduce( [ ] ) do |list, (filename, node)|
- resolve node, list
- list
- end
+ [ ].tap { |list| resolve @start, list }.reverse.map &:filename
end
protected
def resolve( node, list )
- unless list.include? node.filename
- node.dependencies.each do |dep|
- # TODO need to check here to make sure
- # we actually have a node for this dependency
- resolve nodes[ dep ], list
+ list << node # TODO if the node were in the list here,
+ # would that indicate a circular dependency?
+ node.dependencies.map { |filename| @nodes[ filename ] }.each do |dep|
+ if list.include?( dep )
+ list << list.slice!( list.index( dep ) )
+ else
+ resolve dep, list
end
- list << node.filename
end
+ end
+
+ def parser
+ @parser ||= Parser.new @options
end
end
end