lib/hx/listing/recursiveindex.rb in hx-0.7.4 vs lib/hx/listing/recursiveindex.rb in hx-0.8.2

- old
+ new

@@ -19,11 +19,10 @@ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -require 'rubygems' require 'hx' require 'hx/listing/limit' require 'hx/listing/paginate' module Hx @@ -45,26 +44,34 @@ def initialize(input, options) @input = input end - def each_entry + def merge_updated(index, entry) + 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 do |path, entry| + @input.each_entry(Path::ALL) do |path, entry| components = path.split("/") - until components.empty? - components.pop - index_path = (components + ["index"]).join("/") - index = indexes[index_path] - index['items'] << {'path' => path, 'entry' => entry} - if entry['updated'] and - (not index['updated'] or entry['updated'] > index['updated']) - index['updated'] = entry['updated'] + 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 end end indexes.each do |path, entry| - yield path, entry + yield path, entry if selector.accept? path end self end end