lib/rconf/configurator.rb in rconf-0.9.25 vs lib/rconf/configurator.rb in rconf-0.10.0
- old
+ new
@@ -20,20 +20,17 @@
module ClassMethods
# Key associated with configurator
attr_reader :key
-
+
# Description
attr_reader :desc
# Access to settings for documentation
attr_reader :all_settings
- # Access to required settings for validation
- attr_reader :required_settings
-
# Associate configurator with given key
#
# === Parameters
# key(Symbol):: Key configurator should be associated with
#
@@ -55,37 +52,30 @@
def description(description)
@desc = description
true
end
- # Store settings and their descriptions in a hash
+ # Store setting definition
+ # A definition consists of a name, a description and optionally options
+ # Possible options are:
+ # :required => true|false Whether the setting is required
#
# === Parameters
# settings(Hash):: Settings descriptions indexed by names
#
# === Return
# true:: Always return true
- def settings(settings)
- default_settings = { :only_if => 'Ruby code that should return true for configurator to proceed' }
- @all_settings = settings.merge(default_settings)
+ def setting(name, description, options={})
+ @all_settings ||= [{ :name => 'only_if',
+ :description => 'Ruby code that should return true for configurator to proceed',
+ :options => {} }]
+ @all_settings << { :name => name, :description => description, :options => options || {} }
true
end
- # Store required settings for validation
- #
- # === Parameters
- # settings(Array):: List of settings that should be checked
- #
- # === Return
- # true:: Always return true
- def validate_has_settings(*settings)
- @required_settings = settings.flatten
- true
- end
+ end
- end
-
# Extend base class with ClassMethods module methods
#
# === Parameters
# base(Object):: Object including module
def self.included(base)
@@ -96,20 +86,25 @@
#
# === Return
# nil:: If settings are valid for this configurator
# error(String):: Error message otherwise
def validate
- required = self.class.required_settings
- return nil unless required
- missing = required.flatten.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 "
+ @settings_values.merge!(OverridesLanguage.overrides_for(self.class.key.to_s)) unless Profile.force_reconfigure?
+ 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 += "missing for configuration section '#{self.class.key}'" if error
error
end
# Check system to determine whether configurator needs to run
#
@@ -122,11 +117,11 @@
# Run configurator for current platform
#
# === Parameters
# args:: Pass-through arguments, given to platform specific implementation
- #
+ #
# === Return
# true:: Always return true
def run(*args)
key = "#{self.class.key}-#{@index}"
sha = Profile.configurator_signature(key)
@@ -166,11 +161,11 @@
#
# === Returns
# value:: Value of configuration option if there is one
# nil:: Otherwise
def [](config_option)
- @settings_values[config_option]
+ @settings_values[config_option.to_s]
end
protected
# DSL implementation, set settings value if arguments, get it otherwise.
@@ -190,13 +185,13 @@
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] = value
+ res = @settings_values[meth.to_s] = value
end
end
- res || @settings_values[meth]
+ res || @settings_values[meth.to_s]
end
# Initialize configuration settings hash and index
#
# === Parameters