Class: Vedeu::Editor::Line
- Inherits:
-
Object
- Object
- Vedeu::Editor::Line
- Defined in:
- lib/vedeu/editor/line.rb
Overview
Manipulate a single line of an Vedeu::Editor::Document.
Instance Attribute Summary collapse
- #line ⇒ String (also: #to_s)
Class Method Summary collapse
-
.coerce(line) ⇒ Vedeu::Editor::Line
Coerce a line into a new instance of Vedeu::Editor::Line.
Instance Method Summary collapse
-
#[](index) ⇒ String
Return a character or collection of characters (if index is a Range).
-
#character(index = nil) ⇒ String|NilClass
Return the character from the line positioned at the given index.
-
#delete_character(index = nil) ⇒ String
Delete the character from the line positioned at the given index.
-
#empty? ⇒ Boolean
Returns a boolean indicating whether there are characters on this line.
-
#eql?(other) ⇒ Boolean
(also: #==)
An object is equal when its values are the same.
-
#initialize(line = nil) ⇒ Vedeu::Editor::Line
constructor
Returns a new instance of Vedeu::Editor::Line.
-
#insert_character(character, index = nil) ⇒ Vedeu::Editor::Line
Insert the character on the line positioned at the given index.
-
#size ⇒ Fixnum
Return the size of the line in characters.
-
#to_chars ⇒ Array<Vedeu::Views::Char>
Return the line as a collection of Vedeu::Views::Char objects.
Constructor Details
#initialize(line = nil) ⇒ Vedeu::Editor::Line
Returns a new instance of Vedeu::Editor::Line.
34 35 36 |
# File 'lib/vedeu/editor/line.rb', line 34 def initialize(line = nil) @line = line || '' end |
Instance Attribute Details
#line ⇒ String Also known as: to_s
11 12 13 |
# File 'lib/vedeu/editor/line.rb', line 11 def line @line end |
Class Method Details
.coerce(line) ⇒ Vedeu::Editor::Line
Coerce a line into a new instance of Vedeu::Editor::Line.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/vedeu/editor/line.rb', line 18 def self.coerce(line) return line if line.is_a?(self) if line.is_a?(String) new(line) else new end end |
Instance Method Details
#[](index) ⇒ String
Return a character or collection of characters (if index is a Range).
43 44 45 |
# File 'lib/vedeu/editor/line.rb', line 43 def [](index) line[index] end |
#character(index = nil) ⇒ String|NilClass
Return the character from the line positioned at the given index.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/vedeu/editor/line.rb', line 52 def character(index = nil) return '' if line && line.empty? return line[-1] unless index if index <= 0 line[0] elsif index && index <= size line[index] else line[-1] end end |
#delete_character(index = nil) ⇒ String
Delete the character from the line positioned at the given index.
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/vedeu/editor/line.rb', line 73 def delete_character(index = nil) return self if line.empty? || (index && index <= 0) new_line = if index && index <= size line.dup.tap { |line| line.slice!(index) } else line.chop end Vedeu::Editor::Line.coerce(new_line) end |
#empty? ⇒ Boolean
Returns a boolean indicating whether there are characters on this line.
91 92 93 |
# File 'lib/vedeu/editor/line.rb', line 91 def empty? size == 0 end |
#eql?(other) ⇒ Boolean Also known as: ==
An object is equal when its values are the same.
99 100 101 |
# File 'lib/vedeu/editor/line.rb', line 99 def eql?(other) self.class == other.class && line == other.line end |
#insert_character(character, index = nil) ⇒ Vedeu::Editor::Line
Insert the character on the line positioned at the given index.
110 111 112 113 114 115 116 |
# File 'lib/vedeu/editor/line.rb', line 110 def insert_character(character, index = nil) return self unless character insert = Vedeu::Editor::Insert.into(line, character, index, size) Vedeu::Editor::Line.coerce(insert) end |
#size ⇒ Fixnum
Return the size of the line in characters.
121 122 123 |
# File 'lib/vedeu/editor/line.rb', line 121 def size line.size end |
#to_chars ⇒ Array<Vedeu::Views::Char>
Return the line as a collection of Vedeu::Views::Char objects.
128 129 130 |
# File 'lib/vedeu/editor/line.rb', line 128 def to_chars line.chars.map { |char| Vedeu::Views::Char.new(value: char) } end |