Class: Post
- Inherits:
-
Object
- Object
- Post
- Defined in:
- lib/post.rb
Constant Summary collapse
- DIV =
/\r?\n---/
Class Attribute Summary collapse
-
.base_dir ⇒ Object
Returns the value of attribute base_dir.
-
.glob ⇒ Object
Returns the value of attribute glob.
Class Method Summary collapse
- .categories ⇒ Object
- .filenames ⇒ Object
- .find ⇒ Object
- .find_by_tag(tag) ⇒ Object
- .find_latest ⇒ Object (also: find_single)
- .from_file(path) ⇒ Object
- .from_s(raw, date = nil) ⇒ Object
Instance Method Summary collapse
- #[](property) ⇒ Object
- #html ⇒ Object
-
#initialize(meta, textile, date, path = nil, template = nil) ⇒ Post
constructor
A new instance of Post.
- #path ⇒ Object
- #slug ⇒ Object
Constructor Details
#initialize(meta, textile, date, path = nil, template = nil) ⇒ Post
Returns a new instance of Post.
69 70 71 72 73 74 75 |
# File 'lib/post.rb', line 69 def initialize(, textile, date, path = nil, template = nil) @meta = @textile = textile @path = path @meta['date'] = date @meta['template'] = template end |
Class Attribute Details
.base_dir ⇒ Object
Returns the value of attribute base_dir.
8 9 10 |
# File 'lib/post.rb', line 8 def base_dir @base_dir end |
.glob ⇒ Object
Returns the value of attribute glob.
8 9 10 |
# File 'lib/post.rb', line 8 def glob @glob end |
Class Method Details
.categories ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/post.rb', line 20 def categories find.reduce({}) do |tag_score, post| (post[:tags] || []).each do |tag| tag_score[tag] ||= 0 tag_score[tag] += 1 end tag_score end.sort end |
.filenames ⇒ Object
10 11 12 |
# File 'lib/post.rb', line 10 def filenames Dir.glob File.join(base_dir, Post.glob) end |
.find ⇒ Object
34 35 36 37 38 |
# File 'lib/post.rb', line 34 def find filenames \ .map { |path| from_file(path) } \ .sort { |p1,p2| p2[:date] <=> p1[:date] } end |
.find_by_tag(tag) ⇒ Object
30 31 32 |
# File 'lib/post.rb', line 30 def find_by_tag(tag) find.select { |post| (post[:tags] || []).map { |t| t.downcase }.include? tag.downcase } end |
.find_latest ⇒ Object Also known as: find_single
14 15 16 |
# File 'lib/post.rb', line 14 def find_latest find.first end |
.from_file(path) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/post.rb', line 40 def from_file(path) date = Date.parse(File.basename(path)[0..9]) match = path.match(/posts\/(.+)\//) template = (match[1] + '_post').to_sym if match , textile = split_and_parse read_file_strip_bom(path) new , textile, date, path, template end |
.from_s(raw, date = nil) ⇒ Object
48 49 50 51 52 |
# File 'lib/post.rb', line 48 def from_s(raw, date = nil) date ||= Date.today , textile = split_and_parse raw new , textile, date end |
Instance Method Details
#[](property) ⇒ Object
77 78 79 |
# File 'lib/post.rb', line 77 def [](property) @meta[property.to_s] end |
#html ⇒ Object
81 82 83 84 85 |
# File 'lib/post.rb', line 81 def html r = RedCloth.new(@textile) r.extend VideoTag r.to_html end |
#path ⇒ Object
87 88 89 |
# File 'lib/post.rb', line 87 def path @path end |
#slug ⇒ Object
91 92 93 94 |
# File 'lib/post.rb', line 91 def slug return '#' unless @path File.basename(@path, '.textile').sub('-','/').sub('-','/').sub('-','/') end |