Class: Karenina::Book

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_lines = 5) ⇒ Book

Returns a new instance of Book.



8
9
10
11
12
13
# File 'lib/karenina/book.rb', line 8

def initialize(context_lines = 5)
  @context_lines = context_lines
  file  = File.open(Karenina::TEXT_PATH, 'r')
  @lines = file.readlines
  @mark = BookMark.new
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



4
5
6
# File 'lib/karenina/book.rb', line 4

def lines
  @lines
end

#markObject (readonly)

Returns the value of attribute mark.



5
6
7
# File 'lib/karenina/book.rb', line 5

def mark
  @mark
end

Instance Method Details

#bold_line(text) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/karenina/book.rb', line 44

def bold_line(text)
      separator = ""
      80.times { separator << "-"}
      puts separator
      puts text.white.bold
      puts separator
end

#current_lineObject



15
16
17
# File 'lib/karenina/book.rb', line 15

def current_line
    @lines[@mark.line].to_s
end

#current_line_is_blank?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/karenina/book.rb', line 19

def current_line_is_blank?
    current_line =~ /^\s+$/
end


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/karenina/book.rb', line 29

def print()
  skip_blank
  current_line = @mark.line
  first = current_line - @context_lines > 0 ? current_line - @context_lines : 0
  last  = current_line + @context_lines
  first.upto(last) {|line|
    text = lines[line].to_s
    if line == current_line
      bold_line(text)
    else
      puts text.white
    end
  }
end

#skip_blankObject



23
24
25
26
27
# File 'lib/karenina/book.rb', line 23

def skip_blank
    while current_line_is_blank?
      @mark.increment
    end
end