class Ramble::BlogPost

Attributes

body[R]
file[R]
preview[R]
title[R]

Public Class Methods

all(options = { sort_by: :written_on, asc: true }) click to toggle source

TODO: Refactor this shit to be better. Currently 47.8 flog score.

# File lib/ramble/blog_post.rb, line 11
def all(options = { sort_by: :written_on, asc: true })
  Dir.glob("app/blog_posts/*").map do |file_path|
    new(File.open(file_path))
  end.sort do |a, b|
    if options[:asc]
      a.send(options[:sort_by]) <=> b.send(options[:sort_by])
    else
      b.send(options[:sort_by]) <=> a.send(options[:sort_by])
    end
  end
end
find_by_slug(slug) click to toggle source
# File lib/ramble/blog_post.rb, line 23
def find_by_slug(slug)
  post = all.detect { |blog_post| blog_post.slug == slug }

  unless post
    raise Ramble::BlogPost::NotFound
  end

  post
end
new(file) click to toggle source
# File lib/ramble/blog_post.rb, line 34
def initialize(file)
  @file = file
  @title = parsed_markdown.title
  @body = parsed_markdown.body
  @preview = parsed_markdown.preview
end

Public Instance Methods

slug() click to toggle source
# File lib/ramble/blog_post.rb, line 41
def slug
  underscored_slug = file_name.match(/\d+_(.*)/)[1]
  underscored_slug.gsub("_", "-")
end
written_on() click to toggle source
# File lib/ramble/blog_post.rb, line 46
def written_on
  timestamp = file_name.match(/(\d+)_/)[1]
  Date.parse(timestamp)
end

Private Instance Methods

file_name() click to toggle source
# File lib/ramble/blog_post.rb, line 53
def file_name
  @file_name ||= File.basename(file, ".md")
end
parsed_markdown() click to toggle source
# File lib/ramble/blog_post.rb, line 57
def parsed_markdown
  @parsed_markdown ||= BlogPost::MarkdownParser.new(file.read)
end