/usr/lib/ruby/vendor_ruby/aruba/basic_configuration.rb is in ruby-aruba 0.14.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | require 'contracts'
require 'aruba/basic_configuration/option'
require 'aruba/in_config_wrapper'
# Aruba
module Aruba
# Basic configuration for Aruba
#
# @private
class BasicConfiguration
include Contracts
class << self
def known_options
@known_options ||= {}
end
# 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
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
# 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
#
# rubocop:disable Metrics/CyclomaticComplexity
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
# rubocop:enable Metrics/CyclomaticComplexity
private
def add_option(name, value = nil)
return if known_options.key?(name)
known_options[name] = Option.new(:name => name, :value => value)
self
end
end
protected
attr_accessor :local_options
attr_writer :hooks
# attr_reader :hooks
public
# Create configuration
def initialize
initialize_configuration
end
# @yield [Configuration]
#
# Yields self
def configure
yield self if block_given?
end
# Reset configuration
def reset
initialize_configuration
end
# Make deep dup copy of configuration
def make_copy
obj = self.dup
obj.local_options = Marshal.load(Marshal.dump(local_options))
obj.hooks = @hooks
obj
end
# Get access to hooks
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
# @deprecated
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
# 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
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
# 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
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
# Check if before-hook <name> is defined
def before?(name)
name = format('%s_%s', 'before_', name.to_s).to_sym
@hooks.exist? name
end
# Check if after-hook <name> is defined
def after?(name)
name = format('%s_%s', 'after_', name.to_s).to_sym
@hooks.exist? name
end
# Check if <name> is option
#
# @param [String, Symbol] name
# The name of the option
def option?(name)
local_options.any? { |_, v| v.name == name.to_sym }
end
def ==(other)
local_options.values.map(&:value) == other.local_options.values.map(&:value)
end
# Set if name is option
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
def initialize_configuration
@local_options = Marshal.load(Marshal.dump(self.class.known_options))
@hooks = Hooks.new
end
def find_option(name)
fail NotImplementedError, %(Unknown option "#{name}") unless option? name
local_options[name]
end
end
end
|