Class: Hobostove::Line
- Inherits:
-
Object
- Object
- Hobostove::Line
- Defined in:
- lib/hobostove/line.rb
Defined Under Namespace
Classes: LineSegment
Instance Attribute Summary collapse
-
#segments ⇒ Object
readonly
Returns the value of attribute segments.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #add(color, body) ⇒ Object
- #first(count) ⇒ Object
-
#initialize(segments = []) ⇒ Line
constructor
A new instance of Line.
- #length ⇒ Object
- #present? ⇒ Boolean
- #segment(count) ⇒ Object
- #split(width) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(segments = []) ⇒ Line
Returns a new instance of Line.
7 8 9 |
# File 'lib/hobostove/line.rb', line 7 def initialize(segments = []) @segments = segments end |
Instance Attribute Details
#segments ⇒ Object (readonly)
Returns the value of attribute segments.
5 6 7 |
# File 'lib/hobostove/line.rb', line 5 def segments @segments end |
Instance Method Details
#<=>(other) ⇒ Object
43 44 45 |
# File 'lib/hobostove/line.rb', line 43 def <=>(other) to_s <=> other.to_s end |
#add(color, body) ⇒ Object
39 40 41 |
# File 'lib/hobostove/line.rb', line 39 def add(color, body) segments << LineSegment.new(color, body) end |
#first(count) ⇒ Object
23 24 25 |
# File 'lib/hobostove/line.rb', line 23 def first(count) segment(count).first end |
#length ⇒ Object
15 16 17 |
# File 'lib/hobostove/line.rb', line 15 def length to_s.length end |
#present? ⇒ Boolean
19 20 21 |
# File 'lib/hobostove/line.rb', line 19 def present? to_s.present? end |
#segment(count) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hobostove/line.rb', line 47 def segment(count) first_line = Line.new second_line = Line.new segments.inject(0) do |current_count, segment| if current_count > count second_line.add(segment.color, segment.body) next end current_count += segment.body.length if current_count <= count first_line.add(segment.color, segment.body) else count = segment.body.length - (current_count - count) first_line.add(segment.color, segment.body.first(count)) second_line.add(segment.color, segment.body[count..-1]) end current_count end [first_line, second_line] end |
#split(width) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/hobostove/line.rb', line 27 def split(width) lines = [] line = self begin first, second = line.send(:segment, width) lines << first line = second end while second.length > 0 lines end |
#to_s ⇒ Object
11 12 13 |
# File 'lib/hobostove/line.rb', line 11 def to_s @segments.map(&:body).join end |