Class: Peregrin::Chapter

Inherits:
Object
  • Object
show all
Defined in:
lib/peregrin/chapter.rb

Overview

Books have nested sections with headings - each of these is a chapter.

TODO: flag whether a chapter is linkable?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, pos, src = nil) ⇒ Chapter

Returns a new instance of Chapter.



9
10
11
12
13
14
# File 'lib/peregrin/chapter.rb', line 9

def initialize(title, pos, src = nil)
  @title = title.gsub(/[\r\n]/,' ')  if title
  @src = src
  @position = pos.to_i
  @children = []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



7
8
9
# File 'lib/peregrin/chapter.rb', line 7

def children
  @children
end

#positionObject

Returns the value of attribute position.



7
8
9
# File 'lib/peregrin/chapter.rb', line 7

def position
  @position
end

#srcObject

Returns the value of attribute src.



7
8
9
# File 'lib/peregrin/chapter.rb', line 7

def src
  @src
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/peregrin/chapter.rb', line 7

def title
  @title
end

Instance Method Details

#add_child(child_title, child_pos, child_src = nil) ⇒ Object



17
18
19
20
21
# File 'lib/peregrin/chapter.rb', line 17

def add_child(child_title, child_pos, child_src = nil)
  chp = Peregrin::Chapter.new(child_title, child_pos, child_src)
  children.push(chp)
  chp
end

#empty_leaf?Boolean

A chapter is an empty leaf if you can’t link to it or any of its children. Typically you wouldn’t show an empty-leaf chapter in a Table of Contents.

Returns:

  • (Boolean)


27
28
29
# File 'lib/peregrin/chapter.rb', line 27

def empty_leaf?
  src.nil? && children.all? { |ch| ch.empty_leaf? }
end