# coding: utf-8 require 'set' require 'category' class Page # Page is an abstract class. # # Implementors must implement several methods: # # def url => URI # def atom_title => String # def atom_author => Person # def atom_content => html | nil # def atom_rights => html | nil # # def page_categories => String | Enumerable # # def page_published => DateTime | nil # def page_updated => DateTime | nil # def page_years => Enumerable def atom_categories # => Enumerable if @categories.nil? raw = page_categories if raw.is_a?(String) raw = raw.split end @categories = raw.map{|abbr|Category.new(abbr)} end @categories end def atom_published # => DateTime | nil if @published.nil? unless page_published.nil? @published = page_published else unless page_updated.nil? @published = page_updated end end # sanity check unless page_published.nil? or page_updated.nil? if page_updated < page_published @published = page_updated end end end @published end def atom_updated # => DateTime | nil if @updated.nil? unless page_updated.nil? @updated = page_updated else unless page_published.nil? @updated = page_published end end end @updated end def years # => Enumerable if @years.nil? if published.nil? || updated.nil? @years = Set[] else first = published.year last = updated.year years = page_years years.add(first) years.add(last) @years = Set[*years.select{|i|i >= first && i <= last}] end end @years end end