Created
July 23, 2011 01:52
-
-
Save reu/1100846 to your computer and use it in GitHub Desktop.
Implementação do Enumerable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post | |
extend Enumerable | |
def self.files | |
Dir["posts/*.markdown"].sort_by { |file| File.stat(file).ctime }.reverse | |
end | |
def self.each | |
files.each do |entry| | |
File.open(entry) { |file| yield Post.new(file.read) } | |
end | |
end | |
def self.find_by_slug(slug) | |
find { |post| post.slug == slug } | |
end | |
def initialize(content) | |
@content = content | |
end | |
def title | |
@content.lines.first | |
end | |
def slug | |
title.to_slug.normalize | |
end | |
def to_html | |
Redcarpet.new(@content, :smart, :gh_blockcode).to_html | |
end | |
alias :to_s :to_html | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment