Class: Weblog::Month
Instance Attribute Summary collapse
-
#month ⇒ Object
Returns the value of attribute month.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#year ⇒ Object
Returns the value of attribute year.
Class Method Summary collapse
Instance Method Summary collapse
- #generate ⇒ Object
- #index_filename ⇒ Object
-
#initialize(weblog, year, month) ⇒ Month
constructor
A new instance of Month.
-
#posts ⇒ Object
Returns a list of posts for this month in reverse chronological order.
- #render ⇒ Object
- #size ⇒ Object
- #template_filename ⇒ Object
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
#month ⇒ Object
Returns the value of attribute month.
3 4 5 |
# File 'lib/weblog/month.rb', line 3 def month @month end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/weblog/month.rb', line 12 def path @path end |
#year ⇒ Object
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
#generate ⇒ Object
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_filename ⇒ Object
55 56 57 |
# File 'lib/weblog/month.rb', line 55 def index_filename File.join(@weblog.path, 'public', @path, 'index.html') end |
#posts ⇒ Object
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 |
#render ⇒ Object
49 50 51 52 53 |
# File 'lib/weblog/month.rb', line 49 def render Erubis::EscapedEruby.new( File.read(template_filename) ).result(binding) end |
#size ⇒ Object
21 22 23 |
# File 'lib/weblog/month.rb', line 21 def size posts.size end |
#template_filename ⇒ Object
45 46 47 |
# File 'lib/weblog/month.rb', line 45 def template_filename File.join(@weblog.path, "templates/month.html.erb") end |