Class: Vedeu::Buffers::Buffer

Inherits:
Object
  • Object
show all
Includes:
Repositories::Model
Defined in:
lib/vedeu/buffers/buffer.rb,
lib/vedeu/buffers/repository.rb

Overview

Repository

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Vedeu::Buffers::Buffer

Return a new instance of Buffer. Generally a Buffer is initialized with only a ‘name’ and ‘back’ parameter.

Parameters:

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

    a customizable set of options

Options Hash (attributes):



47
48
49
50
51
# File 'lib/vedeu/buffers/buffer.rb', line 47

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

Instance Attribute Details

#backVedeu::Views::View

The next buffer to be displayed; contains the content which will be shown on next refresh.

Returns:



17
18
19
# File 'lib/vedeu/buffers/buffer.rb', line 17

def back
  @back
end

#frontVedeu::Views::View

The currently displayed buffer, contains the content which was last output.

Returns:



24
25
26
# File 'lib/vedeu/buffers/buffer.rb', line 24

def front
  @front
end

#nameString|Symbol (readonly)

Returns:

  • (String|Symbol)


35
36
37
# File 'lib/vedeu/buffers/buffer.rb', line 35

def name
  @name
end

#previousVedeu::Views::View

The previous buffer which was displayed; contains the content that was shown before ‘front’.

Returns:



31
32
33
# File 'lib/vedeu/buffers/buffer.rb', line 31

def previous
  @previous
end

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

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)

#add(content) ⇒ Boolean

Add the content to the back buffer, then update the repository. Returns boolean indicating that the repository was updated.

Parameters:

Returns:

  • (Boolean)


59
60
61
62
63
64
65
# File 'lib/vedeu/buffers/buffer.rb', line 59

def add(content)
  @back = content

  store

  true
end

#back?Boolean

Return a boolean indicating content presence on the buffer type.

Returns:

  • (Boolean)

    Whether the buffer targetted has content.



71
72
73
74
75
# File 'lib/vedeu/buffers/buffer.rb', line 71

def back?
  return false if back.nil? || back.lines.empty?

  true
end

#cursor_visible?Boolean

Return a boolean indicating whether the cursor should be visible for this view.

On a per-view (and per-interface) basis, the cursor can be set to be visible or not visible.

  • If the cursor is visible, then refresh actions or events involving the cursor will act as normal; hiding and showing as the view is rendered or as events are triggered to change the visibility state.

  • If the cursor is not visible, then refresh actions and events involving the cursor will be ignored- the cursor is not shown, so do no work.

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/vedeu/buffers/buffer.rb', line 92

def cursor_visible?
  if front?
    front.cursor_visible?

  elsif previous?
    previous.cursor_visible?

  else
    Vedeu.interfaces.by_name(name).cursor_visible?

  end
end

#defaultsHash<Symbol => NilClass, String> (private)

Returns the default options/attributes for this class.

Returns:

  • (Hash<Symbol => NilClass, String>)


179
180
181
182
183
184
185
186
187
# File 'lib/vedeu/buffers/buffer.rb', line 179

def defaults
  {
    back:       nil,
    front:      nil,
    name:       '',
    previous:   nil,
    repository: Vedeu.buffers,
  }
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)

#front?Boolean

Return a boolean indicating content presence on the buffer type.

Returns:

  • (Boolean)

    Whether the buffer targetted has content.



109
110
111
112
113
# File 'lib/vedeu/buffers/buffer.rb', line 109

def front?
  return false if front.nil? || front.lines.empty?

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

#previous?Boolean

Return a boolean indicating content presence on the buffer type.

Returns:

  • (Boolean)

    Whether the buffer targetted has content.



119
120
121
122
123
# File 'lib/vedeu/buffers/buffer.rb', line 119

def previous?
  return false if previous.nil? || previous.lines.empty?

  true
end

#renderArray<Array<Array<Vedeu::Views::Char>>>

Retrieve the latest content from the buffer.

  • If we have new content (i.e. content on ‘back’) to be shown, we first clear the area occupied by the previous content, then clear the area for the new content, and then finally render the new content.

  • If there is no new content (i.e. ‘back’ is empty), check the ‘front’ buffer and display that.

  • If there is no new content, and the front buffer is empty, display the ‘previous’ buffer.

  • If the ‘previous’ buffer is empty, return an empty collection.

Returns:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/vedeu/buffers/buffer.rb', line 139

def render
  if back?
    swap

    Vedeu::Output::Viewport.render(front)

  elsif front?
    Vedeu::Output::Viewport.render(front)

  elsif previous?
    Vedeu::Output::Viewport.render(previous)

  end
end

#sizeFixnum

Returns the number of lines of content for the buffer or 0 if the buffer is empty.

Returns:

  • (Fixnum)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/vedeu/buffers/buffer.rb', line 158

def size
  if back?
    back.lines.size

  elsif front?
    front.lines.size

  elsif previous?
    previous.lines.size

  else
    0

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

#swapBoolean (private)

Return a boolean indicating content was swapped between buffers.

Returns:

  • (Boolean)


193
194
195
196
197
198
199
200
201
202
203
# File 'lib/vedeu/buffers/buffer.rb', line 193

def swap
  Vedeu.log(type: :output, message: "Buffer swapping: '#{name}'".freeze)

  @previous = front
  @front    = back
  @back     = nil

  store

  true
end