Class: Weblog::Post

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/weblog/post.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#format_date, #format_date_and_time

Constructor Details

#initialize(weblog, path, options = {}) ⇒ Post

Returns a new instance of Post.



5
6
7
8
9
10
11
# File 'lib/weblog/post.rb', line 5

def initialize(weblog, path, options={})
  @weblog  = weblog
  @path    = path
  @post    = self
  @options = options
  
end

Instance Attribute Details

#authorObject



76
77
78
# File 'lib/weblog/post.rb', line 76

def author
  @author ||= _author
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/weblog/post.rb', line 3

def path
  @path
end

Instance Method Details

#_authorObject

  • Author:

    • When it’s in the git repository: first commit author

    • Otherwise: configured author



68
69
70
71
72
73
74
# File 'lib/weblog/post.rb', line 68

def _author
  if commit = first_commit
    first_commit.author.name
  else
    @weblog.author
  end
end

#_load_metadataObject



37
38
39
40
41
# File 'lib/weblog/post.rb', line 37

def 
  JSON.parse(File.read()).each do |key, value|
    self.send("#{key}=", value)
  end if File.exist?()
end

#_published_atObject

  • The created date:

    • When it’s not in the git repository: current date and time

    • When it’s in the git repository: first commit date



83
84
85
86
87
88
89
# File 'lib/weblog/post.rb', line 83

def _published_at
  if commit = first_commit
    commit.date
  else
    Time.now
  end
end

#_updated_atObject

  • 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



99
100
101
102
103
104
105
106
107
# File 'lib/weblog/post.rb', line 99

def _updated_at
  if modified?
    Time.now
  elsif commit = last_commit
    commit.date
  else
    nil
  end
end

#blobObject

— Attributes



45
46
47
48
49
# File 'lib/weblog/post.rb', line 45

def blob
  if File.exist?(index_filename)
    @blob ||= @weblog.git.gblob(index_filename)
  end
end

#contentObject



133
134
135
# File 'lib/weblog/post.rb', line 133

def content
  snippet.content
end

#draft?Boolean

— Metadata

Returns:

  • (Boolean)


15
16
17
# File 'lib/weblog/post.rb', line 15

def draft?
  @options[:draft] == true
end

#first_commitObject



55
56
57
# File 'lib/weblog/post.rb', line 55

def first_commit
  @first_commit ||= blob.log(10_000).map{|l|l}.last if blob
end

#generateObject



155
156
157
158
159
160
161
# File 'lib/weblog/post.rb', line 155

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_filenameObject



151
152
153
# File 'lib/weblog/post.rb', line 151

def index_filename
  File.join(@weblog.path, public_directory, @path, 'index.html')
end

#javascriptsObject



119
120
121
122
123
# File 'lib/weblog/post.rb', line 119

def javascripts
  Dir.glob(File.join(@weblog.path, public_directory, path, '*.js')).map do |javascript_path|
    File.join(path, File.basename(javascript_path))
  end
end

#last_commitObject



51
52
53
# File 'lib/weblog/post.rb', line 51

def last_commit
  @last_commit ||= blob.log(1).first if blob
end

#metadata_filenameObject



23
24
25
# File 'lib/weblog/post.rb', line 23

def 
  File.join(@weblog.path, public_directory, @path, 'post.json')
end

#modified?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/weblog/post.rb', line 59

def modified?
  @modified ||= @weblog.git.status.changed.any? do |filename, status|
    snippet.filename.end_with?(filename)
  end
end

#public_directoryObject



19
20
21
# File 'lib/weblog/post.rb', line 19

def public_directory
  draft? ? 'draft' : 'public'
end

#published_atObject



91
92
93
# File 'lib/weblog/post.rb', line 91

def published_at
  @published_at ||= _published_at
end

#published_at=(published_at) ⇒ Object



29
30
31
# File 'lib/weblog/post.rb', line 29

def published_at=(published_at)
  @published_at = Time.parse(published_at)
end

#renderObject



145
146
147
148
149
# File 'lib/weblog/post.rb', line 145

def render
  Erubis::EscapedEruby.new(
    File.read(template_filename)
  ).result(binding)
end

#snippetObject



125
126
127
# File 'lib/weblog/post.rb', line 125

def snippet
  @snippet ||= Snippet.new(@weblog, self, File.join(public_directory, path))
end

#stylesheetsObject



113
114
115
116
117
# File 'lib/weblog/post.rb', line 113

def stylesheets
  Dir.glob(File.join(@weblog.path, public_directory, path, '*.css')).map do |stylesheet_path|
    File.join(path, File.basename(stylesheet_path))
  end
end

#template_filenameObject



141
142
143
# File 'lib/weblog/post.rb', line 141

def template_filename
  File.join(@weblog.path, "templates/post.html.erb")
end

#titleObject



129
130
131
# File 'lib/weblog/post.rb', line 129

def title
  snippet.title.strip
end

#updated_atObject



109
110
111
# File 'lib/weblog/post.rb', line 109

def updated_at
  @updated_at ||= _updated_at
end

#updated_at=(updated_at) ⇒ Object



33
34
35
# File 'lib/weblog/post.rb', line 33

def updated_at=(updated_at)
  @updated_at = Time.parse(updated_at)
end