class Aruba::BasicConfiguration
Basic configuration for Aruba
@private
Basic Configuration
Attributes
Public Class Methods
Source
# File lib/aruba/basic_configuration.rb, line 16 def known_options @known_options ||= {} end
Source
# File lib/aruba/basic_configuration.rb, line 82 def initialize initialize_configuration end
Create configuration
Source
# File lib/aruba/basic_configuration.rb, line 50 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 } # Add reader option_reader name, type: type end
Define an option reader and writer
@param [Symbol] name
The name of the reader
@option [Class, Module] type
The type contract for the option
@option [Object] default
The default value
Source
# File lib/aruba/basic_configuration.rb, line 30 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
Define an option reader
@param [Symbol] name
The name of the reader
@option [Class, Module] type
The type contract for the option
@option [Object] default
The default value
Private Class Methods
Source
# File lib/aruba/basic_configuration.rb, line 65 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
Source
# File lib/aruba/basic_configuration.rb, line 179 def ==(other) local_options.values.map(&:value) == other.local_options.values.map(&:value) end
Source
# File lib/aruba/basic_configuration.rb, line 146 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
Define after-hook
@param [Symbol, String] name
The name of the hook
@yield
The code block which should be run. This is a configure time only option
Source
# File lib/aruba/basic_configuration.rb, line 114 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
Define before-hook
@param [Symbol, String] name
The name of the hook
@yield
The code block which should be run. This is a configure time only option
Source
# File lib/aruba/basic_configuration.rb, line 89 def configure yield self if block_given? end
@yield [Configuration]
Yields self
Source
# File lib/aruba/basic_configuration.rb, line 99 def make_copy obj = dup obj.local_options = Marshal.load(Marshal.dump(local_options)) obj.hooks = @hooks obj end
Make deep dup copy of configuration
Source
# File lib/aruba/basic_configuration.rb, line 175 def option?(name) local_options.any? { |_, v| v.name == name.to_sym } end
Check if <name> is option
@param [String, Symbol] name
The name of the option
Source
# File lib/aruba/basic_configuration.rb, line 94 def reset initialize_configuration end
Reset configuration
Source
# File lib/aruba/basic_configuration.rb, line 165 def run_after_hook(name, context, *args) name = format('%s_%s', 'after_', name.to_s).to_sym @hooks.execute(name, context, *args) end
Run after-hook
@param [Symbol, String] name
The name of the hook
@param [Proc] context
The context a hook should run in
@param [Array] args
Arguments for the run of hook
Source
# File lib/aruba/basic_configuration.rb, line 133 def run_before_hook(name, context, *args) name = format('%s_%s', 'before_', name.to_s).to_sym @hooks.execute(name, context, *args) end
Run before-hook
@param [Symbol, String] name
The name of the hook
@param [Proc] context
The context a hook should run in
@param [Array] args
Arguments for the run of hook
Source
# File lib/aruba/basic_configuration.rb, line 184 def set_if_option(name, *args) public_send(:"#{name}=", *args) if option? name end
Set if name is option
Private Instance Methods
Source
# File lib/aruba/basic_configuration.rb, line 195 def find_option(name) raise NotImplementedError, %(Unknown option "#{name}") unless option? name local_options[name] end
Source
# File lib/aruba/basic_configuration.rb, line 190 def initialize_configuration @local_options = Marshal.load(Marshal.dump(self.class.known_options)) @hooks = Hooks.new end