Class: SimplePosts::Posts

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_posts/posts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePosts

Returns a new instance of Posts.



22
23
24
25
26
27
28
29
30
31
32
# 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

Instance Attribute Details

#non_blog_postsObject (readonly)

Returns the value of attribute non_blog_posts.



20
21
22
# File 'lib/simple_posts/posts.rb', line 20

def non_blog_posts
  @non_blog_posts
end

#postsObject (readonly)

Returns the value of attribute posts.



20
21
22
# File 'lib/simple_posts/posts.rb', line 20

def posts
  @posts
end

#rendererObject (readonly)

Returns the value of attribute renderer.



20
21
22
# File 'lib/simple_posts/posts.rb', line 20

def renderer
  @renderer
end

Instance Method Details

#blog_postsObject



137
138
139
# File 'lib/simple_posts/posts.rb', line 137

def blog_posts
  @blog_posts.sort { |a, b| a.date <=> b.date }
end

#eachObject



87
88
89
90
91
# File 'lib/simple_posts/posts.rb', line 87

def each
  @posts.each do |post|
    yield post
  end
end

#find(thing) ⇒ Object



71
72
73
# File 'lib/simple_posts/posts.rb', line 71

def find(thing)
  @posts_by_post_id[thing] || @posts_by_post_name[thing]
end

#find_all_filesObject



79
80
81
82
83
84
85
# 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) ⇒ Object



97
98
99
# 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_allObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/simple_posts/posts.rb', line 111

def related_posts(target)
  freqs = tag_frequencies

  highest_freq = freqs.values.max
  related_scores = Hash.new(0)

  blog_posts.each do |post|
    post.tags.each do |tag|
      if target.tags.include?(tag) && target != post
        tag_freq = freqs[tag]
        related_scores[post] += (1 + highest_freq - tag_freq)
      end
    end
  end

  related_scores.sort do |a,b|
   if a[1] < b[1]
        1
      elsif a[1] > b[1]
        -1
      else
        b[0].date <=> a[0].date
      end
  end.select{|post,freq| freq > 1}.collect {|post,freq| post}
end

#setup_rendererObject



34
35
36
37
# File 'lib/simple_posts/posts.rb', line 34

def setup_renderer
  @renderer = Redcarpet::Markdown.new(
    HTMLwithHighlights, :fenced_code_blocks => true)
end

#tag_frequenciesObject



101
102
103
104
105
106
107
108
109
# 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) ⇒ Object



75
76
77
# File 'lib/simple_posts/posts.rb', line 75

def tagged(tag)
  (@posts_by_tag[tag.downcase] || [])
end

#topicsObject



93
94
95
# File 'lib/simple_posts/posts.rb', line 93

def topics
  @posts_by_topic.keys.sort
end