Class: SemiStatic::Post

Inherits:
Base
  • Object
show all
Includes:
Convertable
Defined in:
lib/semi-static/post.rb

Overview

Post represents a single post in the site. Post itself only represents a single source file, but can have a Layout and any number of Snippets, each of which has its own source file. Post’s modification time is the most recent of itself, its Layout, and any Snippets used.

Instance Attribute Summary collapse

Attributes inherited from Base

#full_source_path, #site, #source_content, #source_ext, #source_metadata, #source_path

Instance Method Summary collapse

Methods included from Convertable

#content, #layout, #layout_name, #load, #object_ref, #render, #snippet, #source_mtime, #underscore

Methods inherited from Base

#source_mtime

Constructor Details

#initialize(site, path) ⇒ Post

Initializes a new Post

site

The Site object we belong to

path

The relative path to the source file



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/semi-static/post.rb', line 50

def initialize(site, path)
    super
    @metadata = [ :title, :layout, :author ]
    
    @name = File.basename(source_path, source_ext)
    
    @tags = []
    unless [:tags].nil?
        for tag in [:tags]
            @tags << site.tags[tag]
            @tags.last << self
        end
    end
    
    if name =~ /^(\d+-\d+-\d+)-(.+)$/
        @created = Time.parse $1
        @updated ||= @created
        @slug = $2
        @output_dir = created.strftime('%Y/%m/%d')
        @output_path = File.join output_dir, "#{slug}.html"
        @uri = "/#{output_path}"
    else
        raise ArgumentError, "Bad file name: #{name}"
    end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SemiStatic::Base

Instance Attribute Details

#createdObject (readonly)

Date Post was created



35
36
37
# File 'lib/semi-static/post.rb', line 35

def created
  @created
end

#nameObject (readonly)

The Post’s name



11
12
13
# File 'lib/semi-static/post.rb', line 11

def name
  @name
end

#output_dirObject (readonly)

The Post’s output directory



19
20
21
# File 'lib/semi-static/post.rb', line 19

def output_dir
  @output_dir
end

#output_pathObject (readonly)

The Post’s output path



23
24
25
# File 'lib/semi-static/post.rb', line 23

def output_path
  @output_path
end

#slugObject (readonly)

The Post’s slug



27
28
29
# File 'lib/semi-static/post.rb', line 27

def slug
  @slug
end

#tagsObject (readonly)

The Post’s Tags



15
16
17
# File 'lib/semi-static/post.rb', line 15

def tags
  @tags
end

#updatedObject (readonly)

Date the Post was last updated



39
40
41
# File 'lib/semi-static/post.rb', line 39

def updated
  @updated
end

#uriObject (readonly)

The Post’s URI



31
32
33
# File 'lib/semi-static/post.rb', line 31

def uri
  @uri
end

Instance Method Details

#dayObject

Day the Post was published as a String (i.e., “15”)



102
103
104
# File 'lib/semi-static/post.rb', line 102

def day
    created.strftime '%d'
end

#idObject

Get the Post’s unique identifier



78
79
80
# File 'lib/semi-static/post.rb', line 78

def id
    name.gsub /-/, '_'
end

#monthObject

Month the Post was published as a String (i.e., “03”)



96
97
98
# File 'lib/semi-static/post.rb', line 96

def month
    created.strftime '%m'
end

Get the Post’s permalink



84
85
86
# File 'lib/semi-static/post.rb', line 84

def permalink
    return "#{uri}"
end

#time?Boolean

Does the post have a Time set?

Returns:

  • (Boolean)


108
109
110
# File 'lib/semi-static/post.rb', line 108

def time?
    false
end

#yearObject

Year the Post was published as a String (i.e., “2009”)



90
91
92
# File 'lib/semi-static/post.rb', line 90

def year
    created.strftime '%Y'
end