Class: Ramble::BlogPost
- Inherits:
-
Object
- Object
- Ramble::BlogPost
- Defined in:
- lib/ramble/blog_post.rb,
lib/ramble/blog_post/markdown_parser.rb
Defined Under Namespace
Classes: MarkdownParser, NotFound
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#preview ⇒ Object
readonly
Returns the value of attribute preview.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
-
.all(options = { sort_by: :written_on, asc: true }) ⇒ Object
TODO: Refactor this shit to be better.
- .find_by_slug(slug) ⇒ Object
Instance Method Summary collapse
-
#initialize(file) ⇒ BlogPost
constructor
A new instance of BlogPost.
- #slug ⇒ Object
- #written_on ⇒ Object
Constructor Details
#initialize(file) ⇒ BlogPost
Returns a new instance of BlogPost.
34 35 36 37 38 39 |
# 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 |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/ramble/blog_post.rb', line 7 def body @body end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
7 8 9 |
# File 'lib/ramble/blog_post.rb', line 7 def file @file end |
#preview ⇒ Object (readonly)
Returns the value of attribute preview.
7 8 9 |
# File 'lib/ramble/blog_post.rb', line 7 def preview @preview end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
7 8 9 |
# File 'lib/ramble/blog_post.rb', line 7 def title @title end |
Class Method Details
.all(options = { sort_by: :written_on, asc: true }) ⇒ Object
TODO: Refactor this shit to be better. Currently 47.8 flog score.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ramble/blog_post.rb', line 11 def all( = { 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 [:asc] a.send([:sort_by]) <=> b.send([:sort_by]) else b.send([:sort_by]) <=> a.send([:sort_by]) end end end |
Instance Method Details
#slug ⇒ Object
41 42 43 44 |
# File 'lib/ramble/blog_post.rb', line 41 def slug underscored_slug = file_name.match(/\d+_(.*)/)[1] underscored_slug.gsub("_", "-") end |
#written_on ⇒ Object
46 47 48 49 |
# File 'lib/ramble/blog_post.rb', line 46 def written_on = file_name.match(/(\d+)_/)[1] Date.parse() end |