Class: Vedeu::Buffer

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

Overview

The Buffer object represents the states of display for an interface. The states are ‘front’, ‘back’ and ‘previous’.

Back

-> [Front] -> [Previous]

The content on the screen, or last output will always be the ‘Front’ buffer. Content due to be displayed on next refresh will come from the ‘Back’ buffer if available, otherwise from the current ‘Front’ buffer. When new content is copied to the ‘Front’ buffer, the current ‘Front’ buffer is also copied to the ‘Previous’ buffer.

Instance Attribute Summary collapse

Attributes included from Model

#repository

Instance Method Summary collapse

Methods included from Model

#demodulize, #deputy, #dsl_class, included, #store

Constructor Details

#initialize(attributes = {}) ⇒ Vedeu::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):



52
53
54
55
56
# File 'lib/vedeu/buffers/buffer.rb', line 52

def initialize(attributes = {})
  @attributes = defaults.merge!(attributes)

  @attributes.each { |key, value| instance_variable_set("@#{key}", value) }
end

Instance Attribute Details

#backInterface

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

Returns:



22
23
24
# File 'lib/vedeu/buffers/buffer.rb', line 22

def back
  @back
end

#frontInterface

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

Returns:



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

def front
  @front
end

#nameString (readonly)

Returns:

  • (String)


40
41
42
# File 'lib/vedeu/buffers/buffer.rb', line 40

def name
  @name
end

#previousInterface

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

Returns:



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

def previous
  @previous
end

Instance Method Details

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


63
64
65
66
67
68
69
# File 'lib/vedeu/buffers/buffer.rb', line 63

def add(content)
  @back = content

  store

  true
end

#bufferArray<Array<Array<Vedeu::Char>>> (private)

Retrieve the latest content from the buffer.

Returns:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/vedeu/buffers/buffer.rb', line 120

def buffer
  swap if content_for?(:back)

  if content_for?(:front)
    [front.render]

  elsif content_for?(:previous)
    [previous.render]

  else
    []

  end
end

#clearvoid

This method returns an undefined value.

Returns the front buffer or, if that is empty, the interface cleared.



74
75
76
# File 'lib/vedeu/buffers/buffer.rb', line 74

def clear
  Vedeu::Output.render(clear_buffer)
end

#clear_buffervoid (private)

This method returns an undefined value.

Clear the buffer.



138
139
140
# File 'lib/vedeu/buffers/buffer.rb', line 138

def clear_buffer
  @clear_buffer ||= Vedeu::Clear.new(view).clear
end

#content_for?(buffer) ⇒ Boolean (private)

Return a boolean indicating content presence on the buffer type.

Parameters:

  • buffer (Symbol)

    One of; :back, :front or :previous.

Returns:

  • (Boolean)

    Whether the buffer targetted has content.



172
173
174
175
176
177
# File 'lib/vedeu/buffers/buffer.rb', line 172

def content_for?(buffer)
  return false if public_send(buffer).nil? ||
                  public_send(buffer).content.empty?

  true
end

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

Returns:

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


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

def defaults
  {
    back:       nil,
    front:      nil,
    name:       '',
    previous:   nil,
    repository: Vedeu.buffers,
  }
end

#hidevoid

This method returns an undefined value.

Hide this buffer.



81
82
83
84
85
86
# File 'lib/vedeu/buffers/buffer.rb', line 81

def hide
  return nil unless visible?

  Vedeu::Visibility.hide(interface)
  clear
end

#interfaceVedeu::Interface (private)

Retrieve the interface by name.

Returns:



182
183
184
# File 'lib/vedeu/buffers/buffer.rb', line 182

def interface
  @interface ||= Vedeu.interfaces.by_name(name)
end

#renderArray<Array<Array<Vedeu::Char>>> Also known as: content

Return the content for this 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:



100
101
102
# File 'lib/vedeu/buffers/buffer.rb', line 100

def render
  Vedeu::Output.render(buffer)
end

#showvoid

This method returns an undefined value.

Show this buffer.



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

def show
  return nil if visible?

  Vedeu::Visibility.show(interface)
  render
end

#swapBoolean (private)

Return a boolean indicating content was swapped between buffers.

Returns:

  • (Boolean)


156
157
158
159
160
161
162
163
164
165
166
# File 'lib/vedeu/buffers/buffer.rb', line 156

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

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

  store

  true
end

#viewVedeu::Interface (private)

Returns:



187
188
189
190
191
192
193
194
195
# File 'lib/vedeu/buffers/buffer.rb', line 187

def view
  if content_for?(:front)
    front

  else
    interface

  end
end

#visible?Boolean (private)

Returns:

  • (Boolean)

See Also:



198
199
200
# File 'lib/vedeu/buffers/buffer.rb', line 198

def visible?
  interface.visible?
end