Class: MdRecord::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/md_record/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject

Returns the value of attribute content.



7
8
9
# File 'lib/md_record/base.rb', line 7

def content
  @content
end

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/md_record/base.rb', line 7

def description
  @description
end

#positionObject

Returns the value of attribute position.



7
8
9
# File 'lib/md_record/base.rb', line 7

def position
  @position
end

#publishedObject

Returns the value of attribute published.



7
8
9
# File 'lib/md_record/base.rb', line 7

def published
  @published
end

#published_atObject

Returns the value of attribute published_at.



7
8
9
# File 'lib/md_record/base.rb', line 7

def published_at
  @published_at
end

#slugObject

Returns the value of attribute slug.



7
8
9
# File 'lib/md_record/base.rb', line 7

def slug
  @slug
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/md_record/base.rb', line 7

def title
  @title
end

Class Method Details

.allObject



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

.loaderObject



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

.publishedObject



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

Returns:

  • (Boolean)


62
63
64
# File 'lib/md_record/base.rb', line 62

def published?
  published
end

#to_htmlObject



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
  options = {
    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, options)
  result = markdown.render(content)

  if result.respond_to?(:html_safe)
    result.html_safe
  else
    result
  end
end

#to_paramObject



66
67
68
# File 'lib/md_record/base.rb', line 66

def to_param
  slug
end