Class: Vedeu::Editor::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/editor/line.rb

Overview

Manipulate a single line of an Vedeu::Editor::Document.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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

Parameters:

  • line (String|NilClass) (defaults to: nil)


34
35
36
# File 'lib/vedeu/editor/line.rb', line 34

def initialize(line = nil)
  @line = line || ''
end

Instance Attribute Details

#lineString Also known as: to_s

Returns:

  • (String)


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.

Parameters:

Returns:



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).

Parameters:

  • index (Fixnum|Range)

Returns:

  • (String)


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.

Parameters:

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

Returns:

  • (String|NilClass)


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.

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/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.

Returns:

  • (Boolean)


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.

Parameters:

Returns:

  • (Boolean)


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.

Parameters:

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

Returns:



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

#sizeFixnum

Return the size of the line in characters.

Returns:

  • (Fixnum)


121
122
123
# File 'lib/vedeu/editor/line.rb', line 121

def size
  line.size
end

#to_charsArray<Vedeu::Views::Char>

Return the line as a collection of Vedeu::Views::Char objects.

Returns:



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