Class: Dimples::Post

Inherits:
Page
  • Object
show all
Defined in:
lib/dimples/post.rb

Overview

A single dated post on a site.

Constant Summary collapse

POST_FILENAME =
/(\d{4})-(\d{2})-(\d{2})-(.+)/.freeze

Instance Attribute Summary

Attributes inherited from Page

#contents, #metadata, #path

Instance Method Summary collapse

Methods inherited from Page

#extension, #filename, #render, #write

Constructor Details

#initialize(site, path) ⇒ Post

Returns a new instance of Post.



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

def initialize(site, path)
  super

  parts = File.basename(path, File.extname(path)).match(POST_FILENAME)

  [:layout] ||= @site.config.layouts.post

  [:date] = Date.new(parts[1].to_i, parts[2].to_i, parts[3].to_i)
  [:slug] = parts[4]
  [:categories] ||= []
  [:draft] ||= false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dimples::Page

Instance Method Details

#dayObject



34
35
36
# File 'lib/dimples/post.rb', line 34

def day
  @day ||= [:date].strftime('%d')
end

#monthObject



30
31
32
# File 'lib/dimples/post.rb', line 30

def month
  @month ||= [:date].strftime('%m')
end

#urlObject



21
22
23
24
# File 'lib/dimples/post.rb', line 21

def url
  path = date.strftime(draft ? @site.config.paths.drafts : @site.config.paths.posts)
  "/#{path}/#{slug}/"
end

#yearObject



26
27
28
# File 'lib/dimples/post.rb', line 26

def year
  @year ||= [:date].strftime('%Y')
end