lib/hx/listing/recursiveindex.rb in hx-0.26.0 vs lib/hx/listing/recursiveindex.rb in hx-0.26.1
- old
+ new
@@ -37,42 +37,36 @@
end
def initialize(input, options)
@input = input
@index_name = options[:index_name] || "index"
- @index_re = %r!^((?:.*/)?)#{Regexp.quote(@index_name)}$!
end
- def each_entry_path(selector)
- emitted = Set.new
- @input.each_entry_path(Path::ALL) do |path|
+ def each_entry(selector)
+ indices = {}
+ @input.each_entry(Path::ALL) do |path, entry|
components = path.split("/")
until components.empty?
components.pop
index_path = (components + [@index_name]).join("/")
- break if emitted.include? index_path
- yield index_path if selector.accept_path? index_path
- emitted.add index_path
+ next unless selector.accept_path? index_path
+ index_entry = indices[index_path]
+ unless index_entry
+ index_entry = {'items' => []}
+ indices[index_path] = index_entry
+ end
+ if path == index_path
+ index_entry = entry.merge(index_entry)
+ indices[index_path] = index_entry
+ end
+ index_entry['items'] << {'path' => path, 'entry' => entry}
+ index_entry['updated'] = Hx.last_update_time(index_entry, entry)
end
end
- self
- end
-
- def get_entry(path)
- raise NoSuchEntryError, path unless path =~ @index_re
- selector = Path.parse_pattern("#{$1}**")
- index = {'items' => []}
- @input.each_entry(selector) do |child_path, entry|
- if child_path == path
- index = entry.merge(index)
- else
- index['items'] << {'path' => child_path, 'entry' => entry}
- end
- updated = [entry['updated'], index['updated']].compact.max
- index['updated'] = updated if updated
+ indices.each do |path, entry|
+ yield path, entry
end
- raise NoSuchEntryError, path if index['items'].empty?
- index
+ self
end
end
end
end