Class: Peregrin::Chapter
- Inherits:
-
Object
- Object
- Peregrin::Chapter
- 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
-
#children ⇒ Object
Returns the value of attribute children.
-
#position ⇒ Object
Returns the value of attribute position.
-
#src ⇒ Object
Returns the value of attribute src.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #add_child(child_title, child_pos, child_src = nil) ⇒ Object
-
#empty_leaf? ⇒ Boolean
A chapter is an empty leaf if you can’t link to it or any of its children.
-
#initialize(title, pos, src = nil) ⇒ Chapter
constructor
A new instance of Chapter.
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
#children ⇒ Object
Returns the value of attribute children.
7 8 9 |
# File 'lib/peregrin/chapter.rb', line 7 def children @children end |
#position ⇒ Object
Returns the value of attribute position.
7 8 9 |
# File 'lib/peregrin/chapter.rb', line 7 def position @position end |
#src ⇒ Object
Returns the value of attribute src.
7 8 9 |
# File 'lib/peregrin/chapter.rb', line 7 def src @src end |
#title ⇒ Object
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.
27 28 29 |
# File 'lib/peregrin/chapter.rb', line 27 def empty_leaf? src.nil? && children.all? { |ch| ch.empty_leaf? } end |