From 7edb003cd1c9b53ffdff11ef85532e39f08db16d Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 6 Jan 2017 19:50:45 -0500 Subject: wip --- lib/page.rb | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 lib/page.rb (limited to 'lib/page.rb') 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 + # + # def _published => DateTime | nil + # def _updated => DateTime | nil + # def _years => Enumerable + + def tags # => Enumerable + 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 + 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 -- cgit v1.2.3