diff options
Diffstat (limited to 'lib/page.rb')
-rw-r--r-- | lib/page.rb | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/page.rb b/lib/page.rb new file mode 100644 index 0000000..d0b18ef --- /dev/null +++ b/lib/page.rb @@ -0,0 +1,83 @@ +# coding: utf-8 +require 'set' + +require 'tag' + +class Page + # Page is an abstract class. + # + # Implementors must implement several methods: + # + # def url => URI + # def title => String + # def author => Person + # def content => html | nil + # def rights => html | nil + # + # def _tags => String | Enumerable<String> + # + # def _published => DateTime | nil + # def _updated => DateTime | nil + # def _years => Enumerable<Fixnum> + + def tags # => Enumerable<Tag> + if @tags.nil? + raw = _tags + if raw.is_a?(String) + raw = raw.split + end + @tags = raw.map{|tag|Tag.new(tag)} + end + @tags + end + + def published # => DateTime | nil + if @published.nil? + unless _published.nil? + @published = _published + else + unless _updated.nil? + @published = _updated + end + end + # sanity check + unless _published.nil? or _updated.nil? + if _updated < _published + @published = _updated + end + end + end + @published + end + + def updated # => DateTime | nil + if @updated.nil? + unless _updated.nil? + @updated = _updated + else + unless _published.nil? + @updated = _published + end + end + end + @updated + end + + def years # => Enumerable<Fixnum> + if @years.nil? + if published.nil? || updated.nil? + @years = Set[] + else + first = published.year + last = updated.year + + years = _years + years.add(first) + years.add(last) + + @years = Set[*years.select{|i|i >= first && i <= last}] + end + end + @years + end +end |