Module: RightConf::Configurator

Overview

Configurator mixin, defines DSL and common validation method

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ProgressReporter

report_to_file, report_to_stdout

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object (protected)

DSL implementation, set settings value if arguments, get it otherwise.

Parameters

meth(Symbol)

Method symbol

args(Array)

List of arguments

Return

res(Object)

Configuration setting value or setter return value if arguments



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/rconf/configurator.rb', line 201

def method_missing(meth, *args)
  num_args = args.length
  res = nil
  if num_args > 0
    meth = $1.to_sym unless (meth.to_s =~ /(.+)=$/).nil?
    value = num_args == 1 ? args[0] : args
    method_name = meth.id2name
    if self.public_methods.include?("#{method_name}=")
      res = self.send("#{method_name}=", value)
    else
      res = @settings_values[meth.to_s] = value
    end
  end
  res || @settings_values[meth.to_s]
end

Class Method Details

.included(base) ⇒ Object

Extend base class with ClassMethods module methods

Parameters

base(Object)

Object including module



96
97
98
# File 'lib/rconf/configurator.rb', line 96

def self.included(base)
  base.__send__(:extend, ClassMethods)
end

Instance Method Details

#[](config_option) ⇒ Object

Get value of configuration option

Parameters

config_option(Symbol)

Configuration option to return

Returns

value

Value of configuration option if there is one

nil

Otherwise



186
187
188
# File 'lib/rconf/configurator.rb', line 186

def [](config_option)
  @settings_values[config_option.to_s]
end

#checkObject

Check system to determine whether configurator needs to run

Return

true

If configurator needs to run

false

Otherwise



129
130
131
# File 'lib/rconf/configurator.rb', line 129

def check
  Platform.dispatch { :check }
end

#post_processObject

Called even if configuration is already done for steps that must always happen, do nothing by default

Return

true

Always return true



164
165
166
# File 'lib/rconf/configurator.rb', line 164

def post_process
  true
end

#run(*args) ⇒ Object

Run configurator for current platform

Parameters

args

Pass-through arguments, given to platform specific implementation

Return

true

Always return true



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rconf/configurator.rb', line 140

def run(*args)
    key = "#{self.class.key}-#{@index}"
    sha = Profile.configurator_signature(key)
    sig = signature
    must_configure = Profile.force_reconfigure?
    if !must_configure && (only_if.nil? || instance_eval(only_if))
      must_configure = (Profile.force_check? || sha != sig) && !check
    end
    Platform.dispatch(*args) { :run } if must_configure

    # clear any leftover signature in case of aborting to prevent rconf
    # doing nothing on restart.
    #
    # FIX: there seems to be an issue when a new ruby version is installed
    # and rconf aborts and has to be restarted (on Ubuntu?)
    Profile.set_configurator_signature(key, aborting ? nil : sig)
  true
end

#signatureObject

Calculate unique SHA for current settings

Return

sha(String)

SHA for current settings



172
173
174
175
176
# File 'lib/rconf/configurator.rb', line 172

def signature
  blob = VERSION
  blob = @settings_values.inject(blob) { |b, (k, v)| b += "#{k}:#{v};" } if @settings_values
  sha = Digest::SHA1.hexdigest(blob)
end

#validateObject

Check whether configurator has values for all required settings

Return

nil

If settings are valid for this configurator

error(String)

Error message otherwise



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rconf/configurator.rb', line 105

def validate
  @settings_values.merge!(OverridesLanguage.overrides_for(self.class.key.to_s))
  if e = OverridesLanguage.parse_error
    error = "Could not load overrides file '#{OverridesLanguage::OVERRIDES_FILE}' (#{e.message})"
  else
    required = self.class.all_settings.select { |s| s[:options][:required] }.map { |s| s[:name] }
    return nil unless required
    missing = required.select { |s| !@settings_values.include?(s) }
    error = case missing.size
            when 0 then nil
            when 1 then "Required setting #{missing.first} is "
            else
              "Required settings #{missing.join(', ')} are "
            end
    error += "missing for configuration section '#{self.class.key}'" if error
  end
  error
end