lib/codependency/graph.rb in codependency-1.0.0 vs lib/codependency/graph.rb in codependency-2.0.0
- old
+ new
@@ -1,61 +1,54 @@
require 'tsort'
module Codependency
class Graph < Hash
- def initialize( path, options={} )
- @path, @options = path, options
- super( ){ |h, k| h[ k ] = parser.parse( k ) }
- end
- attr_reader :path, :options
-
- include TSort
-
##
- # the dirname to use for this graph, based on the path
- def dirname
- File.dirname path
+ # Adds a file to the dependency graph. The filename given should be
+ # relative and should not contain an extension, like:
+ #
+ # # require application/user
+ #
+ # Attempts to locate and add all dependencies of this file recursively,
+ # creating our graph.
+ def require( file )
+ self[ file ] ||= parser.parse path[ file ]
+ self[ file ].each do |file|
+ self << file unless key?( file )
+ end
end
+ alias :<< :require
##
- # the extname to use for this graph, based on the path
- def extname
- File.extname path
+ # Returns the sorted list of files as determined by this graph,
+ # relative to the calling file.
+ def files
+ here = Pathname( caller.first.split( ':' ).shift )
+ tsort.map { |file| path[ file ].relative_path_from( here ) }
end
##
- # walk the entire graph and return self
- def populate
- walk path
- self
+ # The path set for this dependency graph.
+ def path
+ @path ||= Path.new parser.extensions
end
##
- # discover all nodes in this graph by walking it
- def walk( path )
- self[ path ].each { |path| walk( path ) unless has_key?( path ) }
- end
-
- ##
- # the parser to use for this graph. shared by all nodes.
+ # The file parser for this dependency graph.
def parser
- @parser ||= begin
- Parser.new options.merge( :dirname => dirname, :extname => extname )
- end
+ @parser ||= Parser.new
end
- ##
- # a topologically sorted list of all dependencies of the `start` file.
- def files
- populate.tsort
- end
-
private
+ include TSort
+
+ ##
# tsort interface
alias :tsort_each_node :each_key
+ ##
# tsort interface
def tsort_each_child( node, &block )
fetch( node ).each( &block )
end
end