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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ BasicConfiguration

Create configuration



76
77
78
# File 'lib/aruba/basic_configuration.rb', line 76

def initialize
  initialize_configuration
end

Class Method Details

.known_options ⇒ Object



16
17
18
# File 'lib/aruba/basic_configuration.rb', line 16

def known_options
  @known_options ||= {}
end

.option_accessor(name, type:, default: nil) ⇒ Object

Define an option reader and writer

Parameters:

  • type (Class, Module) —

    The type contract for the option

  • default (Object) (defaults to: nil) —

    The default value

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/aruba/basic_configuration.rb', line 44

def option_accessor(name, type:, default: nil)
  raise ArgumentError, 'Either use block or default value' if block_given? && default

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

  Contract type => type
  define_method(:"#{name}=") { |v| find_option(name).value = v }

  Contract None => type
  define_method(name) { find_option(name).value }
end

.option_reader(name, type:, default: nil) ⇒ Object

Define an option reader

Parameters:

  • type (Class, Module) —

    The type contract for the option

  • default (Object) (defaults to: nil) —

    The default value

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
# File 'lib/aruba/basic_configuration.rb', line 27

def option_reader(name, type:, default: nil)
  raise ArgumentError, 'Either use block or default value' if block_given? && default

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

  Contract None => type
  define_method(name) { find_option(name).value }
end

Instance Method Details

#==(other) ⇒ Object



173
174
175
# File 'lib/aruba/basic_configuration.rb', line 173

def ==(other)
  local_options.values.map(&:value) == other.local_options.values.map(&:value)
end

#after(name) { ... } ⇒ Object

Define after-hook

Parameters:

  • name (Symbol, String) —

    The name of the hook

Yields:

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

Raises:

  • (ArgumentError)


140
141
142
143
144
145
146
147
# File 'lib/aruba/basic_configuration.rb', line 140

def after(name, &block)
  name = format('%s_%s', 'after_', name.to_s).to_sym
  raise ArgumentError, 'A block is required' unless block

  @hooks.append(name, block)

  self
end

#before(name) { ... } ⇒ Object

Define before-hook

Parameters:

  • name (Symbol, String) —

    The name of the hook

Yields:

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

Raises:

  • (ArgumentError)


108
109
110
111
112
113
114
115
# File 'lib/aruba/basic_configuration.rb', line 108

def before(name, &block)
  name = format('%s_%s', 'before_', name.to_s).to_sym
  raise ArgumentError, 'A block is required' unless block

  @hooks.append(name, block)

  self
end

#configure {|Configuration| ... } ⇒ Object

Yields:



83
84
85
# File 'lib/aruba/basic_configuration.rb', line 83

def configure
  yield self if block_given?
end

#make_copy ⇒ Object

Make deep dup copy of configuration



93
94
95
96
97
98
99
# File 'lib/aruba/basic_configuration.rb', line 93

def make_copy
  obj = 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)


169
170
171
# File 'lib/aruba/basic_configuration.rb', line 169

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

#reset ⇒ Object

Reset configuration



88
89
90
# File 'lib/aruba/basic_configuration.rb', line 88

def reset
  initialize_configuration
end

#run_after_hook(name, context, *args) ⇒ Object

Run after-hook

Parameters:

  • name (Symbol, String) —

    The name of the hook

  • context (Proc) —

    The context a hook should run in

  • args (Array) —

    Arguments for the run of hook



159
160
161
162
163
# File 'lib/aruba/basic_configuration.rb', line 159

def run_after_hook(name, context, *args)
  name = format('%s_%s', 'after_', name.to_s).to_sym

  @hooks.execute(name, context, *args)
end

#run_before_hook(name, context, *args) ⇒ Object

Run before-hook

Parameters:

  • name (Symbol, String) —

    The name of the hook

  • context (Proc) —

    The context a hook should run in

  • args (Array) —

    Arguments for the run of hook



127
128
129
130
131
# File 'lib/aruba/basic_configuration.rb', line 127

def run_before_hook(name, context, *args)
  name = format('%s_%s', 'before_', name.to_s).to_sym

  @hooks.execute(name, context, *args)
end

#set_if_option(name, *args) ⇒ Object

Set if name is option



178
179
180
# File 'lib/aruba/basic_configuration.rb', line 178

def set_if_option(name, *args)
  public_send(:"#{name}=", *args) if option? name
end