Class: Vedeu::Buffers::Buffer

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

Overview

Buffers

Instance Attribute Summary collapse

Attributes included from Model

#repository

Instance Method Summary collapse

Methods included from Model

#deputy, #dsl_class, included, #store

Methods included from Common

#demodulize, #present?, #snake_case

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
52
53
# File 'lib/vedeu/buffers/buffer.rb', line 47

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

  @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 (readonly)

Returns:

  • (String)


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

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)


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

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.



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

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

  true
end

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

Retrieve the latest content from the buffer.

Returns:



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/vedeu/buffers/buffer.rb', line 163

def buffer
  swap if back?

  if front?
    [front.render]

  elsif previous?
    [previous.render]

  else
    []

  end
end

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

Clear the buffer.

Returns:



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

def clear
  Vedeu::Output::Output.render(Vedeu::Clear::NamedInterface.render(name))
end

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

Returns the default options/attributes for this class.

Returns:

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


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

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

#front?Boolean

Return a boolean indicating content presence on the buffer type.

Returns:

  • (Boolean)

    Whether the buffer targetted has content.



90
91
92
93
94
# File 'lib/vedeu/buffers/buffer.rb', line 90

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

  true
end

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

Hide this buffer.

Will hide the named interface. If the interface is currently visible, it will be cleared- rendered blank. To show the interface, the ‘:show_interface’ event should be triggered. Triggering the ‘:hide_group’ event to which this named interface belongs will also hide the interface.

Examples:

Vedeu.trigger(:_hide_interface_, name)
Vedeu.hide_interface(name)

Returns:



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

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

#previous?Boolean

Return a boolean indicating content presence on the buffer type.

Returns:

  • (Boolean)

    Whether the buffer targetted has content.



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

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

  true
end

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

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:



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

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

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

Show this buffer.

Will show the named interface. If the interface is currently invisible, it will be shown- rendered with its latest content. To hide the interface, the ‘:hide_interface’ event should be triggered. Triggering the ‘:show_group’ event to which this named interface belongs will also show the interface.

Examples:

Vedeu.trigger(:_show_interface_, name)
Vedeu.show_interface(name)

Returns:



154
155
156
# File 'lib/vedeu/buffers/buffer.rb', line 154

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

#swapBoolean (private)

Return a boolean indicating content was swapped between buffers.

Returns:

  • (Boolean)


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

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

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

  store

  true
end