Class: Blogue::Post
- Inherits:
-
Object
- Object
- Blogue::Post
- Defined in:
- app/models/blogue/post.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .all ⇒ Object
- .all_paths_in_posts_dir ⇒ Object
- .all_post_paths ⇒ Object
- .cache_key ⇒ Object
- .extract_post_id(path) ⇒ Object
- .find(id) ⇒ Object
- .load_meta(text) ⇒ Object
- .post_file?(path) ⇒ Boolean
- .sort_posts(post_a, post_b) ⇒ Object
Instance Method Summary collapse
- #author_name ⇒ Object
- #body ⇒ Object
- #cache_key ⇒ Object
- #date ⇒ Object
- #id ⇒ Object
-
#initialize(path) ⇒ Post
constructor
A new instance of Post.
- #meta ⇒ Object
- #private? ⇒ Boolean
- #time ⇒ Object
- #title ⇒ Object
- #tldr ⇒ Object
- #to_helper_name ⇒ Object
- #to_partial_path ⇒ Object
Constructor Details
#initialize(path) ⇒ Post
Returns a new instance of Post.
51 52 53 |
# File 'app/models/blogue/post.rb', line 51 def initialize(path) @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
49 50 51 |
# File 'app/models/blogue/post.rb', line 49 def path @path end |
Class Method Details
.all ⇒ Object
8 9 10 11 12 13 14 |
# File 'app/models/blogue/post.rb', line 8 def all posts = all_post_paths. map(&method(:new)). sort(&method(:sort_posts)) Rails.env.production? ? posts.reject(&:private?) : posts end |
.all_paths_in_posts_dir ⇒ Object
20 21 22 |
# File 'app/models/blogue/post.rb', line 20 def all_paths_in_posts_dir Dir["#{Blogue.posts_path}/*"] end |
.all_post_paths ⇒ Object
16 17 18 |
# File 'app/models/blogue/post.rb', line 16 def all_post_paths all_paths_in_posts_dir.select(&method(:post_file?)) end |
.cache_key ⇒ Object
44 45 46 |
# File 'app/models/blogue/post.rb', line 44 def cache_key "blogue/#{Blogue.blanket_checksum}" end |
.extract_post_id(path) ⇒ Object
28 29 30 |
# File 'app/models/blogue/post.rb', line 28 def extract_post_id(path) File.basename(path)[/[^\.]+/] end |
.find(id) ⇒ Object
4 5 6 |
# File 'app/models/blogue/post.rb', line 4 def find(id) all.find{ |p| p.id == id } end |
.load_meta(text) ⇒ Object
36 37 38 39 40 41 42 |
# File 'app/models/blogue/post.rb', line 36 def (text) YAML.load( text.lines.select do |line| !!(line[/^<!--\s*meta/]..line[/--!>$/]) end[1..-2].try(:join, "\n") || '' ) || {} end |
.post_file?(path) ⇒ Boolean
24 25 26 |
# File 'app/models/blogue/post.rb', line 24 def post_file?(path) File.file?(path) && !File.basename(path).starts_with?('.') end |
.sort_posts(post_a, post_b) ⇒ Object
32 33 34 |
# File 'app/models/blogue/post.rb', line 32 def sort_posts(post_a, post_b) post_b.time <=> post_a.time end |
Instance Method Details
#author_name ⇒ Object
79 80 81 |
# File 'app/models/blogue/post.rb', line 79 def || || end |
#body ⇒ Object
71 72 73 |
# File 'app/models/blogue/post.rb', line 71 def body File.read(path) end |
#cache_key ⇒ Object
91 92 93 |
# File 'app/models/blogue/post.rb', line 91 def cache_key "blogue/posts/#{id}/#{Blogue.checksums[id]}" end |
#date ⇒ Object
59 60 61 |
# File 'app/models/blogue/post.rb', line 59 def date time.try(:to_date) end |
#id ⇒ Object
55 56 57 |
# File 'app/models/blogue/post.rb', line 55 def id self.class.extract_post_id(path) end |
#meta ⇒ Object
87 88 89 |
# File 'app/models/blogue/post.rb', line 87 def self.class.(body) end |
#private? ⇒ Boolean
83 84 85 |
# File 'app/models/blogue/post.rb', line 83 def private? filename_with_underscore? || end |
#time ⇒ Object
63 64 65 |
# File 'app/models/blogue/post.rb', line 63 def time || file_ctime end |
#title ⇒ Object
67 68 69 |
# File 'app/models/blogue/post.rb', line 67 def title || parsed_title || filename_title end |
#tldr ⇒ Object
75 76 77 |
# File 'app/models/blogue/post.rb', line 75 def tldr ['tldr'] end |
#to_helper_name ⇒ Object
99 100 101 |
# File 'app/models/blogue/post.rb', line 99 def to_helper_name id.underscore end |
#to_partial_path ⇒ Object
95 96 97 |
# File 'app/models/blogue/post.rb', line 95 def to_partial_path 'posts/post' end |