Module: SimpleInflector

Extended by:
SimpleInflector
Included in:
SimpleInflector
Defined in:
lib/simple_inflector.rb,
lib/simple_inflector/version.rb

Overview

This file should not require anything, as it’s loaded in the ‘*.gemspec` file.

Constant Summary collapse

VERSION =

The version of this Gem.

[ 0, 0, 1 ].freeze
VERSION_STR =

The version of this Gem as a string.

VERSION.map(& :to_s).join('.').freeze

Instance Method Summary collapse

Instance Method Details

#underscore(klassname) ⇒ String

Makes an underscored, lowercase form from a camel-case class name (that may include a namespace).

Changes “‘::`” to “`/`” to convert namespaces to (file system) paths (to be used for `require`).

Examples:

  "FooBar"      .underscore  #=> "foo_bar"
  "FooBar::Baz" .underscore  #=> "foo_bar/baz"
"::FooBar::Baz" .underscore  #=> "foo_bar/baz"
  "HTTPRequest" .underscore  #=> "http_request"

Parameters:

  • klassname (String)

    Class name (may include module name).

Returns:

  • (String)

    Underscored name.



20
21
22
23
24
25
26
27
28
29
# File 'lib/simple_inflector.rb', line 20

def underscore( klassname )
	s = klassname.to_s.dup
	s.gsub!( /^::/, '' )  # get rid of leading "::"
	s.gsub!( /::/, '/' )  # replace namespace separator by "/"
	s.gsub!( /([A-Z0-9]+)([A-Z][a-z])/x ,'\\1_\\2' )  # un-camel-case
	s.gsub!( /([a-z0-9] )([A-Z]     )/x ,'\\1_\\2' )  # "
	s.tr!( '-', '_' )  # replace hyphens by underscores
	s.downcase!  # lowercase. who would have thought?
	s
end