Class: Weblog::Month

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#format_date, #format_date_and_time

Constructor Details

#initialize(weblog, year, month) ⇒ Month

Returns a new instance of Month.



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

def initialize(weblog, year, month)
  @weblog = weblog
  @year   = year.to_i
  @month  = month.to_i
  @path   = File.join(@year.to_s, ("%02d" % @month))
end

Instance Attribute Details

#monthObject

Returns the value of attribute month.



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

def month
  @month
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/weblog/month.rb', line 12

def path
  @path
end

#yearObject

Returns the value of attribute year.



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

def year
  @year
end

Class Method Details

.all(weblog) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/weblog/month.rb', line 25

def self.all(weblog)
  months = []
  Dir.entries(File.join(weblog.path, 'public')).each do |year|
    if year =~ /^\d{4}$/
      Dir.entries(File.join(weblog.path, 'public', year)).each do |month|
        if month =~ /^\d{2}$/
          months << new(weblog, year, month)
        end
      end
    end
  end
  months.sort_by do |month|
    [month.year, month.month]
  end
end

Instance Method Details

#generateObject



59
60
61
62
63
64
65
# File 'lib/weblog/month.rb', line 59

def generate
  FileUtils.mkdir_p(File.dirname(index_filename))
  File.open(index_filename, 'w') do |file|
    file.write(render)
  end
  puts "Generated #{@year}/#{"%02d" % @month}" if @weblog.verbose?
end

#index_filenameObject



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

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

#postsObject

Returns a list of posts for this month in reverse chronological order



15
16
17
18
19
# File 'lib/weblog/month.rb', line 15

def posts
  @weblog.posts.select do |post|
    post.path.start_with?(@path)
  end.sort_by { |post| -post.published_at.to_i }
end

#renderObject



49
50
51
52
53
# File 'lib/weblog/month.rb', line 49

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

#sizeObject



21
22
23
# File 'lib/weblog/month.rb', line 21

def size
  posts.size
end

#template_filenameObject



45
46
47
# File 'lib/weblog/month.rb', line 45

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