summaryrefslogtreecommitdiff
path: root/lib/page.rb
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2017-01-06 19:50:45 -0500
committerLuke Shumaker <lukeshu@sbcglobal.net>2017-01-06 19:50:45 -0500
commit7edb003cd1c9b53ffdff11ef85532e39f08db16d (patch)
tree180db82a4af7720508ae2732393401bf4d27cadf /lib/page.rb
parent7d875df65221d4da91953cf129a03e76fe8e5d29 (diff)
wip
Diffstat (limited to 'lib/page.rb')
-rw-r--r--lib/page.rb83
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