Class: Vedeu::DSL::View

Inherits:
Object
  • Object
show all
Extended by:
Common
Includes:
Common, Vedeu::DSL, Presentation, Shared, Text, Use
Defined in:
lib/vedeu/dsl/view.rb

Overview

There are two ways to construct views with Vedeu. You would like to draw the view to the screen immediately (immediate render) or you want to save a view to be drawn when you trigger a refresh event later (deferred view).

Both of these approaches require that you have defined an interface (or ‘visible area’) first. You can find out how to define an interface with Vedeu below or in Interface. The examples in ‘Immediate Render’ and ‘Deferred View’ use these interface definitions: (Note: should you use these examples, ensure your terminal is at least 70 characters in width and 5 lines in height.)

Vedeu.interface 'main' do
  geometry do
    centred!
    height 4
    width  50
  end
end

Vedeu.interface 'title' do
  geometry do
    height 1
    width  50
    x      use('main').left
    y      use('main').north
  end
end

Both of these approaches use a concept of Buffers in Vedeu. There are three buffers for any defined interface. These are imaginatively called: ‘back’, ‘front’ and ‘previous’.

The ‘back’ buffer is the content for an interface which will be shown next time a refresh event is fired globally or for that interface. So, ‘back’ becomes ‘front’.

The ‘front’ buffer is the content for an interface which is currently showing. When a refresh event is fired, again, globally or for that interface specifically, the content of this ‘front’ buffer is first copied to the ‘previous’ buffer, and then the current ‘back’ buffer overwrites this ‘front’ buffer.

The ‘previous’ buffer contains what was shown on the ‘front’ before the current ‘front’.

You can only write to either the ‘front’ (you want the content to be drawn immediately (immediate render)) or the ‘back’ (you would like the content to be drawn on the next refresh (deferred view)).

The basic view DSL methods look like this:

renders/views
  |- view
      |- lines
      |   |- line
      |       |- streams
      |       |   |- stream
      |       |       |- char
      |       |
      |       |- stream
      |           |- char
      |
      |- line
          |- streams
          |   |- stream
          |       |- char
          |
          |- stream
              |- char

renders/views
  |- view
      |- lines/line
          |- streams/stream
              |- char

Instance Attribute Summary

Attributes included from Vedeu::DSL

#client, #model

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

demodulize, present?, snake_case

Methods included from Use

#use

Methods included from Text

#text

Methods included from Shared

#border, #border!, #geometry

Methods included from Presentation

#background, #colour, #colour_attributes, #foreground, #style

Methods included from Vedeu::DSL

#attributes, #method_missing

Constructor Details

#initialize(model, client = nil) ⇒ Vedeu::DSL::View

Returns an instance of Vedeu::DSL::View.

Parameters:



224
225
226
227
# File 'lib/vedeu/dsl/view.rb', line 224

def initialize(model, client = nil)
  @model  = model
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Vedeu::DSL

Class Method Details

.client(&block) ⇒ Object (private)

Returns the client object which called the DSL method.

Parameters:

  • block (Proc)

Returns:

  • (Object)


189
190
191
# File 'lib/vedeu/dsl/view.rb', line 189

def client(&block)
  eval('self', block.binding)
end

.composition(client, &block) ⇒ Vedeu::Views::Composition (private)

Creates a new Vedeu::Views::Composition which may contain one or more views (Vedeu::Views::View objects).

Parameters:

  • client (Object)
  • block (Proc)

Returns:



199
200
201
# File 'lib/vedeu/dsl/view.rb', line 199

def composition(client, &block)
  Vedeu::Views::Composition.build(client: client, &block)
end

.renders(&block) ⇒ Array<View> Also known as: render

Directly write a view buffer to the terminal. Using this method means that the refresh event does not need to be triggered after creating the views, though can be later triggered when needed.

Vedeu.renders do
  view 'some_interface' do
    line do
      stream do
        left 'Title goes here', width: 35
      end
      stream do
        right Time.now.strftime('%H:%m'), width: 7
      end
    end
  end
  view 'other_interface' do
    lines do
      line 'This is content for the main interface.'
      line ''
      line 'Pretty easy eh?'
    end
  end
  # ... some code
end

# or...

Vedeu.render do
  view 'my_interface' do
    # ... some code
  end
end

Parameters:

  • block (Proc)

    The directives you wish to send to render. Typically includes ‘view` with associated sub-directives.

Returns:

Raises:



131
132
133
134
135
136
# File 'lib/vedeu/dsl/view.rb', line 131

def renders(&block)
  fail Vedeu::Error::InvalidSyntax,
       'block not given' unless block_given?

  store(:store_immediate, &block)
end

.store(method, &block) ⇒ Array (private)

Stores each of the views defined in their respective buffers ready to be rendered on next refresh.

Parameters:

  • method (Symbol)

    An instruction; ‘:store_immediate` or `:store_deferred` which determines whether the view will be shown immediately or later respectively.

  • block (Proc)

Returns:

  • (Array)


211
212
213
214
215
# File 'lib/vedeu/dsl/view.rb', line 211

def store(method, &block)
  composition(client(&block), &block).views.map do |view|
    view.public_send(method)
  end
end

.views(&block) ⇒ Array<View>

Define a view (content) for an interface.

As you can see by comparing the examples above to these below, the immediate render simply wraps what is already here in the deferred view.

The views declared within this block are stored in their respective interface back buffers until a refresh event occurs. When the refresh event is triggered, the back buffers are swapped into the front buffers and the content here will be rendered to Terminal#output.

Vedeu.views do
  view 'some_interface' do
    line do
      stream do
        left 'Title goes here', width: 35
      end
      stream do
        right Time.now.strftime('%H:%m'), width: 7
      end
    end
  end
  view 'other_interface' do
    lines do
      line 'This is content for the main interface.'
      line ''
      line 'Pretty easy eh?'
    end
  end
  # ... some code
end

Parameters:

  • block (Proc)

    The directives you wish to send to render. Typically includes ‘view` with associated sub-directives.

Returns:

Raises:



176
177
178
179
180
181
# File 'lib/vedeu/dsl/view.rb', line 176

def views(&block)
  fail Vedeu::Error::InvalidSyntax,
       'block not given' unless block_given?

  store(:store_deferred, &block)
end

Instance Method Details

#lines(&block) ⇒ Vedeu::Views::Line Also known as: line

Specify multiple lines in a view.

Examples:

Vedeu.view 'my_interface' do
  lines do
    # ... see {Vedeu::DSL::Line} and {Vedeu::DSL::Stream}
  end
end

Vedeu.view 'my_interface' do
  line do
    # ... see {Vedeu::DSL::Line} and {Vedeu::DSL::Stream}
  end
end

Parameters:

  • block (Proc)

Returns:

Raises:



248
249
250
251
252
# File 'lib/vedeu/dsl/view.rb', line 248

def lines(&block)
  fail Vedeu::Error::InvalidSyntax, 'block not given' unless block_given?

  model.add(model.member.build(attributes, &block))
end