lib/codependency/graph.rb in codependency-2.2.0 vs lib/codependency/graph.rb in codependency-2.3.0
- old
+ new
@@ -8,15 +8,14 @@
# graph, the key of which is the relative path to this file and
# the value is the array of relative paths to its dependencies.
# Any dependent files will also be recursively added to this
# graph.
def require( string )
- root = Pathname.getwd
- file = Pathname( string ).expand_path.relative_path_from( root ).to_path
+ file = path_to( string ).to_path
self[ file ] ||= parser.parse( file ).map do |short|
- path[ short ].relative_path_from( root ).to_path
+ path_to( path[ short ] ).to_path
end
self[ file ].each { |f| self.require( f ) unless key?( f ) }
end
alias :<< :require
@@ -49,8 +48,30 @@
##
# tsort interface
def tsort_each_child( node, &block )
fetch( node ).each( &block )
+ end
+
+ ##
+ # The current working directory of this graph. All paths in the graph
+ # will be relative to this path.
+ def root
+ @root ||= Pathname.getwd
+ end
+
+ ##
+ # Calculates the relative path from one path the `#root` path. Accepts
+ # a string, returns a Pathname object populated with the relative path.
+ def path_to( string )
+ path = Pathname( string )
+ path = path.expand_path
+ path = path.relative_path_from( root )
+
+ if path.to_path.start_with?( '.' )
+ path
+ else
+ Pathname( File.join( '.', path.to_path ) )
+ end
end
end
end