Class: Hx::Listing::DateArchive

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Filter

#each_entry_path, #get_entry

Methods included from View

#edit_entry

Constructor Details

#initialize(input, options) ⇒ DateArchive

Returns a new instance of DateArchive.



38
39
40
41
42
# File 'lib/hx/listing/datearchive.rb', line 38

def initialize(input, options)
  @input = input
  @date_fields = Array(options[:date_fields] || %w(created updated))
  @index_name = options[:index_name] || "index"
end

Class Method Details

.new(input, options) ⇒ Object



33
34
35
36
# File 'lib/hx/listing/datearchive.rb', line 33

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

Instance Method Details

#each_entry(selector) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hx/listing/datearchive.rb', line 44

def each_entry(selector)
  indices = {}
  @input.each_entry(Path::ALL) do |path, entry|
    date = @date_fields.map { |field| entry[field] }.compact.first
    next unless date
    month_path = "%04d/%02d/#{@index_name}" % [date.year, date.month]
    year_path = "%04d/#{@index_name}" % [date.year]
    for index_path in [month_path, year_path]
      next unless selector.accept_path? index_path
      index_entry = indices[index_path]
      unless index_entry
        index_entry = {"items" => [], "year" => date.year, "updated" => date}
        index_entry["month"] = date.month if index_path == month_path
        indices[index_path] = index_entry
      end
      index_entry["updated"] = date if date > index_entry["updated"]
      index_entry["items"] << {"path" => path, "entry" => entry}
    end
  end
  indices.each do |index_path, index_entry|
    yield index_path, index_entry
  end
  self
end