lib/ro/root.rb in ro-1.4.6 vs lib/ro/root.rb in ro-4.2.0

- old
+ new

@@ -1,52 +1,73 @@ module Ro - class Root < ::String - def initialize(root) - super(Ro.realpath(root.to_s)) - ensure - raise ArgumentError.new("root=#{ root.inspect }") if root.nil? - raise ArgumentError.new("root=#{ root.inspect }") unless test(?d, self) - end - - def root + class Root < Path + def identifier self end - def nodes - Node::List.new(root) do |list| - node_directories do |path| - list.load(path) - end + def collections(&block) + accum = Collection::List.for(self) + + subdirectories do |subdirectory| + collection = collection_for(subdirectory) + block ? block.call(collection) : accum.push(collection) end - end - def directories(&block) - Dir.glob(File.join(root, '*/'), &block) + block ? self : accum end - def node_directories(&block) - Dir.glob(File.join(root, '*/*/'), &block) + def collection_for(subdirectory) + Collection.new(subdirectory) end - def git - @git ||= Git.new(self) + def paths_for(name) + [ + subdirectory_for(name), + subdirectory_for(Slug.for(name, :join => '-')), + subdirectory_for(Slug.for(name, :join => '_')), + ] end - def patch(*args, &block) - git.patch(*args, &block) + def get(name) + name = name.to_s + + if name.index('/') + collection_name, node_name = name.split('/', 2) + collection = get(collection_name) + + if collection + node = collection.get(node_name) + return node + else + return nil + end + end + + paths_for(name).each do |path| + next unless path.directory? + return collection_for(path) + end + + nil end - def dotdir - File.join(self, '.ro') + def [](name) + get(name) end - def lockpath - File.join(dotdir, 'lock') + def nodes(&block) + accum = [] + + collections.each do |collection| + collection.nodes do |node| + block ? block.call(node) : accum.push(node) + end + end + + block ? self : accum end - def lock(&block) - FileUtils.mkdir_p(File.dirname(lockpath)) - @lock ||= Lock.new(lockpath) - block ? @lock.lock(&block) : @lock + def method_missing(name, *args, **kws, &block) + get(name) || super end end end