Module: SimpleCommandDispatcher::Helpers::Camelize

Includes:
TrimAll
Included in:
Services::CommandNamespaceService, Services::CommandService
Defined in:
lib/simple_command_dispatcher/helpers/camelize.rb

Instance Method Summary collapse

Methods included from TrimAll

#trim_all

Instance Method Details

#camelize(token) ⇒ String

Transforms a RESTful route into a Ruby constant string for instantiation

Examples:


camelize("/api/users/v1") # => "Api::Users::V1"
# Then: Api::Users::V1.new.call

Parameters:

  • token (String)

    the route path to be camelized

Returns:

  • (String)

    the camelized constant name

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/simple_command_dispatcher/helpers/camelize.rb', line 20

def camelize(token)
  raise ArgumentError, 'Token is not a String' unless token.instance_of? String

  return if token.empty?

  # For RESTful paths → Ruby constants, use Rails' proven methods
  # They're fast, reliable, and handle edge cases that matter for constants
  result = trim_all(token)
    .gsub(%r{[/\-\.\s:]+}, '/')                    # Normalize separators to /
    .split('/')                                    # Split into path segments
    .reject(&:empty?)                              # Remove empty segments
    .map { |segment| segment.underscore.camelize } # Rails camelization
    .join('::')                                    # Join as Ruby namespace

  result.empty? ? '' : result
end