Class: Vedeu::Editor::Lines Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vedeu/editor/lines.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Manipulate the lines of an Vedeu::Editor::Document.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines = nil) ⇒ Vedeu::Editor::Lines

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Vedeu::Editor::Lines.

Parameters:

  • lines (Array<String>|NilClass) (defaults to: nil)


47
48
49
# File 'lib/vedeu/editor/lines.rb', line 47

def initialize(lines = nil)
  @lines = lines || []
end

Instance Attribute Details

#linesString

Returns:

  • (String)


15
16
17
# File 'lib/vedeu/editor/lines.rb', line 15

def lines
  @lines
end

Class Method Details

.coerce(document) ⇒ Vedeu::Editor::Lines

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerce a document into a new instance of Vedeu::Editor::Lines.

Parameters:

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vedeu/editor/lines.rb', line 21

def self.coerce(document)
  if document.is_a?(self)
    new(document.lines)

  elsif document.is_a?(Array)
    lines = document.map { |line| Vedeu::Editor::Line.coerce(line) }

    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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a line or collection of lines (if index is a Range).

Parameters:

  • index (Fixnum|Range)

Returns:



55
56
57
# File 'lib/vedeu/editor/lines.rb', line 55

def [](index)
  lines[index]
end

#delete_character(y, x) ⇒ Vedeu::Editor::Lines

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deletes the character from the line where the cursor is currently positioned.

Parameters:

  • y (Fixnum)
  • x (Fixnum)

Returns:



65
66
67
68
69
# File 'lib/vedeu/editor/lines.rb', line 65

def delete_character(y, x)
  lines[y] = line(y).delete_character(x) unless line(y).empty?

  Vedeu::Editor::Lines.coerce(lines)
end

#delete_line(index = nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delete the line from the lines positioned at the given index.

Parameters:

  • index (Fixnum|NilClass) (defaults to: nil)

Returns:

  • (String)


75
76
77
78
# File 'lib/vedeu/editor/lines.rb', line 75

def delete_line(index = nil)
  Vedeu::Editor::Lines.coerce(Vedeu::Editor::Delete
                              .from(lines, index, size))
end

#each(&block) ⇒ Enumerator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Provides iteration over the collection.

Parameters:

  • block (Proc)

Returns:

  • (Enumerator)


84
85
86
# File 'lib/vedeu/editor/lines.rb', line 84

def each(&block)
  lines.each(&block)
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating whether there are lines.

Returns:

  • (Boolean)


91
92
93
# File 'lib/vedeu/editor/lines.rb', line 91

def empty?
  lines.empty?
end

#eql?(other) ⇒ Boolean Also known as: ==

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An object is equal when its values are the same.

Parameters:

Returns:

  • (Boolean)


99
100
101
# File 'lib/vedeu/editor/lines.rb', line 99

def eql?(other)
  self.class == other.class && lines == other.lines
end

#insert_character(character, y, x) ⇒ Vedeu::Editor::Lines

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Insert a character in to a line.

Parameters:

  • character (String)
  • y (Fixnum)
  • x (Fixnum)

Returns:



110
111
112
113
114
# File 'lib/vedeu/editor/lines.rb', line 110

def insert_character(character, y, x)
  lines[y] = line(y).insert_character(character, x)

  Vedeu::Editor::Lines.coerce(lines)
end

#insert_line(index = nil) ⇒ Vedeu::Editor::Lines

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Insert the line on the line below the given index.

Parameters:

  • index (Fixnum|NilClass) (defaults to: nil)

Returns:



120
121
122
123
124
125
126
# File 'lib/vedeu/editor/lines.rb', line 120

def insert_line(index = nil)
  Vedeu::Editor::Lines.coerce(
    Vedeu::Editor::Insert.into(lines,
                               Vedeu::Editor::Line.new,
                               index,
                               size))
end

#line(index = nil) ⇒ Vedeu::Editor::Line

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the line at the given index.

Parameters:

  • index (Fixnum|NilClass) (defaults to: nil)

Returns:



132
133
134
135
136
137
# File 'lib/vedeu/editor/lines.rb', line 132

def line(index = nil)
  return Vedeu::Editor::Line.new unless lines
  return Vedeu::Editor::Line.coerce(lines[-1]) unless index

  Vedeu::Editor::Line.coerce(Vedeu::Editor::Item.by_index(lines, index))
end

#sizeFixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the number of lines.

Returns:

  • (Fixnum)


142
143
144
# File 'lib/vedeu/editor/lines.rb', line 142

def size
  lines.size
end

#to_sString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the lines as a string.

Returns:

  • (String)


149
150
151
# File 'lib/vedeu/editor/lines.rb', line 149

def to_s
  lines.map(&:to_s).join
end