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)


29
30
31
# File 'lib/vedeu/editor/line.rb', line 29

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

Parameters:

  • index (Fixnum|Range)

Returns:

  • (String)


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.

Parameters:

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

Returns:

  • (String|NilClass)


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.

Parameters:

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

Returns:

  • (String)


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.

Returns:

  • (Boolean)


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.

Parameters:

Returns:

  • (Boolean)


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.

Parameters:

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

Returns:



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

#sizeFixnum

Return the size of the line in characters.

Returns:

  • (Fixnum)


115
116
117
# File 'lib/vedeu/editor/line.rb', line 115

def size
  line.size
end