Class: Vedeu::Editor::Document
- Inherits:
-
Object
- Object
- Vedeu::Editor::Document
- 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
- #attributes ⇒ Hash readonly
- #data ⇒ String
- #name ⇒ String|Symbol
- #repository ⇒ Vedeu::Repositories::Repository included from Repositories::Model
Instance Method Summary collapse
-
#absent?(variable) ⇒ Boolean
included
from Common
Returns a boolean indicating whether a variable is nil or empty.
-
#bol ⇒ Vedeu::Editor::Document
Move the virtual cursor to the beginning of the line.
-
#cursor ⇒ Vedeu::Editor::Cursor
private
Return a virtual cursor to track the cursor position within the document.
-
#defaults ⇒ Hash<Symbol => void|Symbol]
private
Returns the default options/attributes for this class.
-
#delete_character ⇒ Vedeu::Editor::Document
Deletes the character from the line where the cursor is currently positioned.
-
#delete_line ⇒ Vedeu::Editor::Document
Delete a line.
-
#demodulize(klass) ⇒ String
included
from Common
Removes the module part from the expression in the string.
-
#deputy(client = nil) ⇒ void
included
from Repositories::Model
Returns a DSL instance responsible for defining the DSL methods of this model.
-
#down ⇒ Vedeu::Editor::Document
Move the virtual cursor down.
-
#dsl_class ⇒ String
included
from Repositories::Model
private
Returns the DSL class name responsible for this model.
-
#eol ⇒ Vedeu::Editor::Document
Move the virtual cursor to the end of the line.
-
#execute ⇒ String
Returns the document as a string with line breaks if there is more than one line.
-
#initialize(attributes = {}) ⇒ Vedeu::Editor::Document
constructor
Returns a new instance of Vedeu::Editor::Document.
-
#insert_character(character) ⇒ Vedeu::Editor::Document
Inserts the given character in to the line where the cursor is currently positioned.
-
#insert_line ⇒ Vedeu::Editor::Document
Insert an empty line.
-
#left ⇒ Vedeu::Editor::Document
Move the virtual cursor left.
-
#line(index = y) ⇒ Array<String|void>
Returns the current line from the collection of lines.
-
#lines ⇒ Array<String|void>
Returns the collection of lines which constitutes the document content.
-
#output ⇒ Array<Vedeu::Views::Char>
private
Return the data needed to render the document, based on the current virtual cursor position.
-
#present?(variable) ⇒ Boolean
included
from Common
Returns a boolean indicating whether a variable has a useful value.
-
#refresh ⇒ Vedeu::Editor::Document
Store the document in the documents repository, clear and send the content to the terminal.
-
#reset! ⇒ Vedeu::Editor::Document
Reset the document to the empty state.
-
#right ⇒ Vedeu::Editor::Document
Move the virtual cursor right.
-
#snake_case(name) ⇒ String
included
from Common
Converts a class name to a lowercase snake case string.
-
#store ⇒ void
included
from Repositories::Model
The model instance stored in the repository.
-
#up ⇒ Vedeu::Editor::Document
Move the virtual cursor up.
Constructor Details
#initialize(attributes = {}) ⇒ Vedeu::Editor::Document
Returns a new instance of Vedeu::Editor::Document.
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
#attributes ⇒ Hash (readonly)
20 21 22 |
# File 'lib/vedeu/editor/document.rb', line 20 def attributes @attributes end |
#data ⇒ String
24 25 26 |
# File 'lib/vedeu/editor/document.rb', line 24 def data @data end |
#name ⇒ String|Symbol
28 29 30 |
# File 'lib/vedeu/editor/document.rb', line 28 def name @name end |
#repository ⇒ Vedeu::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.
#bol ⇒ Vedeu::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 |
#cursor ⇒ Vedeu::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 |
#defaults ⇒ Hash<Symbol => void|Symbol] (private)
Returns the default options/attributes for this class.
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_character ⇒ Vedeu::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_line ⇒ Vedeu::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.
#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.
#down ⇒ Vedeu::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_class ⇒ String (private) Originally defined in module Repositories::Model
Returns the DSL class name responsible for this model.
#eol ⇒ Vedeu::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 |
#execute ⇒ String
Returns the document as a string with line breaks if there is more than one line.
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.
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_line ⇒ Vedeu::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 |
#left ⇒ Vedeu::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.
116 117 118 |
# File 'lib/vedeu/editor/document.rb', line 116 def line(index = y) lines.line(index) end |
#lines ⇒ Array<String|void>
Returns the collection of lines which constitutes the document content.
124 125 126 |
# File 'lib/vedeu/editor/document.rb', line 124 def lines @lines ||= Vedeu::Editor::Lines.coerce(data) end |
#output ⇒ Array<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.
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). end |
#present?(variable) ⇒ Boolean Originally defined in module Common
Returns a boolean indicating whether a variable has a useful value.
#refresh ⇒ Vedeu::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 |
#right ⇒ Vedeu::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.
#store ⇒ void Originally defined in module Repositories::Model
Perhaps some validation could be added here?
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.
#up ⇒ Vedeu::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 |