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.

API:

  • public

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:

API:

  • public



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:

API:

  • public



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

def default
  @default
end

Class Method Details

.configure(default, &block) ⇒ Object

Parameters:

API:

  • public



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:

  • (defaults to: nil)

Returns:

API:

  • public



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:

  • (defaults to: nil)

Returns:

API:

  • public



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:

  • (defaults to: {})

Returns:

API:

  • public



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:

  • (defaults to: nil)

Returns:

Raises:

  • 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.

API:

  • public



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:

  • (defaults to: true)

Returns:

API:

  • public



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:

API:

  • public



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:

API:

  • public



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:

  • (defaults to: true)

Returns:

API:

  • public



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:

  • (defaults to: true)

Returns:

API:

  • public



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:

  • (defaults to: 25)

Returns:

API:

  • public



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:

  • (defaults to: '')

Returns:

API:

  • public



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:

  • (defaults to: '')

Returns:

API:

  • public



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:

  • (defaults to: 80)

Returns:

API:

  • public



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:

API:

  • public



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:

  • (defaults to: nil)

Returns:

API:

  • public



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:

  • (defaults to: 25)

Returns:

API:

  • public



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:

  • (defaults to: true)

Returns:

API:

  • public



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

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

#invalid_mode!Object (private)

Raises:

  • 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.

API:

  • public



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:

  • (defaults to: false)

Returns:

API:

  • public



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:

  • The message types which should not be logged.

Returns:

API:

  • public



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:

  • The message types which should be logged.

Returns:

API:

  • public



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:

API:

  • public



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:

  • (defaults to: true)

Returns:

API:

  • public



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:

API:

  • public



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

def options
  @options ||= {}
end

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

Parameters:

  • (defaults to: true)

Returns:

API:

  • public



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:

API:

  • public



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:

Returns:

API:

  • public



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

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

#root(*args) ⇒ Class

Parameters:

Returns:

API:

  • public



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:

  • (defaults to: true)

Returns:

API:

  • public



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:

  • (defaults to: true)

Returns:

API:

  • public



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

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

#stderr(io) ⇒ File|IO

Parameters:

Returns:

API:

  • public



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

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

#stdin(io) ⇒ File|IO

Parameters:

Returns:

API:

  • public



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

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

#stdout(io) ⇒ File|IO

Parameters:

Returns:

API:

  • public



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:

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

Returns:

Raises:

  • 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:

API:

  • public



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:

API:

  • public



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:

Returns:

API:

  • public



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:

  • :cooked, :fake or :raw are valid

Returns:

API:

  • public



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:

  • (defaults to: 80)

Returns:

API:

  • public



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

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