Class: MdRecord::Base
- Inherits:
-
Object
- Object
- MdRecord::Base
- Includes:
- ActiveModel::Model
- Defined in:
- lib/md_record/base.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#description ⇒ Object
Returns the value of attribute description.
-
#position ⇒ Object
Returns the value of attribute position.
-
#published ⇒ Object
Returns the value of attribute published.
-
#published_at ⇒ Object
Returns the value of attribute published_at.
-
#slug ⇒ Object
Returns the value of attribute slug.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
- .all ⇒ Object
- .find(slug) ⇒ Object
- .loader ⇒ Object
- .md_record_path(path = nil) ⇒ Object
- .parse(file) ⇒ Object
- .published ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
7 8 9 |
# File 'lib/md_record/base.rb', line 7 def content @content end |
#description ⇒ Object
Returns the value of attribute description.
7 8 9 |
# File 'lib/md_record/base.rb', line 7 def description @description end |
#position ⇒ Object
Returns the value of attribute position.
7 8 9 |
# File 'lib/md_record/base.rb', line 7 def position @position end |
#published ⇒ Object
Returns the value of attribute published.
7 8 9 |
# File 'lib/md_record/base.rb', line 7 def published @published end |
#published_at ⇒ Object
Returns the value of attribute published_at.
7 8 9 |
# File 'lib/md_record/base.rb', line 7 def published_at @published_at end |
#slug ⇒ Object
Returns the value of attribute slug.
7 8 9 |
# File 'lib/md_record/base.rb', line 7 def slug @slug end |
#title ⇒ Object
Returns the value of attribute title.
7 8 9 |
# File 'lib/md_record/base.rb', line 7 def title @title end |
Class Method Details
.all ⇒ Object
34 35 36 |
# File 'lib/md_record/base.rb', line 34 def all loader.load { |file| parse(file) } end |
.find(slug) ⇒ Object
42 43 44 45 |
# File 'lib/md_record/base.rb', line 42 def find(slug) published.find { |doc| doc.slug == slug } || raise(RecordNotFound.new(model: name, slug: slug)) end |
.loader ⇒ Object
16 17 18 |
# File 'lib/md_record/base.rb', line 16 def loader MdRecord::Loader.new(path: resolve_path(md_record_path)) end |
.md_record_path(path = nil) ⇒ Object
11 12 13 14 |
# File 'lib/md_record/base.rb', line 11 def md_record_path(path = nil) @md_record_path = path if path @md_record_path || default_path end |
.parse(file) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/md_record/base.rb', line 20 def parse(file) parser = MdRecord::FrontmatterParser.new(file).parse! new( slug: parser.slug, title: parser.title, content: parser.content, published: parser.published, published_at: parser.frontmatter["published_at"], description: parser.frontmatter["description"], position: parser.frontmatter.fetch("position", 0) ) end |
.published ⇒ Object
38 39 40 |
# File 'lib/md_record/base.rb', line 38 def published @published ||= all.select(&:published?).sort_by(&:published_at).reverse end |
Instance Method Details
#published? ⇒ Boolean
62 63 64 |
# File 'lib/md_record/base.rb', line 62 def published? published end |
#to_html ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/md_record/base.rb', line 70 def to_html = { autolink: true, tables: true, fenced_code_blocks: true, strikethrough: true, no_intra_emphasis: true } renderer = MdRecord::MarkdownRenderer.new(hard_wrap: true) markdown = Redcarpet::Markdown.new(renderer, ) result = markdown.render(content) if result.respond_to?(:html_safe) result.html_safe else result end end |
#to_param ⇒ Object
66 67 68 |
# File 'lib/md_record/base.rb', line 66 def to_param slug end |