Module: Bauk::Gen::ConfigUtils
Defined Under Namespace
Classes: ConfigError
Instance Method Summary collapse
-
#underscore(camel_cased_word) ⇒ Object
Function copied from online thread.
-
#validate_config ⇒ Object
Method to check/validate the config being provided to the generator.
-
#validate_config_item(keys, map = {}) ⇒ Object
Method that can be used to validate individual config items.
Instance Method Details
#underscore(camel_cased_word) ⇒ Object
Function copied from online thread
58 59 60 61 62 63 64 |
# File 'lib/bauk/gen/config_utils.rb', line 58 def underscore(camel_cased_word) camel_cased_word.to_s.gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .downcase end |
#validate_config ⇒ Object
Method to check/validate the config being provided to the generator. It can, for example, be used to ensure mandatory config is present or that given config is in the correct format.
55 |
# File 'lib/bauk/gen/config_utils.rb', line 55 def validate_config; end |
#validate_config_item(keys, map = {}) ⇒ Object
Method that can be used to validate individual config items. Takes an array of keys (or singular key) that specify the item to be checked
(e.g. :a would check that {a: 123}, while [:a, :b] would check the following nested hash: {a: {b: 123}}
The second argument is a hash of optional things to check. If this is not given, the check is merely to ensure that the value is set and not null. Optional items to check are:
- allow_null : allows the value to be null or not exist
- matches : specify a regex that the item needs to match
- options : specify a list of valid options
- message : specify an optional default error message to display
- null_message : specify an optional error message to display if the value is nil
- matches_message : specify an optional error message to display if the value is not in the valid list of options
- options_message : specify an optional error message to display if the value does not match the given regex
- default_value : Specify a default value in case of null
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 |
# File 'lib/bauk/gen/config_utils.rb', line 24 def validate_config_item(keys, map = {}) config_item = config keys = [keys] unless keys.is_a? Array parent_keys = keys.clone key = parent_keys.pop # Get value we are referring to parent_keys.each do |parent_key| config_item[parent_key] = {} unless config_item[parent_key].is_a?(Hash) config_item = config_item[parent_key] end # Run checks if config_item[key].nil? if map[:default_value] log.debug "Setting #{keys.join('->')} to default value: #{map[:default_value]}" config_item[key] = map[:default_value] else map[:nil_message] ||= map[:message] || "Config item #{keys.join('->')} does not exist" raise ConfigError, map[:nil_message] unless map[:allow_null] end elsif map[:matches] && config_item[key] !~ (map[:matches]) map[:matches_message] ||= map[:message] || "Config #{keys.join('->')}(#{config_item[key]}) does not match: '#{map[:matches]}'" raise ConfigError, map[:matches_message] elsif map[:options] && ! map[:options].include?(config_item[key]) map[:options_message] ||= map[:message] || "Config #{keys.join('->')}(#{config_item[key]}) needs to be one of: '#{map[:options].join(", ")}'" raise ConfigError, map[:options_message] end end |