Class: EacRubyUtils::Inflector

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_ruby_utils/inflector.rb

Constant Summary collapse

VARIABLE_NAME_PATTERN =
/[_a-z][_a-z0-9]*/i.freeze

Class Method Summary collapse

Class Method Details

.variableize(string) ⇒ Object

Raises:

  • (::ArgumentError)

8
9
10
11
12
13
14
15
# File 'lib/eac_ruby_utils/inflector.rb', line 8

def variableize(string)
  r = string.gsub(/[^_a-z0-9]/i, '_').gsub(/_+/, '_').gsub(/_\z/, '').gsub(/\A_/, '').downcase
  m = VARIABLE_NAME_PATTERN.match(r)
  return r if m

  raise ::ArgumentError, "Invalid variable name \"#{r}\" was generated " \
    "from string \"#{string}\""
end