Class: Hx::Listing::RecursiveIndex

Inherits:
Object
  • Object
show all
Includes:
Filter
Defined in:
lib/hx/listing/recursiveindex.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Filter

#each_entry, #edit_entry

Constructor Details

#initialize(input, options) ⇒ RecursiveIndex

Returns a new instance of RecursiveIndex.



39
40
41
42
43
# File 'lib/hx/listing/recursiveindex.rb', line 39

def initialize(input, options)
  @input = input
  @index_name = options[:index_name] || "index"
  @index_re = %r!^((?:.*/)?)#{Regexp.quote(@index_name)}$!
end

Class Method Details

.new(input, options) ⇒ Object



34
35
36
37
# File 'lib/hx/listing/recursiveindex.rb', line 34

def self.new(input, options)
  listing = super(input, options)
  Listing.apply_options(listing, options)
end

Instance Method Details

#each_entry_path(selector) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hx/listing/recursiveindex.rb', line 45

def each_entry_path(selector)
  emitted = Set.new
  @input.each_entry_path(Path::ALL) do |path|
    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
    end
  end
  self
end

#get_entry(path) ⇒ Object

Raises:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hx/listing/recursiveindex.rb', line 60

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
  end
  raise NoSuchEntryError, path if index['items'].empty?
  index
end