Class: Vedeu::Editor::Lines
- Inherits:
-
Object
- Object
- Vedeu::Editor::Lines
- Includes:
- Enumerable
- Defined in:
- lib/vedeu/editor/lines.rb
Overview
Manipulate the lines of an Vedeu::Editor::Document.
Instance Attribute Summary collapse
Class Method Summary collapse
-
.coerce(document) ⇒ Vedeu::Editor::Lines
Coerce a document into a new instance of Vedeu::Editor::Lines.
Instance Method Summary collapse
-
#[](index) ⇒ Vedeu::Editor::Line
Return a line or collection of lines (if index is a Range).
-
#delete_character(y, x) ⇒ Vedeu::Editor::Lines
Deletes the character from the line where the cursor is currently positioned.
-
#delete_line(index = nil) ⇒ String
Delete the line from the lines positioned at the given index.
-
#each(&block) ⇒ Enumerator
Provides iteration over the collection.
-
#empty? ⇒ Boolean
Returns a boolean indicating whether there are lines.
-
#eql?(other) ⇒ Boolean
(also: #==)
An object is equal when its values are the same.
-
#initialize(lines = nil) ⇒ Vedeu::Editor::Lines
constructor
Returns a new instance of Vedeu::Editor::Lines.
-
#insert_character(character, y, x) ⇒ Vedeu::Editor::Lines
Insert a character in to a line.
-
#insert_line(line, index = nil) ⇒ Vedeu::Editor::Lines
Insert the line on the line below the given index.
-
#line(index = nil) ⇒ Vedeu::Editor::Line
Returns the line at the given index.
-
#size ⇒ Fixnum
Return the number of lines.
-
#to_s ⇒ String
Return the lines as a string.
Constructor Details
#initialize(lines = nil) ⇒ Vedeu::Editor::Lines
Returns a new instance of Vedeu::Editor::Lines.
56 57 58 |
# File 'lib/vedeu/editor/lines.rb', line 56 def initialize(lines = nil) @lines = lines || [] end |
Instance Attribute Details
#lines ⇒ String
13 14 15 |
# File 'lib/vedeu/editor/lines.rb', line 13 def lines @lines end |
Class Method Details
.coerce(document) ⇒ Vedeu::Editor::Lines
Coerce a document into a new instance of Vedeu::Editor::Lines.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/vedeu/editor/lines.rb', line 19 def self.coerce(document) if document.is_a?(self) new(document.lines) elsif document.is_a?(Array) lines = document.map do |line| if line.is_a?(Vedeu::Editor::Line) line elsif line.is_a?(String) Vedeu::Editor::Line.coerce(line) else Vedeu::Editor::Line.new end end new(lines) elsif document.is_a?(String) lines = document.lines.map(&:chomp).map do |line| Vedeu::Editor::Line.coerce(line) end new(lines) else new end end |
Instance Method Details
#[](index) ⇒ Vedeu::Editor::Line
Return a line or collection of lines (if index is a Range).
64 65 66 |
# File 'lib/vedeu/editor/lines.rb', line 64 def [](index) lines[index] end |
#delete_character(y, x) ⇒ Vedeu::Editor::Lines
Deletes the character from the line where the cursor is currently positioned.
74 75 76 77 |
# File 'lib/vedeu/editor/lines.rb', line 74 def delete_character(y, x) lines[y] = line(y).delete_character(x) Vedeu::Editor::Lines.coerce(lines) end |
#delete_line(index = nil) ⇒ String
Delete the line from the lines positioned at the given index.
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/vedeu/editor/lines.rb', line 83 def delete_line(index = nil) return self if lines.empty? || (index && index <= 0) new_lines = if index && index <= size lines.dup.tap { |lines| lines.slice!(index) } else lines.dup.tap(&:pop) end Vedeu::Editor::Lines.coerce(new_lines) end |
#each(&block) ⇒ Enumerator
Provides iteration over the collection.
101 102 103 |
# File 'lib/vedeu/editor/lines.rb', line 101 def each(&block) lines.each(&block) end |
#empty? ⇒ Boolean
Returns a boolean indicating whether there are lines.
108 109 110 |
# File 'lib/vedeu/editor/lines.rb', line 108 def empty? size == 0 end |
#eql?(other) ⇒ Boolean Also known as: ==
An object is equal when its values are the same.
116 117 118 |
# File 'lib/vedeu/editor/lines.rb', line 116 def eql?(other) self.class == other.class && lines == other.lines end |
#insert_character(character, y, x) ⇒ Vedeu::Editor::Lines
Insert a character in to a line.
127 128 129 130 |
# File 'lib/vedeu/editor/lines.rb', line 127 def insert_character(character, y, x) lines[y] = line(y).insert_character(character, x) Vedeu::Editor::Lines.coerce(lines) end |
#insert_line(line, index = nil) ⇒ Vedeu::Editor::Lines
Insert the line on the line below the given index.
137 138 139 140 141 142 143 |
# File 'lib/vedeu/editor/lines.rb', line 137 def insert_line(line, index = nil) return self unless line insert = Vedeu::Editor::Insert.into(lines, line, index, size) Vedeu::Editor::Lines.coerce(insert) end |
#line(index = nil) ⇒ Vedeu::Editor::Line
Returns the line at the given index.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/vedeu/editor/lines.rb', line 149 def line(index = nil) return Vedeu::Editor::Line.new unless lines return Vedeu::Editor::Line.coerce(lines.last) unless index indexed = if index <= 0 lines.first elsif index && index <= size lines[index] else lines.last end Vedeu::Editor::Line.coerce(indexed) end |
#size ⇒ Fixnum
Return the number of lines.
170 171 172 |
# File 'lib/vedeu/editor/lines.rb', line 170 def size lines.size end |
#to_s ⇒ String
Return the lines as a string.
177 178 179 |
# File 'lib/vedeu/editor/lines.rb', line 177 def to_s lines.map(&:to_s).join end |