class SimplePosts::Posts

Attributes

non_blog_posts[R]
posts[R]
renderer[R]

Public Class Methods

new() click to toggle source
# File lib/simple_posts/posts.rb, line 22
def initialize
  @posts_by_post_name = {}
  @posts_by_post_id = {}
  @posts_by_tag = {}
  @posts_by_topic = {}
  @blog_posts = []
  @non_blog_posts = []

  setup_renderer
  parse_all
end

Public Instance Methods

blog_posts() click to toggle source
# File lib/simple_posts/posts.rb, line 137
def blog_posts
  @blog_posts.sort { |a, b| a.date <=> b.date }
end
each() { |post| ... } click to toggle source
# File lib/simple_posts/posts.rb, line 87
def each
  @posts.each do |post|
    yield post
  end
end
find(thing) click to toggle source
# File lib/simple_posts/posts.rb, line 71
def find(thing)
  @posts_by_post_id[thing] || @posts_by_post_name[thing]
end
find_all_files() click to toggle source
# File lib/simple_posts/posts.rb, line 79
def find_all_files
  basepath = Rails.root.join('app', 'posts').to_s
  Dir.glob(File.join(basepath, "**/*")).map do |fullpath|
    next if File.directory?(fullpath)
    fullpath.gsub(basepath + "/", '')
  end.compact
end
for_topic(topic) click to toggle source
# File lib/simple_posts/posts.rb, line 97
def for_topic(topic)
  @posts_by_topic[topic].sort { |a,b| b.date <=> a.date }
end
parse_all() click to toggle source
# File lib/simple_posts/posts.rb, line 39
def parse_all
  @posts = find_all_files.map do |post|
    next if File.basename(post).start_with?('_')
    Post.new(post, @renderer)
  end.compact

  @posts.each do |post|
    @posts_by_post_name[post.name] = post
    @posts_by_post_id[post.post_id] = post
    if post.topic
      topic = post.topic.downcase
      @posts_by_topic[topic] ||= []
      @posts_by_topic[topic] << post
    end

    post.alternate_links.each do |link|
      @posts_by_post_id[link] = post
    end

    post.tags.each do |tag|
      @posts_by_tag[tag.downcase] ||= []
      @posts_by_tag[tag.downcase] << post
    end

    if post.is_blog_post?
      @blog_posts << post
    else
      @non_blog_posts << post
    end
  end
end
setup_renderer() click to toggle source
# File lib/simple_posts/posts.rb, line 34
def setup_renderer
  @renderer = Redcarpet::Markdown.new(
    HTMLwithHighlights, :fenced_code_blocks => true)
end
tag_frequencies() click to toggle source
# File lib/simple_posts/posts.rb, line 101
def tag_frequencies
  tags = Hash.new(0)
  @posts.each do |post|
    post.tags.each do |tag|
      tags[tag] += 1
    end
  end
  tags
end
tagged(tag) click to toggle source
# File lib/simple_posts/posts.rb, line 75
def tagged(tag)
  (@posts_by_tag[tag.downcase] || [])
end
topics() click to toggle source
# File lib/simple_posts/posts.rb, line 93
def topics
  @posts_by_topic.keys.sort
end