Class: Vedeu::Editor::Document

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Repositories::Model
Defined in:
lib/vedeu/editor/document.rb

Overview

A collection of keypresses ordered by input.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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

Parameters:

  • attributes (Hash) (defaults to: {})

Options Hash (attributes):

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


38
39
40
41
42
# File 'lib/vedeu/editor/document.rb', line 38

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)

Returns:

  • (Hash)


20
21
22
# File 'lib/vedeu/editor/document.rb', line 20

def attributes
  @attributes
end

#dataString

Returns:

  • (String)


24
25
26
# File 'lib/vedeu/editor/document.rb', line 24

def data
  @data
end

#nameString|Symbol

Returns:

  • (String|Symbol)


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

def name
  @name
end

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

Instance Method Details

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

Returns a boolean indicating whether a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#bolVedeu::Editor::Document

Move the virtual cursor to the beginning of the line.



214
215
216
217
218
# File 'lib/vedeu/editor/document.rb', line 214

def bol
  cursor.bol

  refresh
end

#cursorVedeu::Editor::Cursor (private)

Return a virtual cursor to track the cursor position within the document.



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

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

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

Returns the default options/attributes for this class.

Returns:

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

    Hash<Symbol => void|Symbol]



234
235
236
237
238
239
240
# File 'lib/vedeu/editor/document.rb', line 234

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

#delete_characterVedeu::Editor::Document

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



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vedeu/editor/document.rb', line 48

def delete_character
  if x - 1 < 0 && y == 0
    bol

  elsif x - 1 < 0 && y > 0
    delete_line

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

    left
  end
end

#delete_lineVedeu::Editor::Document

Delete a line.



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

def delete_line
  @lines = lines.delete_line(y)

  up

  eol
end

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

Removes the module part from the expression in the string.

Examples:

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

Parameters:

  • klass (Class|String)

Returns:

  • (String)

#deputy(client = nil) ⇒ void Originally defined in module Repositories::Model

This method returns an undefined value.

Returns a DSL instance responsible for defining the DSL methods of this model.

Parameters:

  • client (Object|NilClass) (defaults to: nil)

    The client binding represents the client application object that is currently invoking a DSL method. It is required so that we can send messages to the client application object should we need to.

#downVedeu::Editor::Document

Move the virtual cursor down.



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/vedeu/editor/document.rb', line 197

def down
  return self if y + 1 >= lines.size

  cursor.down

  if x > line(y).size
    eol

  else
    refresh

  end
end

#dsl_classString (private) Originally defined in module Repositories::Model

Returns the DSL class name responsible for this model.

Returns:

  • (String)

#eolVedeu::Editor::Document

Move the virtual cursor to the end of the line.



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

def eol
  cursor.x = line.size

  refresh
end

#executeString

Returns the document as a string with line breaks if there is more than one line.

Returns:

  • (String)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vedeu/editor/document.rb', line 77

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

  reset!

  Vedeu.trigger(:_clear_view_content_, name)

  Vedeu.trigger(:_command_, command)

  command
end

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

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

Parameters:

  • character (String|Symbol)

Returns:



94
95
96
97
98
99
100
# File 'lib/vedeu/editor/document.rb', line 94

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

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

  right
end

#insert_lineVedeu::Editor::Document

Insert an empty line.



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

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

  down

  bol
end

#leftVedeu::Editor::Document

Move the virtual cursor left.



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

def left
  return self if x - 1 < 0

  cursor.left

  refresh
end

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

Returns the current line from the collection of lines.

Returns:

  • (Array<String|void>)


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

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

#linesArray<String|void>

Returns the collection of lines which constitutes the document content.

Returns:

  • (Array<String|void>)


124
125
126
# File 'lib/vedeu/editor/document.rb', line 124

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

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

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:



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

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

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

Returns a boolean indicating whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#refreshVedeu::Editor::Document

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



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/vedeu/editor/document.rb', line 143

def refresh
  store

  Vedeu.trigger(:_clear_view_content_, name)

  Vedeu.render_output(output)

  cursor.refresh

  self
end

#reset!Vedeu::Editor::Document

Reset the document to the empty state.



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

def reset!
  @cursor = cursor.reset!

  @lines = defaults[:data]

  refresh
end

#rightVedeu::Editor::Document

Move the virtual cursor right.



169
170
171
172
173
174
175
# File 'lib/vedeu/editor/document.rb', line 169

def right
  return self if x + 1 > line.size

  cursor.right

  refresh
end

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

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

Move the virtual cursor up.



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/vedeu/editor/document.rb', line 180

def up
  return self if y - 1 < 0

  cursor.up

  if x > line(y).size
    eol

  else
    refresh

  end
end