Class: Shinmun::Post
- Inherits:
-
Object
- Object
- Shinmun::Post
- Defined in:
- lib/shinmun/post.rb
Overview
This class represents a post or page. Each post has a header, encoded as YAML and a body.
Example:
---
category: Ruby
date: 2008-09-05
title: BlueCloth, a Markdown library
---
This is the summary, which is by definition the first paragraph of the
article. The summary shows up in list views and rss feeds.
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#body_html ⇒ Object
Returns the value of attribute body_html.
-
#dirname ⇒ Object
Returns the value of attribute dirname.
-
#head ⇒ Object
Returns the value of attribute head.
-
#name ⇒ Object
Returns the value of attribute name.
-
#src ⇒ Object
Returns the value of attribute src.
-
#summary ⇒ Object
Returns the value of attribute summary.
-
#tag_list ⇒ Object
Returns the value of attribute tag_list.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #==(obj) ⇒ Object
- #date=(date) ⇒ Object
-
#dump ⇒ Object
Convert to string representation.
- #eql?(obj) ⇒ Boolean
- #filename ⇒ Object
- #filename=(filename) ⇒ Object
-
#initialize(attributes = {}) ⇒ Post
constructor
Initialize empty post and set specified attributes.
- #method_missing(id, *args) ⇒ Object
-
#month ⇒ Object
Shortcut for month of date.
-
#parse(src) ⇒ Object
Split up the source into header and body.
- #path ⇒ Object
- #path=(path) ⇒ Object
-
#transform(src) ⇒ Object
Transform the body of this post.
-
#year ⇒ Object
Shortcut for year of date.
Constructor Details
#initialize(attributes = {}) ⇒ Post
Initialize empty post and set specified attributes.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/shinmun/post.rb', line 25 def initialize(attributes={}) @head = {} @body = '' attributes.each do |k, v| send "#{k}=", v end @type ||= 'md' parse(src) if src raise "post without a title" if title.nil? @name ||= title.downcase.gsub(/[ -]+/, '-').gsub(/[^-a-z0-9_]+/, '') @dirname = date ? "posts/#{year}/#{month}" : 'pages' end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/shinmun/post.rb', line 43 def method_missing(id, *args) key = id.to_s if @head.has_key?(key) @head[key] else raise NoMethodError, "undefined method `#{id}' for #{self}", caller(1) end end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
22 23 24 |
# File 'lib/shinmun/post.rb', line 22 def body @body end |
#body_html ⇒ Object
Returns the value of attribute body_html.
22 23 24 |
# File 'lib/shinmun/post.rb', line 22 def body_html @body_html end |
#dirname ⇒ Object
Returns the value of attribute dirname.
22 23 24 |
# File 'lib/shinmun/post.rb', line 22 def dirname @dirname end |
#head ⇒ Object
Returns the value of attribute head.
22 23 24 |
# File 'lib/shinmun/post.rb', line 22 def head @head end |
#name ⇒ Object
Returns the value of attribute name.
22 23 24 |
# File 'lib/shinmun/post.rb', line 22 def name @name end |
#src ⇒ Object
Returns the value of attribute src.
22 23 24 |
# File 'lib/shinmun/post.rb', line 22 def src @src end |
#summary ⇒ Object
Returns the value of attribute summary.
22 23 24 |
# File 'lib/shinmun/post.rb', line 22 def summary @summary end |
#tag_list ⇒ Object
Returns the value of attribute tag_list.
22 23 24 |
# File 'lib/shinmun/post.rb', line 22 def tag_list @tag_list end |
#type ⇒ Object
Returns the value of attribute type.
22 23 24 |
# File 'lib/shinmun/post.rb', line 22 def type @type end |
Instance Method Details
#==(obj) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/shinmun/post.rb', line 123 def ==(obj) if Post === obj if date year == obj.year and month == obj.month and name == obj.name else name == obj.name end end end |
#date=(date) ⇒ Object
52 53 54 |
# File 'lib/shinmun/post.rb', line 52 def date=(date) @head['date'] = String === date ? Date.parse(date) : date end |
#dump ⇒ Object
Convert to string representation
103 104 105 |
# File 'lib/shinmun/post.rb', line 103 def dump head.to_yaml + "---" + body end |
#eql?(obj) ⇒ Boolean
119 120 121 |
# File 'lib/shinmun/post.rb', line 119 def eql?(obj) self == obj end |
#filename ⇒ Object
66 67 68 |
# File 'lib/shinmun/post.rb', line 66 def filename "#{name}.#{type}" end |
#filename=(filename) ⇒ Object
70 71 72 |
# File 'lib/shinmun/post.rb', line 70 def filename=(filename) self.name, self.type = filename.split('.') end |
#month ⇒ Object
Shortcut for month of date
62 63 64 |
# File 'lib/shinmun/post.rb', line 62 def month date.month end |
#parse(src) ⇒ Object
Split up the source into header and body. Load the header as yaml document.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/shinmun/post.rb', line 86 def parse(src) if src =~ /\A---(.*?)---(.*)/m @head = YAML.load($1) @body = $2 else raise ArgumentError, "yaml header not found in src" end @body_html = transform(@body) @summary = body_html.split("\n\n")[0] @tag_list = .to_s.split(",").map { |s| s.strip } @dirname = date ? "posts/#{year}/#{month}" : 'pages' self end |
#path ⇒ Object
74 75 76 |
# File 'lib/shinmun/post.rb', line 74 def path dirname.to_s.empty? ? filename : "#{dirname}/#{filename}" end |
#path=(path) ⇒ Object
78 79 80 81 82 |
# File 'lib/shinmun/post.rb', line 78 def path=(path) list = path.split('/') self.dirname = list[0..-2].join('/') self.filename = list[-1] end |
#transform(src) ⇒ Object
Transform the body of this post. Defaults to Markdown.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/shinmun/post.rb', line 108 def transform(src) case type when 'html' RubyPants.new(src).to_html when 'tt' RubyPants.new(RedCloth.new(src).to_html).to_html else RubyPants.new(BlueCloth.new(src).to_html).to_html end end |
#year ⇒ Object
Shortcut for year of date
57 58 59 |
# File 'lib/shinmun/post.rb', line 57 def year date.year end |