Sha256: ca73151d0d5d12a02042bfabef4724325460f69dc3399acbdad7ebdfbda6b4cd

Contents?: true

Size: 1.41 KB

Versions: 8

Compression:

Stored size: 1.41 KB

Contents

module Zeitwerk
  class Inflector
    # 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`.
    #
    # @param basename [String]
    # @param _abspath [String]
    # @return [String]
    def camelize(basename, _abspath)
      overrides[basename] || basename.split('_').map!(&:capitalize).join
    end

    # 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"
    #
    # @param inflections [{String => String}]
    # @return [void]
    def inflect(inflections)
      overrides.merge!(inflections)
    end

    private

    # Hard-coded basename to constant name user maps that override the default
    # inflection logic.
    #
    # @return [{String => String}]
    def overrides
      @overrides ||= {}
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
opal-zeitwerk-0.3.0 opal/zeitwerk/inflector.rb
opal-zeitwerk-0.2.4 opal/zeitwerk/inflector.rb
opal-zeitwerk-0.2.3 opal/zeitwerk/inflector.rb
opal-zeitwerk-0.2.2 opal/zeitwerk/inflector.rb
opal-zeitwerk-0.2.1 opal/zeitwerk/inflector.rb
opal-zeitwerk-0.2.0 opal/zeitwerk/inflector.rb
opal-zeitwerk-0.1.0 opal/zeitwerk/inflector.rb
opal-zeitwerk-0.0.4 opal/zeitwerk/inflector.rb