class EacRubyUtils::Inflector

def variableize(string, validate = true) # rubocop:disable Style/OptionalBooleanParameter

Raises:
  • (ArgumentError) -

Returns:
  • (String, nil) -

Parameters:
  • validate (Boolean) -- Affect the outcome when the result builded is not in a valid
  • string (String) -- The source string.
def variableize(string, validate = true) # rubocop:disable Style/OptionalBooleanParameter
  r = ::ActiveSupport::Inflector.transliterate(string).gsub(/[^_a-z0-9]/i, '_')
        .gsub(/_+/, '_').gsub(/_\z/, '').gsub(/\A_/, '').downcase
  m = VARIABLE_NAME_PATTERN.match(r)
  return r if m
  return nil unless validate
  raise ::ArgumentError, "Invalid variable name \"#{r}\" was generated " \
                         "from string \"#{string}\""
end