Class: Zeitwerk::Inflector

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

Direct Known Subclasses

GemInflector

Instance Method Summary collapse

Instance Method Details

#camelize(basename, _abspath) ⇒ String

Very basic snake case -> camel case conversion.

inflector = Zeitwerk::Inflector.new
inflector.camelize("post", ...)             # => "Post"
inflector.camelize("users_controller", ...) # => "UsersController"
inflector.camelize("api", ...)              # => "Api"

Takes into account hard-coded mappings configured with ‘inflect`.

Parameters:

  • basename (String)
  • _abspath (String)

Returns:

  • (String)


17
18
19
# File 'lib/zeitwerk/inflector.rb', line 17

def camelize(basename, _abspath)
  overrides[basename] || basename.split('_').each(&:capitalize!).join
end

#inflect(inflections) ⇒ void

This method returns an undefined value.

Configures hard-coded inflections:

inflector = Zeitwerk::Inflector.new
inflector.inflect(
  "html_parser"   => "HTMLParser",
  "mysql_adapter" => "MySQLAdapter"
)

inflector.camelize("html_parser", abspath)      # => "HTMLParser"
inflector.camelize("mysql_adapter", abspath)    # => "MySQLAdapter"
inflector.camelize("users_controller", abspath) # => "UsersController"

Parameters:

  • inflections ({String => String})


35
36
37
# File 'lib/zeitwerk/inflector.rb', line 35

def inflect(inflections)
  overrides.merge!(inflections)
end