Class: Gutenug::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/gutenug/book.rb

Instance Method Summary collapse

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

#chaptersObject



32
33
34
# File 'lib/gutenug/book.rb', line 32

def chapters
  @chapters
end

#to_sObject



28
29
30
# File 'lib/gutenug/book.rb', line 28

def to_s
  @chapters.map(&:to_s).join("\n\n* * *\n\n")
end