Class: Karenina::Book
- Inherits:
-
Object
- Object
- Karenina::Book
- Defined in:
- lib/karenina/book.rb
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#mark ⇒ Object
readonly
Returns the value of attribute mark.
Instance Method Summary collapse
- #bold_line(text) ⇒ Object
- #current_line ⇒ Object
- #current_line_is_blank? ⇒ Boolean
- #current_line_num ⇒ Object
- #first_line_num ⇒ Object
-
#initialize(context_lines = 5) ⇒ Book
constructor
A new instance of Book.
- #last_line_num ⇒ Object
- #print ⇒ Object
- #printable_line_nums ⇒ Object
- #skip_blank ⇒ Object
- #to_str ⇒ Object
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
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
4 5 6 |
# File 'lib/karenina/book.rb', line 4 def lines @lines end |
#mark ⇒ Object (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
68 69 70 71 72 73 74 75 76 |
# File 'lib/karenina/book.rb', line 68 def bold_line(text) output = [] separator = "" 80.times { separator << "-"} output << separator output << text output << separator output.join("\n") end |
#current_line ⇒ Object
15 16 17 |
# File 'lib/karenina/book.rb', line 15 def current_line @lines[@mark.line].to_s end |
#current_line_is_blank? ⇒ Boolean
19 20 21 |
# File 'lib/karenina/book.rb', line 19 def current_line_is_blank? current_line =~ /^\s+$/ end |
#current_line_num ⇒ Object
37 38 39 |
# File 'lib/karenina/book.rb', line 37 def current_line_num @mark.line end |
#first_line_num ⇒ Object
29 30 31 |
# File 'lib/karenina/book.rb', line 29 def first_line_num current_line_num - @context_lines > 0 ? current_line_num - @context_lines : 0 end |
#last_line_num ⇒ Object
33 34 35 |
# File 'lib/karenina/book.rb', line 33 def last_line_num current_line_num + @context_lines end |
#print ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/karenina/book.rb', line 41 def print output = [] skip_blank printable_line_nums.each {|line| text = @lines[line].to_s.chomp if line == current_line_num output << "#{bold_line(text)}" else output << "#{text}" end } output.join("\n") + "\n" end |
#printable_line_nums ⇒ Object
64 65 66 |
# File 'lib/karenina/book.rb', line 64 def printable_line_nums first_line_num.upto(last_line_num) end |
#skip_blank ⇒ Object
23 24 25 26 27 |
# File 'lib/karenina/book.rb', line 23 def skip_blank while current_line_is_blank? @mark.increment end end |
#to_str ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/karenina/book.rb', line 55 def to_str output = [] printable_line_nums.each {|line| text = @lines[line].to_s.chomp output << "#{text}" } output.join("\n") + "\n" end |