Class: Vedeu::Editor::Lines

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

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

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

Parameters:

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


45
46
47
# File 'lib/vedeu/editor/lines.rb', line 45

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

Instance Attribute Details

#linesString

Returns:

  • (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.

Parameters:

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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 { |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

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

Parameters:

  • index (Fixnum|Range)

Returns:



53
54
55
# File 'lib/vedeu/editor/lines.rb', line 53

def [](index)
  lines[index]
end

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

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

Parameters:

  • y (Fixnum)
  • x (Fixnum)

Returns:



63
64
65
66
67
# File 'lib/vedeu/editor/lines.rb', line 63

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

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

Parameters:

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

Returns:

  • (String)


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vedeu/editor/lines.rb', line 73

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.

Parameters:

  • block (Proc)

Returns:

  • (Enumerator)


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

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

#empty?Boolean

Returns a boolean indicating whether there are lines.

Returns:

  • (Boolean)


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

def empty?
  lines.empty?
end

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

An object is equal when its values are the same.

Parameters:

Returns:

  • (Boolean)


106
107
108
# File 'lib/vedeu/editor/lines.rb', line 106

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.

Parameters:

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

Returns:



117
118
119
120
121
# File 'lib/vedeu/editor/lines.rb', line 117

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

Insert the line on the line below the given index.

Parameters:

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

Returns:



127
128
129
130
131
132
133
# File 'lib/vedeu/editor/lines.rb', line 127

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

Returns the line at the given index.

Parameters:

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

Returns:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/vedeu/editor/lines.rb', line 139

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

  indexed = if index <= 0
              lines[0]

            elsif index && index <= size
              lines[index]

            else
              lines[-1]

            end

  Vedeu::Editor::Line.coerce(indexed)
end

#sizeFixnum

Return the number of lines.

Returns:

  • (Fixnum)


160
161
162
# File 'lib/vedeu/editor/lines.rb', line 160

def size
  lines.size
end

#to_sString

Return the lines as a string.

Returns:

  • (String)


167
168
169
# File 'lib/vedeu/editor/lines.rb', line 167

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