Class: BookLab::Toc::Content

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/booklab/toc/content.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ Content

Returns a new instance of Content.



52
53
54
# File 'lib/booklab/toc/content.rb', line 52

def initialize(items)
  @items = items
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



6
7
8
# File 'lib/booklab/toc/content.rb', line 6

def items
  @items
end

Instance Method Details

#_dumpObject



10
11
12
# File 'lib/booklab/toc/content.rb', line 10

def _dump
  YAML.dump(items.as_json)
end

#_render(format: :html, prefix: nil, suffix: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/booklab/toc/content.rb', line 30

def _render(format: :html, prefix: nil, suffix: nil)
  render_items = []
  items.each do |item|
    new_item = item.dup

    new_item.slug = new_item.url
    if new_item.url && !new_item.slug.include?("/")
      new_item.url = "#{prefix.to_s}#{new_item.slug}#{suffix.to_s}"
    end
    render_items << new_item
  end

  ApplicationController.renderer.render(partial: "booklab/toc/content", locals: {
    format: format,
    items: render_items,
  })
end

#find_by_url(url) ⇒ Object



48
49
50
# File 'lib/booklab/toc/content.rb', line 48

def find_by_url(url)
  items.find { |item| item.url&.strip == url&.strip }
end

#to_html(prefix: nil, suffix: nil) ⇒ Object



18
19
20
# File 'lib/booklab/toc/content.rb', line 18

def to_html(prefix: nil, suffix: nil)
  _render(format: :html, prefix: prefix, suffix: suffix)
end

#to_jsonObject



26
27
28
# File 'lib/booklab/toc/content.rb', line 26

def to_json
  items.to_json
end

#to_markdown(prefix: nil, suffix: nil) ⇒ Object



22
23
24
# File 'lib/booklab/toc/content.rb', line 22

def to_markdown(prefix: nil, suffix: nil)
  _render(format: :markdown, prefix: prefix, suffix: suffix)
end

#to_yamlObject



14
15
16
# File 'lib/booklab/toc/content.rb', line 14

def to_yaml
  _dump
end