Class: Weblog::Index

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

Constant Summary collapse

SIZE =
5

Instance Method Summary collapse

Methods included from Helpers

#format_date, #format_date_and_time

Constructor Details

#initialize(weblog) ⇒ Index

Returns a new instance of Index.



5
6
7
# File 'lib/weblog/index.rb', line 5

def initialize(weblog)
  @weblog = weblog
end

Instance Method Details

#feed_filenameObject



43
44
45
# File 'lib/weblog/index.rb', line 43

def feed_filename
  File.join(@weblog.path, 'public', 'weblog.rss')
end

#feed_template_filenameObject



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

def feed_template_filename
  File.join(@weblog.path, "templates/index.rss.erb")
end

#generateObject



47
48
49
50
51
52
53
54
# File 'lib/weblog/index.rb', line 47

def generate
  FileUtils.mkdir_p(File.dirname(html_filename))
  File.open(html_filename, 'w') { |file| file.write(render(html_template_filename)) }
  puts "Generated weblog index" if @weblog.verbose?
  FileUtils.mkdir_p(File.dirname(feed_filename))
  File.open(feed_filename, 'w') { |file| file.write(render(feed_template_filename)) }
  puts "Generated weblog feed" if @weblog.verbose?
end

#html_filenameObject



39
40
41
# File 'lib/weblog/index.rb', line 39

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

#html_template_filenameObject



25
26
27
# File 'lib/weblog/index.rb', line 25

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

#postsObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/weblog/index.rb', line 9

def posts
  if @posts.nil?
    @posts = []
    @weblog.months.reverse.each do |month|
      month.posts.each do |post|
        @posts << post
        return @posts if @posts.length >= SIZE
      end
    end
  end; @posts
end

#render(template_filename) ⇒ Object



33
34
35
36
37
# File 'lib/weblog/index.rb', line 33

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