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.
Constructor Details
#initialize(line = nil) ⇒ Vedeu::Editor::Line
Returns a new instance of Vedeu::Editor::Line.
29 30 31 |
# File 'lib/vedeu/editor/line.rb', line 29 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 |
# File 'lib/vedeu/editor/line.rb', line 18 def self.coerce(line) return line if line.is_a?(self) return new(line) if line.is_a?(String) new end |
Instance Method Details
#[](index) ⇒ String
Return a character or collection of characters (if index is a Range).
38 39 40 |
# File 'lib/vedeu/editor/line.rb', line 38 def [](index) line[index] end |
#character(index = nil) ⇒ String|NilClass
Return the character from the line positioned at the given index.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/vedeu/editor/line.rb', line 47 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.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/vedeu/editor/line.rb', line 68 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.
86 87 88 |
# File 'lib/vedeu/editor/line.rb', line 86 def empty? line.empty? end |
#eql?(other) ⇒ Boolean Also known as: ==
An object is equal when its values are the same.
94 95 96 |
# File 'lib/vedeu/editor/line.rb', line 94 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.
105 106 107 108 109 110 |
# File 'lib/vedeu/editor/line.rb', line 105 def insert_character(character, index = nil) return self unless character Vedeu::Editor::Line.coerce(Vedeu::Editor::Insert .into(line, character, index, size)) end |
#size ⇒ Fixnum
Return the size of the line in characters.
115 116 117 |
# File 'lib/vedeu/editor/line.rb', line 115 def size line.size end |