Module: SimpleCommand::KlassTransform

Included in:
Dispatcher
Defined in:
lib/simple_command_dispatcher/klass_transform.rb

Overview

Handles class and module transformations.

Instance Method Summary collapse

Instance Method Details

#camelize(token) ⇒ String

Transforms a route into a module string

Examples:


camelize("/api/app/auth/v1") # => "Api::App::Auth::V1"
camelize("/api/app_name/auth/v1") # => "Api::AppName::Auth::V1"

Returns:

  • (String)

    the camelized token.

Raises:

  • (ArgumentError)


158
159
160
161
162
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 158

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

  token.titlecase.camelize.sub(/^:*/, '').trim_all unless token.empty?
end

#ensure_options(options) ⇒ Hash

Ensures options are initialized and valid before accessing them.

Parameters:

  • options (Hash)

    the options that determine how processing and transformations will be handled.

Options Hash (options):

  • :camelize (Boolean) — default: false

    determines whether or not both class and module names should be camelized.

  • :titleize (Boolean) — default: false

    determines whether or not both class and module names should be titleized.

  • :class_titleize (Boolean) — default: false

    determines whether or not class names should be titleized.

  • :module_titleize (Boolean) — default: false

    determines whether or not module names should be titleized.

  • :class_camelized (Boolean) — default: false

    determines whether or not class names should be camelized.

  • :module_camelized (Boolean) — default: false

    determines whether or not module names should be camelized.

Returns:

  • (Hash)

    the initialized, validated options.



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 182

def ensure_options(options)
  options = {} unless options.instance_of? Hash
  options = { camelize: false, titleize: false, class_titleize: false, module_titleize: false,
              class_camelize: false, module_camelize: false }.merge(options)

  options[:class_camelize] = options[:module_camelize] = true if options[:camelize]

  options[:class_titleize] = options[:module_titleize] = true if options[:titleize]

  options
end

#to_class_string(klass, options = {}) ⇒ String

Returns the klass as a string after transformations have been applied.

Examples:


to_class_string("MyClass") # => "MyClass"
to_class_string("myClass", { class_titleize: true }) # => "MyClass"
to_class_string(:MyClass) # => "MyClass"
to_class_string(:myClass, { class_titleize: true }) # => "MyClass"

Parameters:

  • klass (Symbol or String)

    the class name to be transformed.

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

    the options that determine how klass will be transformed.

Options Hash (options):

  • :class_titleize (Boolean) — default: false

    Determines whether or not klass should be titleized.

Returns:

  • (String)

    the transformed class as a string.



142
143
144
145
146
147
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 142

def to_class_string(klass, options = {})
  klass = validate_klass(klass, options)
  klass = klass.titleize if options[:class_titleize]
  klass = camelize(klass) if options[:class_camelize]
  klass
end

#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.



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

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

#to_constantized_class_string(klass, klass_modules = [], options = {}) ⇒ String

Returns a fully-qualified constantized class (as a string), given the klass and klass_modules.

Examples:


to_constantized_class_string("Authenticate", "Api") # => "Api::Authenticate"
to_constantized_class_string(:Authenticate, [:Api, :AppName, :V1]) # => "Api::AppName::V1::Authenticate"
to_constantized_class_string(:Authenticate, { :api :Api, app_name: :AppName, api_version: :V2 })
   # => "Api::AppName::V2::Authenticate"
to_constantized_class_string("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):

  • :class_titleize (Boolean) — default: false

    Determines whether or not klass should be titleized.

  • :module_titleize (Boolean) — default: false

    Determines whether or not klass_modules should be titleized.

Returns:

  • (String)

    the fully qualified class, which includes module(s) and class name.



72
73
74
75
76
77
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 72

def to_constantized_class_string(klass, klass_modules = [], options = {})
  options = ensure_options(options)
  klass_modules = to_modules_string(klass_modules, options)
  klass_string = to_class_string(klass, options)
  "#{klass_modules}#{klass_string}"
end

#to_modules_string(klass_modules = [], options = {}) ⇒ String

Returns a string of modules that can be subsequently prepended to a class, to create a constantized class.

Examples:


to_modules_string("Api") # => "Api::"
to_modules_string([:Api, :AppName, :V1]) # => "Api::AppName::V1::"
to_modules_string({ :api :Api, app_name: :AppName, api_version: :V1 }) # => "Api::AppName::V1::"
to_modules_string({ :api :api, app_name: :app_name, api_version: :v1 }, { module_titleize: true })
   # => "Api::AppName::V1::"

Parameters:

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

    the modules a class belongs to.

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

    the options that determine how klass_modules is transformed.

Options Hash (options):

  • :module_titleize (Boolean) — default: false

    Determines whether or not klass_modules should be titleized.

Returns:

  • (String)

    a string of modules that can be subsequently prepended to a class, to create a constantized class.

Raises:

  • (ArgumentError)

    if the klass_modules is not of type String, Hash or Array.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 98

def to_modules_string(klass_modules = [], options = {}) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
  klass_modules = validate_klass_modules(klass_modules)

  options = ensure_options(options)

  klass_modules_string = ''
  unless klass_modules.empty?
    case klass_modules
    when String
      klass_modules_string = klass_modules
    when Array
      klass_modules_string = klass_modules.join('::').to_s
    when Hash
      klass_modules_string = ''
      klass_modules.to_a.each_with_index.map do |value, index|
        klass_modules_string = index.zero? ? value[1].to_s : "#{klass_modules_string}::#{value[1]}"
      end
    else
      raise ArgumentError, 'Class modules is not a String, Hash or Array.'
    end
    klass_modules_string = klass_modules_string.split('::').map(&:titleize).join('::') if options[:module_titleize]
    klass_modules_string = camelize(klass_modules_string) if options[:module_camelize]
    klass_modules_string = klass_modules_string.trim_all
    klass_modules_string = "#{klass_modules_string}::" unless klass_modules_string.empty?
  end

  klass_modules_string
end

#validate_klass(klass, _options) ⇒ String

Validates klass and returns klass as a string after all blanks have been removed using klass.gsub(/s+/, “”).

Examples:


validate_klass(" My Class ") # => "MyClass"
validate_klass(:MyClass) # => "MyClass"

Parameters:

  • klass (Symbol or String)

    the class name to be validated. klass cannot be empty?

Returns:

  • (String)

    the validated class as a string with blanks removed.

Raises:

  • (ArgumentError)

    if the klass is empty? or not of type String or Symbol.



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 209

def validate_klass(klass, _options)
  unless klass.is_a?(Symbol) || klass.is_a?(String)
    raise ArgumentError,
      'Class is not a String or Symbol. Class must equal the class name of the ' \
      'SimpleCommand or Command to call in the form of a String or Symbol.'
  end

  klass = klass.to_s.strip

  raise ArgumentError, 'Class is empty?' if klass.empty?

  klass
end

#validate_klass_modules(klass_modules) ⇒ Symbol, Array or String

Validates and returns klass_modules.

Examples:


validate_modules(" Module ") # => " Module "
validate_modules(:Module) # => "Module"
validate_module("ModuleA::ModuleB") # => "ModuleA::ModuleB"

Parameters:

  • klass_modules (Symbol, Array or String)

    the module(s) to be validated.

Returns:

  • (Symbol, Array or String)

    the validated module(s).

Raises:

  • (ArgumentError)

    if the klass_modules is not of type String, Hash or Array.



239
240
241
242
243
244
245
246
247
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 239

def validate_klass_modules(klass_modules)
  return {} if klass_modules.nil? || (klass_modules.respond_to?(:empty?) && klass_modules.empty?)

  if !klass_modules.instance_of?(String) && !klass_modules.instance_of?(Hash) && !klass_modules.instance_of?(Array)
    raise ArgumentError, 'Class modules is not a String, Hash or Array.'
  end

  klass_modules
end