Class: Gutenug::Book
- Inherits:
-
Object
- Object
- Gutenug::Book
- Defined in:
- lib/gutenug/book.rb
Instance Method Summary collapse
- #chapters ⇒ Object
-
#initialize(blob) ⇒ Book
constructor
A new instance of Book.
- #to_s ⇒ Object
Constructor Details
#initialize(blob) ⇒ Book
Returns a new instance of Book.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gutenug/book.rb', line 3 def initialize(blob) paragraphs = [] buffer = [] blob.split("\n").map(&:strip).each do |line| if line.empty? paragraphs << Paragraph.new(buffer) buffer.clear else buffer << line end end paragraphs << Paragraph.new(buffer) unless buffer.empty? @chapters = [] candidates = [] paragraphs.chunk(&:status).each do |chunk| if chunk.first == :invalid _add_chapter(candidates) candidates.clear else candidates << chunk end end _add_chapter(candidates) end |
Instance Method Details
#chapters ⇒ Object
32 33 34 |
# File 'lib/gutenug/book.rb', line 32 def chapters @chapters end |
#to_s ⇒ Object
28 29 30 |
# File 'lib/gutenug/book.rb', line 28 def to_s @chapters.map(&:to_s).join("\n\n* * *\n\n") end |