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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Vedeu::Common

#absent?, #array?, #boolean, #boolean?, #empty_value?, #escape?, #falsy?, #hash?, #line_model?, #numeric?, #positionable?, #present?, #snake_case, #stream_model?, #string?, #symbol?, #truthy?, #view_model?

Constructor Details

#initialize(default, &block) ⇒ Vedeu::Configuration::API

Returns a new instance of Vedeu::Config::API.

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

Vedeu.configure do
  # ...
end

Parameters:

  • default (Hash)
  • block (Proc)


35
36
37
38
39
# File 'lib/vedeu/configuration/api.rb', line 35

def initialize(default, &block)
  @default = default

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#defaultHash (readonly, protected)

Returns:

  • (Hash)


332
333
334
# File 'lib/vedeu/configuration/api.rb', line 332

def default
  @default
end

Class Method Details

.configure(default, &block) ⇒ Object

Parameters:

  • default (Hash)
  • block (Proc)


18
19
20
# File 'lib/vedeu/configuration/api.rb', line 18

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

Instance Method Details

#background(value = nil) ⇒ Vedeu::Colours::Background

Parameters:

  • value (String) (defaults to: nil)

Returns:



44
45
46
47
48
# File 'lib/vedeu/configuration/api.rb', line 44

def background(value = nil)
  return options[:background] unless value

  options[:background] = value
end

#base_path(path = nil) ⇒ String

Parameters:

  • path (String) (defaults to: nil)

Returns:

  • (String)


53
54
55
# File 'lib/vedeu/configuration/api.rb', line 53

def base_path(path = nil)
  options[:base_path] = path
end

#colour(attrs = {}) ⇒ Hash<Symbol => void>

Parameters:

  • attrs (Hash<Symbol => String>) (defaults to: {})

Returns:

  • (Hash<Symbol => void>)


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

def colour(attrs = {})
  options[:background] = attrs[:background] if attrs.key?(:background)
  options[:foreground] = attrs[:foreground] if attrs.key?(:foreground)
  options
end

#colour_mode(value = nil) ⇒ Boolean

Parameters:

  • value (Fixnum) (defaults to: nil)

Returns:

Raises:

  • (Vedeu::Error::InvalidSyntax)

    When the value given for an argument or parameter cannot be used because it is not valid for the use case, unsupported or the method expects a different type.



70
71
72
73
74
75
76
77
# File 'lib/vedeu/configuration/api.rb', line 70

def colour_mode(value = nil)
  unless valid_colour_mode?(value)
    raise Vedeu::Error::InvalidSyntax,
          '`colour_mode` must be `8`, `16`, `256`, `16777216`.'
  end

  options[:colour_mode] = value
end

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

Parameters:

  • value (Boolean) (defaults to: true)

Returns:



82
83
84
# File 'lib/vedeu/configuration/api.rb', line 82

def compression(value = true)
  options[:compression] = value
end

#configurationHash<Symbol => Boolean, Fixnum, String>

Returns the configuration options set up by the API DSL.

Returns:

  • (Hash<Symbol => Boolean, Fixnum, String>)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/vedeu/configuration/api.rb', line 90

def configuration
  if options[:log].nil?          ||
     options[:log] == false      ||
     empty_value?(options[:log])
    Vedeu.log(type:    :config,
              message: 'Logging has been disabled.')

    return options
  end

  log_options!

  if options[:log] != default[:log]
    Vedeu.log(message: "Switching to '#{options[:log]}' for logging.\n")
  end

  options
end

#cooked!Boolean Also known as: cooked

Returns:



111
112
113
# File 'lib/vedeu/configuration/api.rb', line 111

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

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

Parameters:

  • value (Boolean) (defaults to: true)

Returns:



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

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

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

Parameters:

  • value (Boolean) (defaults to: true)

Returns:



127
128
129
# File 'lib/vedeu/configuration/api.rb', line 127

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

#drb_height(height = 25) ⇒ Fixnum

Parameters:

  • height (Fixnum) (defaults to: 25)

Returns:

  • (Fixnum)


135
136
137
# File 'lib/vedeu/configuration/api.rb', line 135

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

#drb_host(hostname = '') ⇒ String

Parameters:

  • hostname (String) (defaults to: '')

Returns:

  • (String)


142
143
144
# File 'lib/vedeu/configuration/api.rb', line 142

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

#drb_port(port = '') ⇒ String

Parameters:

  • port (Fixnum|String) (defaults to: '')

Returns:

  • (String)


149
150
151
# File 'lib/vedeu/configuration/api.rb', line 149

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

#drb_width(width = 80) ⇒ Fixnum

Parameters:

  • width (Fixnum) (defaults to: 80)

Returns:

  • (Fixnum)


156
157
158
# File 'lib/vedeu/configuration/api.rb', line 156

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

#fake!Boolean Also known as: fake

Returns:



162
163
164
# File 'lib/vedeu/configuration/api.rb', line 162

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

#foreground(value = nil) ⇒ Vedeu::Colours::Foreground

Parameters:

  • value (String) (defaults to: nil)

Returns:



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

def foreground(value = nil)
  return options[:foreground] unless value

  options[:foreground] = value
end

#height(height = 25) ⇒ Fixnum Also known as: height=

Parameters:

  • height (Fixnum) (defaults to: 25)

Returns:

  • (Fixnum)


179
180
181
# File 'lib/vedeu/configuration/api.rb', line 179

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

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

Parameters:

  • value (Boolean) (defaults to: true)

Returns:



187
188
189
# File 'lib/vedeu/configuration/api.rb', line 187

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

#invalid_mode!Object (private)

Raises:

  • (Vedeu::Error::InvalidSyntax)

    When the value given for an argument or parameter cannot be used because it is not valid for the use case, unsupported or the method expects a different type.



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

def invalid_mode!
  raise Vedeu::Error::InvalidSyntax,
        'Terminal mode can be set to either :cooked, :fake or :raw'
end

#log(filename_or_false = false) ⇒ NilClass|String

Parameters:

  • filename_or_false (FalseClass|String) (defaults to: false)

Returns:

  • (NilClass|String)


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

def log(filename_or_false = false)
  options[:log] = if filename_or_false.nil? ||
                     filename_or_false == false ||
                     empty_value?(filename_or_false)
                    nil

                  else
                    filename_or_false

                  end
end

#log_except(*types) ⇒ Array<Symbol>

Parameters:

  • types (Array<Symbol>)

    The message types which should not be logged.

Returns:

  • (Array<Symbol>)


211
212
213
# File 'lib/vedeu/configuration/api.rb', line 211

def log_except(*types)
  options[:log_except] = types.flatten
end

#log_only(*types) ⇒ Array<Symbol>

Parameters:

  • types (Array<Symbol>)

    The message types which should be logged.

Returns:

  • (Array<Symbol>)


219
220
221
# File 'lib/vedeu/configuration/api.rb', line 219

def log_only(*types)
  options[:log_only] = types.flatten
end

#log_options!Hash (private)

Returns:

  • (Hash)


343
344
345
346
347
348
# File 'lib/vedeu/configuration/api.rb', line 343

def log_options!
  options.each do |option, value|
    Vedeu.log(type:    :config,
              message: "#{option}: #{value.inspect}")
  end
end

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

Parameters:

  • value (Boolean) (defaults to: true)

Returns:



226
227
228
# File 'lib/vedeu/configuration/api.rb', line 226

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

#optionsHash<Symbol => void> (private)

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

Returns:

  • (Hash<Symbol => void>)


354
355
356
# File 'lib/vedeu/configuration/api.rb', line 354

def options
  @options ||= {}
end

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

Parameters:

  • value (Boolean) (defaults to: true)

Returns:



234
235
236
# File 'lib/vedeu/configuration/api.rb', line 234

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

#raw!Boolean Also known as: raw

Returns:



241
242
243
# File 'lib/vedeu/configuration/api.rb', line 241

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

#renderer(*renderer) ⇒ Array<Class> Also known as: renderers

Parameters:

  • renderer (Array<Class>|Class)

Returns:

  • (Array<Class>)


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

def renderer(*renderer)
  options[:renderers] = renderer.flatten
end

#root(*args) ⇒ Class

Parameters:

  • args (Array<Symbol|void>)

Returns:

  • (Class)


257
258
259
# File 'lib/vedeu/configuration/api.rb', line 257

def root(*args)
  options[:root] = args
end

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

Parameters:

  • value (Boolean) (defaults to: true)

Returns:



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

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

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

Parameters:

  • value (Boolean) (defaults to: true)

Returns:



272
273
274
# File 'lib/vedeu/configuration/api.rb', line 272

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

#stderr(io) ⇒ File|IO

Parameters:

  • io (File|IO)

Returns:

  • (File|IO)


294
295
296
# File 'lib/vedeu/configuration/api.rb', line 294

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

#stdin(io) ⇒ File|IO

Parameters:

  • io (File|IO)

Returns:

  • (File|IO)


280
281
282
# File 'lib/vedeu/configuration/api.rb', line 280

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

#stdout(io) ⇒ File|IO

Parameters:

  • io (File|IO)

Returns:

  • (File|IO)


287
288
289
# File 'lib/vedeu/configuration/api.rb', line 287

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

#terminal_mode(mode) ⇒ Symbol Also known as: terminal_mode=

Parameters:

  • mode (Symbol)

    Either ‘:cooked’, ‘:fake’ or ‘:raw’.

Returns:

  • (Symbol)

Raises:

  • (Vedeu::Error::InvalidSyntax)

    When the value given for an argument or parameter cannot be used because it is not valid for the use case, unsupported or the method expects a different type.

See Also:



305
306
307
308
309
# File 'lib/vedeu/configuration/api.rb', line 305

def terminal_mode(mode)
  return invalid_mode! unless valid_mode?(mode)

  options[:terminal_mode] = mode
end

#threaded(boolean) ⇒ Boolean Also known as: threaded=

Parameters:

Returns:



315
316
317
# File 'lib/vedeu/configuration/api.rb', line 315

def threaded(boolean)
  options[:threaded] = boolean
end

#valid_colour_mode?(value) ⇒ Boolean (private)

Checks that the value provided to #colour_mode is valid.

Parameters:

  • value (Fixnum)

Returns:



362
363
364
# File 'lib/vedeu/configuration/api.rb', line 362

def valid_colour_mode?(value)
  numeric?(value) && [8, 16, 256, 16_777_216].include?(value)
end

#valid_mode?(mode) ⇒ Boolean (private)

Checks that the mode provided is valid.

Parameters:

  • mode (Symbol)

    :cooked, :fake or :raw are valid

Returns:



370
371
372
# File 'lib/vedeu/configuration/api.rb', line 370

def valid_mode?(mode)
  [:cooked, :fake, :raw].include?(mode)
end

#width(width = 80) ⇒ Fixnum Also known as: width=

Parameters:

  • width (Fixnum) (defaults to: 80)

Returns:

  • (Fixnum)


323
324
325
# File 'lib/vedeu/configuration/api.rb', line 323

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