Class: Vedeu::Config::API

Inherits:
Object
  • Object
show all
Includes:
Vedeu::Common
Defined in:
lib/vedeu/configuration/api.rb

Overview

The Configuration::API class parses client application configuration into options used by Vedeu to affect certain behaviours.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Vedeu::Common

#defined_value?, #to_sentence

Constructor Details

#initialize(&block) ⇒ Configuration::API

Returns an instance of Configuration::API.



32
33
34
# File 'lib/vedeu/configuration/api.rb', line 32

def initialize(&block)
  instance_eval(&block) if block_given?
end

Class Method Details

.configure(&block) ⇒ Hash

Configure Vedeu via a simple configuration API DSL. Options set here override the default Vedeu configuration set in Vedeu::Configuration#defaults.

Examples:

Vedeu.configure do
  ...


24
25
26
# File 'lib/vedeu/configuration/api.rb', line 24

def self.configure(&block)
  new(&block).configuration
end

Instance Method Details

#colour_mode(value = nil) ⇒ Boolean

Sets the colour mode of the terminal.

Examples:

Vedeu.configure do
  colour_mode 256
  ...

Raises:

  • (InvalidSyntax)

    When the value parameter is not one of 8, 16, 256 or 16777216.



248
249
250
251
252
# File 'lib/vedeu/configuration/api.rb', line 248

def colour_mode(value = nil)
  fail InvalidSyntax, '`colour_mode` must be `8`, `16`, `256`, ' \
                      '`16777216`.' unless valid_colour_mode?(value)
  options[:colour_mode] = value
end

#configurationHash

Returns the configuration options set up by the API DSL.



39
40
41
42
43
44
45
# File 'lib/vedeu/configuration/api.rb', line 39

def configuration
  options.merge!({
    system_keys: Configuration.default_system_keys.merge!(system_keys)
  }) if system_keys.any?

  Vedeu::Config.log('API', options)
end

#cooked!Boolean Also known as: cooked

Sets the terminal mode to ‘cooked`. Default terminal mode is `raw`.

Examples:

Vedeu.configure do
  cooked!
  ...


171
172
173
# File 'lib/vedeu/configuration/api.rb', line 171

def cooked!
  options[:terminal_mode] = :cooked
end

#debug!(value = true) ⇒ Boolean Also known as: debug

Sets boolean to enable/disable debugging. Vedeu’s default setting is for debugging to be disabled. Using ‘debug!` or setting `debug` to true will enable debugging. If `trace!` is used, or `trace` is set to true, debugging will be enabled, overriding any setting here.

Examples:

Vedeu.configure do
  debug!
  ...

Vedeu.configure do
  debug false
  ...


205
206
207
208
209
210
211
212
213
# File 'lib/vedeu/configuration/api.rb', line 205

def debug!(value = true)
  if options.key?(:trace) && options[:trace] != false
    options[:debug] = true

  else
    options[:debug] = value

  end
end

#drb!(value = true) ⇒ Boolean Also known as: drb

Sets boolean to run a DRb server.

Examples:

Vedeu.configure do
  drb!
  ...


106
107
108
# File 'lib/vedeu/configuration/api.rb', line 106

def drb!(value = true)
  options[:drb] = value
end

#drb_height(height = 25) ⇒ Fixnum

Sets the height of the fake terminal in the DRb server.

Examples:

Vedeu.configure do
  drb_height 25
  ...


146
147
148
# File 'lib/vedeu/configuration/api.rb', line 146

def drb_height(height = 25)
  options[:drb_height] = height
end

#drb_host(hostname = '') ⇒ String

Sets the hostname or IP address of the DRb server.

Examples:

Vedeu.configure do
  drb_host 'localhost'
  ...


120
121
122
# File 'lib/vedeu/configuration/api.rb', line 120

def drb_host(hostname = '')
  options[:drb_host] = hostname
end

#drb_port(port = '') ⇒ String

Sets the port of the DRb server.

Examples:

Vedeu.configure do
  drb_port 12345
  ...


133
134
135
# File 'lib/vedeu/configuration/api.rb', line 133

def drb_port(port = '')
  options[:drb_port] = port
end

#drb_width(width = 80) ⇒ Fixnum

Sets the width of the fake terminal in the DRb server.

Examples:

Vedeu.configure do
  drb_width 80
  ...


159
160
161
# File 'lib/vedeu/configuration/api.rb', line 159

def drb_width(width = 80)
  options[:drb_width] = width
end

#exit_key(value) ⇒ String|Symbol

Sets the key used to exit the client application. The default is ‘q`.

Examples:

Vedeu.configure do
  exit_key 'x'
  ...

Vedeu.configure do
  exit_key :f4
  ...


319
320
321
322
# File 'lib/vedeu/configuration/api.rb', line 319

def exit_key(value)
  return invalid_key('exit_key') unless valid_key?(value)
  system_keys[:exit] = value
end

#focus_next_key(value) ⇒ String|Symbol

Sets the key used to switch focus to the next defined interface. The default is ‘:tab`.

Examples:

Vedeu.configure do
  focus_next_key 'n'
  ...

Vedeu.configure do
  focus_next_key :right
  ...


338
339
340
341
# File 'lib/vedeu/configuration/api.rb', line 338

def focus_next_key(value)
  return invalid_key('focus_next_key') unless valid_key?(value)
  system_keys[:focus_next] = value
end

#focus_prev_key(value) ⇒ String|Symbol

Sets the key used to switch focus to the previous interface. The default is ‘:shift_tab`.

Examples:

Vedeu.configure do
  focus_prev_key 'p'
  ...

Vedeu.configure do
  focus_prev_key :left
  ...


357
358
359
360
# File 'lib/vedeu/configuration/api.rb', line 357

def focus_prev_key(value)
  return invalid_key('focus_prev_key') unless valid_key?(value)
  system_keys[:focus_prev] = value
end

#interactive!(value = true) ⇒ Boolean Also known as: interactive

Sets boolean to allow user input. The default behaviour of Vedeu is to be interactive.

Examples:

Vedeu.configure do
  interactive!
  ...

Vedeu.configure do
  interactive false
  ...


61
62
63
# File 'lib/vedeu/configuration/api.rb', line 61

def interactive!(value = true)
  options[:interactive] = value
end

#invalid_key(system_key) ⇒ InvalidSyntax (private)

Raises an exception on behalf of the calling method to report that the value provided is not valid.

Raises:

  • (InvalidSyntax)

    When the system_key parameter is not a String or Symbol.



429
430
431
# File 'lib/vedeu/configuration/api.rb', line 429

def invalid_key(system_key)
  fail InvalidSyntax, "`#{system_key}` must be a String or a Symbol."
end

#log(filename = '') ⇒ String

Sets the location of the log file.

Examples:

Vedeu.configure do
  log '/var/log/vedeu.log'
  ...


263
264
265
# File 'lib/vedeu/configuration/api.rb', line 263

def log(filename = '')
  options[:log] = filename
end

#mode_switch_key(value) ⇒ String|Symbol

Sets the key used to switch between raw and cooked mode in Vedeu. The default is ‘:escape`.

Examples:

Vedeu.configure do
  mode_switch_key 'm'
  ...

Vedeu.configure do
  mode_switch_key :f1
  ...


376
377
378
379
# File 'lib/vedeu/configuration/api.rb', line 376

def mode_switch_key(value)
  return invalid_key('mode_switch_key') unless valid_key?(value)
  system_keys[:mode_switch] = value
end

#optionsHash (private)

Returns the options set via the configuration API DSL or an empty Hash if none were set.



387
388
389
# File 'lib/vedeu/configuration/api.rb', line 387

def options
  @options ||= {}
end

#raw!Boolean Also known as: raw

Sets the terminal mode to ‘raw`. Default terminal mode is `raw`.

Examples:

Vedeu.configure do
  raw!
  ...


184
185
186
# File 'lib/vedeu/configuration/api.rb', line 184

def raw!
  options[:terminal_mode] = :raw
end

#run_once!(value = true) ⇒ Boolean Also known as: run_once

Sets boolean to run the Vedeu main application loop once. In effect, using ‘run_once!` or setting `run_once` to true will allow Vedeu to initialize, run any client application code, cleanup, then terminate.

Examples:

Vedeu.configure do
  run_once!
  ...


92
93
94
# File 'lib/vedeu/configuration/api.rb', line 92

def run_once!(value = true)
  options[:once] = value
end

#standalone!(value = true) ⇒ Boolean Also known as: standalone

Sets boolean to prevent user intervention. This is the same as setting #interactive! to false.

Examples:

Vedeu.configure do
  standalone!
  ...


76
77
78
# File 'lib/vedeu/configuration/api.rb', line 76

def standalone!(value = true)
  options[:interactive] = !value
end

#stderr(io) ⇒ File|IO

Sets the value of STDERR.

Examples:

Vedeu.configure do
  stderr IO.console
  ...


302
303
304
# File 'lib/vedeu/configuration/api.rb', line 302

def stderr(io)
  options[:stderr] = io
end

#stdin(io) ⇒ File|IO

Sets the value of STDIN.

Examples:

Vedeu.configure do
  stdin IO.console
  ...


276
277
278
# File 'lib/vedeu/configuration/api.rb', line 276

def stdin(io)
  options[:stdin] = io
end

#stdout(io) ⇒ File|IO

Sets the value of STDOUT.

Examples:

Vedeu.configure do
  stdout IO.console
  ...


289
290
291
# File 'lib/vedeu/configuration/api.rb', line 289

def stdout(io)
  options[:stdout] = io
end

#system_keysHash (private)

Returns the system keys set via the configuration API DSL or an empty hash if none were redefined.



395
396
397
# File 'lib/vedeu/configuration/api.rb', line 395

def system_keys
  @system_keys ||= {}
end

#trace!(value = true) ⇒ Boolean Also known as: trace

Sets boolean to enable/disable tracing. Vedeu’s default setting is for tracing to be disabled. Using ‘trace!` or setting `trace` to true will enable tracing and debugging.

Examples:

Vedeu.configure do
  trace!
  ...

Vedeu.configure do
  trace false
  ...


231
232
233
234
# File 'lib/vedeu/configuration/api.rb', line 231

def trace!(value = true)
  options[:debug] = true if value === true
  options[:trace] = value
end

#valid_colour_mode?(value) ⇒ Boolean (private)

Checks that the value provided to #colour_mode is valid.



403
404
405
# File 'lib/vedeu/configuration/api.rb', line 403

def valid_colour_mode?(value)
  value.is_a?(Fixnum) && [8, 16, 256, 16777216].include?(value)
end

#valid_key?(value) ⇒ Boolean (private)

Checks that the value provided to #exit_key, #focus_next_key, #focus_prev_key and #mode_switch_key is valid. Must be a Symbol or a non-empty String.



413
414
415
416
417
418
419
# File 'lib/vedeu/configuration/api.rb', line 413

def valid_key?(value)
  return false unless value.is_a?(String) || value.is_a?(Symbol)

  return false if value.is_a?(String) && value.size != 1

  (value.is_a?(String) || value.is_a?(Symbol)) && defined_value?(value)
end