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) ⇒ 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
  
end

Instance Attribute Details

#authorObject



67
68
69
# File 'lib/weblog/post.rb', line 67

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



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

#_load_metadataObject



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

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



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_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



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

#blobObject

— Attributes



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

#contentObject



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

def content
  snippet.content
end

#first_commitObject



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

#generateObject



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_filenameObject



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

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

#javascriptsObject



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_commitObject



42
43
44
# File 'lib/weblog/post.rb', line 42

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

#metadata_filenameObject

— Metadata



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

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

#modified?Boolean

Returns:

  • (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_atObject



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

#renderObject



136
137
138
139
140
# File 'lib/weblog/post.rb', line 136

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

#snippetObject



116
117
118
# File 'lib/weblog/post.rb', line 116

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

#stylesheetsObject



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_filenameObject



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

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

#titleObject



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

def title
  snippet.title.strip
end

#updated_atObject



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