Class: Aruba::BasicConfiguration

Inherits:
Object
  • Object
show all
Includes:
Contracts
Defined in:
lib/aruba/basic_configuration.rb,
lib/aruba/basic_configuration/option.rb

Overview

Basic Configuration

Direct Known Subclasses

Configuration

Defined Under Namespace

Classes: Option

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBasicConfiguration

Create configuration



99
100
101
# File 'lib/aruba/basic_configuration.rb', line 99

def initialize
  initialize_configuration
end

Instance Attribute Details

#hooksObject

Get access to hooks



125
126
127
128
129
130
131
# 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

Class Method Details

.known_optionsObject



14
15
16
# File 'lib/aruba/basic_configuration.rb', line 14

def known_options
  @known_options ||= {}
end

.option_accessor(name, opts = {}) ⇒ Object

Define an option reader and writer

Parameters:

  • name (Symbol)

    The name of the reader

  • opts (Hash) (defaults to: {})

    Options

  • [Class, (Hash)

    a customizable set of options

  • [Object] (Hash)

    a customizable set of options



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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 = {}) ⇒ Object

Define an option reader

Parameters:

  • name (Symbol)

    The name of the reader

  • opts (Hash) (defaults to: {})

    Options

  • [Class, (Hash)

    a customizable set of options

  • [Object] (Hash)

    a customizable set of options



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

Instance Method Details

#==(other) ⇒ Object



212
213
214
# 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) { ... } ⇒ Object

Define or run after-hook

Parameters:

  • name (Symbol, String)

    The name of the hook

  • context (Proc) (defaults to: proc {})

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

  • args (Array)

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

Yields:

  • The code block which should be run. This is a configure time only option



178
179
180
181
182
183
184
185
186
187
188
# 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) ⇒ Boolean

Check if after-hook is defined

Returns:

  • (Boolean)


198
199
200
201
202
# 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) { ... } ⇒ Object

Define or run before-hook

Parameters:

  • name (Symbol, String)

    The name of the hook

  • context (Proc) (defaults to: proc {})

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

  • args (Array)

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

Yields:

  • The code block which should be run. This is a configure time only option



153
154
155
156
157
158
159
160
161
162
163
# 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) ⇒ Boolean

Check if before-hook is defined

Returns:

  • (Boolean)


191
192
193
194
195
# 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) ⇒ Object

Deprecated.


134
135
136
137
138
# 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 {|Configuration| ... } ⇒ Object

Yields:



106
107
108
# File 'lib/aruba/basic_configuration.rb', line 106

def configure
  yield self if block_given?
end

#make_copyObject

Make deep dup copy of configuration



116
117
118
119
120
121
122
# 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) ⇒ Boolean

Check if is option

Parameters:

  • name (String, Symbol)

    The name of the option

Returns:

  • (Boolean)


208
209
210
# File 'lib/aruba/basic_configuration.rb', line 208

def option?(name)
  local_options.any? { |_, v| v.name == name.to_sym }
end

#resetObject

Reset configuration



111
112
113
# File 'lib/aruba/basic_configuration.rb', line 111

def reset
  initialize_configuration
end

#set_if_option(name, *args) ⇒ Object

Set if name is option



217
218
219
220
221
222
223
# 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