Class: SimplePosts::Posts
- Inherits:
-
Object
- Object
- SimplePosts::Posts
- Defined in:
- lib/simple_posts/posts.rb
Instance Attribute Summary collapse
-
#non_blog_posts ⇒ Object
readonly
Returns the value of attribute non_blog_posts.
-
#posts ⇒ Object
readonly
Returns the value of attribute posts.
-
#renderer ⇒ Object
readonly
Returns the value of attribute renderer.
Instance Method Summary collapse
- #blog_posts ⇒ Object
- #each ⇒ Object
- #find(thing) ⇒ Object
- #find_all_files ⇒ Object
- #for_topic(topic) ⇒ Object
-
#initialize ⇒ Posts
constructor
A new instance of Posts.
- #parse_all ⇒ Object
- #related_posts(target) ⇒ Object
- #setup_renderer ⇒ Object
- #tag_frequencies ⇒ Object
- #tagged(tag) ⇒ Object
- #topics ⇒ Object
Constructor Details
#initialize ⇒ Posts
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_posts ⇒ Object (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 |
#posts ⇒ Object (readonly)
Returns the value of attribute posts.
20 21 22 |
# File 'lib/simple_posts/posts.rb', line 20 def posts @posts end |
#renderer ⇒ Object (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_posts ⇒ Object
137 138 139 |
# File 'lib/simple_posts/posts.rb', line 137 def blog_posts @blog_posts.sort { |a, b| a.date <=> b.date } end |
#each ⇒ Object
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_files ⇒ Object
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_all ⇒ Object
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..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 |
#related_posts(target) ⇒ Object
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 (target) freqs = tag_frequencies highest_freq = freqs.values.max = Hash.new(0) blog_posts.each do |post| post..each do |tag| if target..include?(tag) && target != post tag_freq = freqs[tag] [post] += (1 + highest_freq - tag_freq) end end end .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_renderer ⇒ Object
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_frequencies ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/simple_posts/posts.rb', line 101 def tag_frequencies = Hash.new(0) @posts.each do |post| post..each do |tag| [tag] += 1 end end end |
#tagged(tag) ⇒ Object
75 76 77 |
# File 'lib/simple_posts/posts.rb', line 75 def tagged(tag) (@posts_by_tag[tag.downcase] || []) end |
#topics ⇒ Object
93 94 95 |
# File 'lib/simple_posts/posts.rb', line 93 def topics @posts_by_topic.keys.sort end |