summaryrefslogtreecommitdiff
path: root/lib/page.rb
blob: d0b18ef09c0b6cd8ef5172c221dc0946b7f5842d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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