lib/hx/listing/recursiveindex.rb in hx-0.13.0 vs lib/hx/listing/recursiveindex.rb in hx-0.14.0

- old
+ new

@@ -22,10 +22,11 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. require 'hx' require 'hx/listing/limit' require 'hx/listing/paginate' +require 'set' module Hx module Listing class RecursiveIndex @@ -50,30 +51,39 @@ updated = [entry, index].map { |e| e['updated'] }.compact.max index['updated'] = updated if updated end private :merge_updated - def each_entry(selector) - indexes = Hash.new { |h,k| h[k] = {'items' => []} } - @input.each_entry(Path::ALL) do |path, entry| + def each_entry_path(selector) + emitted = Set.new + @input.each_entry_path(Path::ALL) do |path, entry| components = path.split("/") - if components.last == "index" - index = indexes[path] = entry.merge(indexes[path]) - merge_updated(index, entry) - else - until components.empty? - components.pop - index_path = (components + ["index"]).join("/") - index = indexes[index_path] - index['items'] << {'path' => path, 'entry' => entry} - merge_updated(index, entry) - end + until components.empty? + components.pop + index_path = (components + ["index"]).join("/") + break if emitted.include? index_path + yield index_path if selector.accept_path? index_path + emitted.add index_path end end - indexes.each do |path, entry| - yield path, entry if selector.accept_path? path - end self + end + + def get_entry(path) + raise NoSuchEntryError, path unless path =~ %r!^((?:.*/)?)index$! + prefix = $1 + selector = Path.parse_pattern("#{prefix}**") + 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 + merge_updated(index, entry) + end + raise NoSuchEntryError, path if index['items'].empty? + index end end end end