Class: Vedeu::Editor::Document Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Repositories::Model
Defined in:
lib/vedeu/editor/document.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.

A collection of keypresses ordered by input.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Vedeu::Editor::Document

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.

Note:

If a particular key is missing from the attributes parameter, then it is added with the respective value from #defaults.

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

Parameters:

Options Hash (attributes):

  • data (String)
  • name (String|Symbol)
  • repository (Object)
    Vedeu::Repositories::Repository


54
55
56
57
58
# File 'lib/vedeu/editor/document.rb', line 54

def initialize(attributes = {})
  defaults.merge!(attributes).each do |key, value|
    instance_variable_set("@#{key}", value || defaults.fetch(key))
  end
end

Instance Attribute Details

#attributesHash (readonly)

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:

  • (Hash)


22
23
24
# File 'lib/vedeu/editor/document.rb', line 22

def attributes
  @attributes
end

#dataString

Returns:

  • (String)


26
27
28
# File 'lib/vedeu/editor/document.rb', line 26

def data
  @data
end

#nameString|Symbol

Returns:

  • (String|Symbol)


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

def name
  @name
end

#repositoryVedeu::Repositories::Repository Originally defined in module Repositories::Model

Class Method Details

.store(attributes = {}) ⇒ Vedeu::Editor::Document

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.

Store an instance of this class with its repository.

Parameters:

Returns:



36
37
38
# File 'lib/vedeu/editor/document.rb', line 36

def self.store(attributes = {})
  new(attributes).store
end

Instance Method Details

#absent?(variable) ⇒ Boolean Originally defined in module Common

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 a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#cursorVedeu::Editor::Cursor (private)

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 virtual cursor to track the cursor position within the document.



239
240
241
# File 'lib/vedeu/editor/document.rb', line 239

def cursor
  @cursor ||= Vedeu::Editor::Cursor.new(name: name)
end

#defaultsHash<Symbol => void|Symbol] (private)

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 default options/attributes for this class.

Returns:

  • (Hash<Symbol => void|Symbol])

    Hash<Symbol => void|Symbol]



246
247
248
249
250
251
252
# File 'lib/vedeu/editor/document.rb', line 246

def defaults
  {
    data:       Vedeu::Editor::Lines.new([Vedeu::Editor::Line.new]),
    name:       nil,
    repository: Vedeu.documents,
  }
end

#delete_characterVedeu::Editor::Document

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.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vedeu/editor/document.rb', line 80

def delete_character
  return self if document_start?

  if first_char? && y > 0
    delete_line

    return

  else
    @lines = lines.delete_character(y, x - 1)

    left

  end

  refresh
end

#delete_lineVedeu::Editor::Document

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 a line.



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/vedeu/editor/document.rb', line 101

def delete_line
  @lines = lines.delete_line(y)

  up

  cursor.x = line.size

  cursor.refresh

  refresh
end

#demodulize(klass) ⇒ String Originally defined in module Common

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.

Removes the module part from the expression in the string.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

Parameters:

  • klass (Class|String)

Returns:

  • (String)

#document_start?Boolean (private)

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:

  • (Boolean)


231
232
233
# File 'lib/vedeu/editor/document.rb', line 231

def document_start?
  first_char? && first_line?
end

#downVedeu::Editor::Document

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.

Move the virtual cursor down.



220
221
222
223
224
225
226
# File 'lib/vedeu/editor/document.rb', line 220

def down
  return self if last_line?

  cursor.down(next_line_size).refresh

  self
end

#executeString

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 document as a string with line breaks if there is more than one line.

Returns:

  • (String)


64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vedeu/editor/document.rb', line 64

def execute
  command = lines.map(&:to_s).join("\n".freeze)

  reset!

  Vedeu.trigger(:_clear_view_content_, name)

  Vedeu.trigger(:_command_, command)

  command
end

#first_char?Boolean (private)

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:

  • (Boolean)


255
256
257
# File 'lib/vedeu/editor/document.rb', line 255

def first_char?
  x - 1 < 0
end

#first_line?Boolean (private)

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:

  • (Boolean)


260
261
262
# File 'lib/vedeu/editor/document.rb', line 260

def first_line?
  y - 1 < 0
end

#insert_character(character) ⇒ Vedeu::Editor::Document

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.

Inserts the given character in to the line where the cursor is currently positioned.

Parameters:

  • character (String|Symbol)

Returns:



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

def insert_character(character)
  return self if character.is_a?(Symbol)

  @lines = lines.insert_character(character, y, x)

  right

  refresh
end

#insert_lineVedeu::Editor::Document

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 an empty line.



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

def insert_line
  @lines = lines.insert_line(y + 1)

  down

  refresh
end

#last_char?Boolean (private)

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:

  • (Boolean)


265
266
267
# File 'lib/vedeu/editor/document.rb', line 265

def last_char?
  x + 1 > line.size
end

#last_line?Boolean (private)

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:

  • (Boolean)


270
271
272
# File 'lib/vedeu/editor/document.rb', line 270

def last_line?
  y + 1 >= lines.size
end

#leftVedeu::Editor::Document

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.

Move the virtual cursor left.



187
188
189
190
191
192
193
# File 'lib/vedeu/editor/document.rb', line 187

def left
  return self if first_char?

  cursor.left.refresh

  self
end

#line(index = y) ⇒ Array<String|void>

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 current line from the collection of lines.

Parameters:

  • index (Fixnum) (defaults to: y)

Returns:

  • (Array<String|void>)


143
144
145
# File 'lib/vedeu/editor/document.rb', line 143

def line(index = y)
  lines.line(index)
end

#linesArray<String|void>

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 collection of lines which constitutes the document content.

Returns:

  • (Array<String|void>)


151
152
153
# File 'lib/vedeu/editor/document.rb', line 151

def lines
  @lines ||= Vedeu::Editor::Lines.coerce(data)
end

#next_line_sizeFixnum (private)

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:

  • (Fixnum)


275
276
277
# File 'lib/vedeu/editor/document.rb', line 275

def next_line_size
  line(y + 1).size
end

#outputArray<Vedeu::Views::Char> (private)

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 data needed to render the document, based on the current virtual cursor position. This output may be a cropped representation of the full document depending on the size of the interface.

Returns:



285
286
287
288
289
290
# File 'lib/vedeu/editor/document.rb', line 285

def output
  Vedeu::Editor::Cropper.new(lines: lines,
                             name:  name,
                             ox:    ox,
                             oy:    oy).viewport
end

#present?(variable) ⇒ Boolean Originally defined in module Common

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 a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#prev_line_sizeFixnum (private)

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:

  • (Fixnum)


293
294
295
# File 'lib/vedeu/editor/document.rb', line 293

def prev_line_size
  line(y - 1).size
end

#refreshVedeu::Editor::Document

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.

Store the document in the documents repository, clear and send the content to the terminal.



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/vedeu/editor/document.rb', line 172

def refresh
  store

  Vedeu.trigger(:_clear_view_content_, name)

  Vedeu.buffer_update(output)

  Vedeu.direct_write(output.map(&:to_s).join)

  self
end

#reset!Vedeu::Editor::Document

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.

Reset the document to the empty state.



158
159
160
161
162
163
164
165
166
# File 'lib/vedeu/editor/document.rb', line 158

def reset!
  @cursor = cursor.reset!

  @lines = defaults[:data]

  cursor.refresh

  refresh
end

#rightVedeu::Editor::Document

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.

Move the virtual cursor right.



198
199
200
201
202
203
204
# File 'lib/vedeu/editor/document.rb', line 198

def right
  return self if last_char?

  cursor.right.refresh

  self
end

#snake_case(name) ⇒ String Originally defined in module Common

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.

Converts a class name to a lowercase snake case string.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • name (String)

Returns:

  • (String)

#storevoid Originally defined in module Repositories::Model

TODO:

Perhaps some validation could be added here?

Note:

If a block is given, store the model, return the model after yielding.

This method returns an undefined value.

Returns The model instance stored in the repository.

#upVedeu::Editor::Document

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.

Move the virtual cursor up.



209
210
211
212
213
214
215
# File 'lib/vedeu/editor/document.rb', line 209

def up
  return self if first_line?

  cursor.up(prev_line_size).refresh

  self
end