Class: Weblog::Post
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Helpers
#format_date, #format_date_and_time
Constructor Details
#initialize(weblog, path) ⇒ Post
Returns a new instance of Post.
5
6
7
8
9
10
|
# File 'lib/weblog/post.rb', line 5
def initialize(weblog, path)
@weblog = weblog
@path = path
@post = self
_load_metadata
end
|
Instance Attribute Details
#author ⇒ Object
67
68
69
|
# File 'lib/weblog/post.rb', line 67
def author
@author ||= _author
end
|
#path ⇒ Object
Returns the value of attribute path.
3
4
5
|
# File 'lib/weblog/post.rb', line 3
def path
@path
end
|
Instance Method Details
#_author ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/weblog/post.rb', line 59
def _author
if commit = first_commit
first_commit.author.name
else
@weblog.author
end
end
|
28
29
30
31
32
|
# File 'lib/weblog/post.rb', line 28
def _load_metadata
JSON.parse(File.read(metadata_filename)).each do |key, value|
self.send("#{key}=", value)
end if File.exist?(metadata_filename)
end
|
#_published_at ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/weblog/post.rb', line 74
def _published_at
if commit = first_commit
commit.date
else
Time.now
end
end
|
#_updated_at ⇒ Object
-
The updated date:
-
When it’s not in the git repository: nil
-
When it has modifications: current date and time
-
When it has no modifications: last commit date
90
91
92
93
94
95
96
97
98
|
# File 'lib/weblog/post.rb', line 90
def _updated_at
if modified?
Time.now
elsif commit = last_commit
commit.date
else
nil
end
end
|
#blob ⇒ Object
36
37
38
39
40
|
# File 'lib/weblog/post.rb', line 36
def blob
if File.exist?(index_filename)
@blob ||= @weblog.git.gblob(index_filename)
end
end
|
#content ⇒ Object
124
125
126
|
# File 'lib/weblog/post.rb', line 124
def content
snippet.content
end
|
#first_commit ⇒ Object
46
47
48
|
# File 'lib/weblog/post.rb', line 46
def first_commit
@first_commit ||= blob.log(10_000).map{|l|l}.last if blob
end
|
#generate ⇒ Object
146
147
148
149
150
151
152
|
# File 'lib/weblog/post.rb', line 146
def generate
FileUtils.mkdir_p(File.dirname(index_filename))
File.open(index_filename, 'w') do |file|
file.write(render)
end
puts "Generated #{path}" if @weblog.verbose?
end
|
#index_filename ⇒ Object
142
143
144
|
# File 'lib/weblog/post.rb', line 142
def index_filename
File.join(@weblog.path, 'public', @path, 'index.html')
end
|
#javascripts ⇒ Object
110
111
112
113
114
|
# File 'lib/weblog/post.rb', line 110
def javascripts
Dir.glob(File.join(@weblog.path, 'public', path, '*.js')).map do |path|
File.basename(path)
end
end
|
#last_commit ⇒ Object
42
43
44
|
# File 'lib/weblog/post.rb', line 42
def last_commit
@last_commit ||= blob.log(1).first if blob
end
|
14
15
16
|
# File 'lib/weblog/post.rb', line 14
def metadata_filename
File.join(@weblog.path, 'public', @path, 'post.json')
end
|
#modified? ⇒ Boolean
50
51
52
53
54
|
# File 'lib/weblog/post.rb', line 50
def modified?
@modified ||= @weblog.git.status.changed.any? do |filename, status|
snippet.filename.end_with?(filename)
end
end
|
#published_at ⇒ Object
82
83
84
|
# File 'lib/weblog/post.rb', line 82
def published_at
@published_at ||= _published_at
end
|
#published_at=(published_at) ⇒ Object
20
21
22
|
# File 'lib/weblog/post.rb', line 20
def published_at=(published_at)
@published_at = Time.parse(published_at)
end
|
#render ⇒ Object
136
137
138
139
140
|
# File 'lib/weblog/post.rb', line 136
def render
Erubis::EscapedEruby.new(
File.read(template_filename)
).result(binding)
end
|
#snippet ⇒ Object
116
117
118
|
# File 'lib/weblog/post.rb', line 116
def snippet
@snippet ||= Snippet.new(@weblog, self, File.join('public', path))
end
|
#stylesheets ⇒ Object
104
105
106
107
108
|
# File 'lib/weblog/post.rb', line 104
def stylesheets
Dir.glob(File.join(@weblog.path, 'public', path, '*.css')).map do |path|
File.basename(path)
end
end
|
#template_filename ⇒ Object
132
133
134
|
# File 'lib/weblog/post.rb', line 132
def template_filename
File.join(@weblog.path, "templates/post.html.erb")
end
|
#title ⇒ Object
120
121
122
|
# File 'lib/weblog/post.rb', line 120
def title
snippet.title.strip
end
|
#updated_at ⇒ Object
100
101
102
|
# File 'lib/weblog/post.rb', line 100
def updated_at
@updated_at ||= _updated_at
end
|
#updated_at=(updated_at) ⇒ Object
24
25
26
|
# File 'lib/weblog/post.rb', line 24
def updated_at=(updated_at)
@updated_at = Time.parse(updated_at)
end
|