class Aruba::BasicConfiguration

Basic configuration for Aruba

@private

Basic Configuration

Attributes

hooks[W]
local_options[RW]

Public Class Methods

known_options() click to toggle source
# File lib/aruba/basic_configuration.rb, line 14
def known_options
  @known_options ||= {}
end
new() click to toggle source

Create configuration

# File lib/aruba/basic_configuration.rb, line 99
def initialize
  initialize_configuration
end
option_accessor(name, opts = {}) { |in_config_wrapper| ... } click to toggle source

Define an option reader and writer

@param [Symbol] name

The name of the reader

@param [Hash] opts

Options

@option [Class, Module] contract

The contract for the option

@option [Object] default

The default value
# File lib/aruba/basic_configuration.rb, line 60
def option_accessor(name, opts = {})
  contract = opts[:contract]
  default  = opts[:default]

  fail ArgumentError, 'Either use block or default value' if block_given? && default
  # fail ArgumentError, 'Either use block or default value' if !block_given? && default.nil? && default.to_s.empty?
  fail ArgumentError, 'contract-options is required' if contract.nil?

  # Add writer
  add_option(name, block_given? ? yield(InConfigWrapper.new(known_options)) : default)

  Contract contract
  define_method("#{name}=") { |v| find_option(name).value = v }

  # Add reader
  option_reader name, :contract => { None => contract.values.first }
end
option_reader(name, opts = {}) { |in_config_wrapper| ... } click to toggle source

Define an option reader

@param [Symbol] name

The name of the reader

@param [Hash] opts

Options

@option [Class, Module] contract

The contract for the option

@option [Object] default

The default value
# File lib/aruba/basic_configuration.rb, line 31
def option_reader(name, opts = {})
  contract = opts[:contract]
  default  = opts[:default]

  fail ArgumentError, 'Either use block or default value' if block_given? && default
  fail ArgumentError, 'contract-options is required' if contract.nil?

  Contract contract
  add_option(name, block_given? ? yield(InConfigWrapper.new(known_options)) : default)

  define_method(name) { find_option(name).value }

  self
end

Private Class Methods

add_option(name, value = nil) click to toggle source
# File lib/aruba/basic_configuration.rb, line 80
def add_option(name, value = nil)
  return if known_options.key?(name)

  known_options[name] = Option.new(:name => name, :value => value)

  self
end

Public Instance Methods

==(other) click to toggle source
# File lib/aruba/basic_configuration.rb, line 212
def ==(other)
  local_options.values.map(&:value) == other.local_options.values.map(&:value)
end
after(name, context = proc {}, *args, &block) click to toggle source

Define or run after-hook

@param [Symbol, String] name

The name of the hook

@param [Proc] context

The context a hook should run in. This is a runtime only option.

@param [Array] args

Arguments for the run of hook. This is a runtime only option.

@yield

The code block which should be run. This is a configure time only option
# File lib/aruba/basic_configuration.rb, line 178
def after(name, context = proc {}, *args, &block)
  name = format('%s_%s', 'after_', name.to_s).to_sym

  if block_given?
    @hooks.append(name, block)

    self
  else
    @hooks.execute(name, context, *args)
  end
end
after?(name) click to toggle source

Check if after-hook <name> is defined

# File lib/aruba/basic_configuration.rb, line 198
def after?(name)
  name = format('%s_%s', 'after_', name.to_s).to_sym

  @hooks.exist? name
end
before(name, context = proc {}, *args, &block) click to toggle source

Define or run before-hook

@param [Symbol, String] name

The name of the hook

@param [Proc] context

The context a hook should run in. This is a runtime only option.

@param [Array] args

Arguments for the run of hook. This is a runtime only option.

@yield

The code block which should be run. This is a configure time only option
# File lib/aruba/basic_configuration.rb, line 153
def before(name, context = proc {}, *args, &block)
  name = format('%s_%s', 'before_', name.to_s).to_sym

  if block_given?
    @hooks.append(name, block)

    self
  else
    @hooks.execute(name, context, *args)
  end
end
before?(name) click to toggle source

Check if before-hook <name> is defined

# File lib/aruba/basic_configuration.rb, line 191
def before?(name)
  name = format('%s_%s', 'before_', name.to_s).to_sym

  @hooks.exist? name
end
before_cmd(&block) click to toggle source

@deprecated

# File lib/aruba/basic_configuration.rb, line 134
def before_cmd(&block)
  Aruba.platform.deprecated 'The use of the "#before_cmd"-hook is deprecated. Please define with "#before(:command) {}" instead'

  before(:command, &block)
end
configure() { |self| ... } click to toggle source

@yield [Configuration]

Yields self
# File lib/aruba/basic_configuration.rb, line 106
def configure
  yield self if block_given?
end
hooks() click to toggle source

Get access to hooks

# File lib/aruba/basic_configuration.rb, line 125
def hooks
  # rubocop:disable Metrics/LineLength
  Aruba.platform.deprecated 'The use of the "#aruba.config.hooks" is deprecated. Please use "#aruba.config.before(:name) {}" to define and "#aruba.config.before(:name, *args)" to run a hook. This method will become private in the next major version.'
  # rubocop:enable Metrics/LineLength

  @hooks
end
make_copy() click to toggle source

Make deep dup copy of configuration

# File lib/aruba/basic_configuration.rb, line 116
def make_copy
  obj = self.dup
  obj.local_options = Marshal.load(Marshal.dump(local_options))
  obj.hooks         = @hooks

  obj
end
option?(name) click to toggle source

Check if <name> is option

@param [String, Symbol] name

The name of the option
# File lib/aruba/basic_configuration.rb, line 208
def option?(name)
  local_options.any? { |_, v| v.name == name.to_sym }
end
reset() click to toggle source

Reset configuration

# File lib/aruba/basic_configuration.rb, line 111
def reset
  initialize_configuration
end
set_if_option(name, *args) click to toggle source

Set if name is option

# File lib/aruba/basic_configuration.rb, line 217
def set_if_option(name, *args)
  if RUBY_VERSION < '1.9'
    send("#{name}=".to_sym, *args) if option? name
  else
    public_send("#{name}=".to_sym, *args) if option? name
  end
end

Private Instance Methods

find_option(name) click to toggle source
# File lib/aruba/basic_configuration.rb, line 232
def find_option(name)
  fail NotImplementedError, %(Unknown option "#{name}") unless option? name

  local_options[name]
end
initialize_configuration() click to toggle source
# File lib/aruba/basic_configuration.rb, line 227
def initialize_configuration
  @local_options = Marshal.load(Marshal.dump(self.class.known_options))
  @hooks         = Hooks.new
end