class EacRubyUtils::Inflector
Constants
- VARIABLE_NAME_PATTERN
Public Class Methods
variableize(string, validate = true)
click to toggle source
Convert a string to a variable format: first character as a lowercase letter or underscore and other as a lowercase letter, underscore or numbers. @param string [String] The source string. @param validate [Boolean] Affect the outcome when the result builded is not in a valid
variable format. If `true`, it raises a {ArgumentError}. If `false`, return `nil`.
@return [String, nil] @raise [ArgumentError]
# File lib/eac_ruby_utils/inflector.rb, line 17 def variableize(string, validate = true) 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