Class: Ramble::BlogPost

Inherits:
Object
  • Object
show all
Defined in:
lib/ramble/blog_post.rb,
lib/ramble/blog_post/markdown_parser.rb

Defined Under Namespace

Classes: MarkdownParser, NotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/ramble/blog_post.rb', line 7

def body
  @body
end

#fileObject (readonly)

Returns the value of attribute file.



7
8
9
# File 'lib/ramble/blog_post.rb', line 7

def file
  @file
end

#previewObject (readonly)

Returns the value of attribute preview.



7
8
9
# File 'lib/ramble/blog_post.rb', line 7

def preview
  @preview
end

#titleObject (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(options = { 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 options[:asc]
      a.send(options[:sort_by]) <=> b.send(options[:sort_by])
    else
      b.send(options[:sort_by]) <=> a.send(options[:sort_by])
    end
  end
end

.find_by_slug(slug) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/ramble/blog_post.rb', line 23

def find_by_slug(slug)
  post = all.detect { |blog_post| blog_post.slug == slug }

  unless post
    raise Ramble::BlogPost::NotFound
  end

  post
end

Instance Method Details

#slugObject



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_onObject



46
47
48
49
# File 'lib/ramble/blog_post.rb', line 46

def written_on
  timestamp = file_name.match(/(\d+)_/)[1]
  Date.parse(timestamp)
end