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

- (Chapter) initialize(title, pos, src = nil)

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

- (Object) children

Returns the value of attribute children



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

def children
  @children
end

- (Object) position

Returns the value of attribute position



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

def position
  @position
end

- (Object) src

Returns the value of attribute src



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

def src
  @src
end

- (Object) title

Returns the value of attribute title



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

def title
  @title
end

Instance Method Details

- (Object) add_child(child_title, child_pos, child_src = nil)



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

- (Boolean) empty_leaf?

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