Method: SimpleCommand::KlassTransform#to_constantized_class

Defined in:
lib/simple_command_dispatcher/klass_transform.rb

#to_constantized_class(klass, klass_modules = [], options = {}) ⇒ Class

Returns a constantized class (as a Class constant), given the klass and klass_modules.

Examples:


to_constantized_class("Authenticate", "Api") # => Api::Authenticate
to_constantized_class(:Authenticate, [:Api, :AppName, :V1]) # => Api::AppName::V1::Authenticate
to_constantized_class(:Authenticate, { :api :Api, app_name: :AppName, api_version: :V2 })
  # => Api::AppName::V2::Authenticate
to_constantized_class("authenticate", { :api :api, app_name: :app_name, api_version: :v1 },
  { class_titleize: true, module_titleize: true }) # => Api::AppName::V1::Authenticate

Parameters:

  • klass (Symbol or String)

    the class name.

  • klass_modules (Hash, Array or String) (defaults to: [])

    the modules klass belongs to.

  • options (Hash) (defaults to: {})

    the options that determine how klass_modules is transformed.

Options Hash (options):

  • :camelize (Boolean) — default: false

    determines whether or not both klass and klass_modules should be camelized.

  • :titleize (Boolean) — default: false

    determines whether or not both klass and klass_modules should be titleized.

  • :class_titleize (Boolean) — default: false

    determines whether or not klass names should be titleized.

  • :class_camelized (Boolean) — default: false

    determines whether or not klass names should be camelized.

  • :module_titleize (Boolean) — default: false

    determines whether or not klass_modules names should be titleized.

  • :module_camelized (Boolean) — default: false

    determines whether or not klass_modules names should be camelized.

Returns:

  • (Class)

    the class constant. Can be used to call ClassConstant.constantize.

Raises:

  • (NameError)

    if the constantized class string cannot be constantized; that is, if it is not a valid class constant.



40
41
42
43
44
45
46
47
48
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 40

def to_constantized_class(klass, klass_modules = [], options = {})
  constantized_class_string = to_constantized_class_string(klass, klass_modules, options)

  begin
    constantized_class_string.constantize
  rescue StandardError
    raise NameError, "\"#{constantized_class_string}\" is not a valid class constant."
  end
end