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.



147
148
149
150
151
152
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 147

def camelize(token)
   if !token.instance_of? String
      raise ArgumentError.new('Token is not a String')
   end
   token = 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.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 170

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)

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

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

   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.



127
128
129
130
131
132
133
134
135
136
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 127

def to_class_string(klass, options = {})
   klass = validate_klass(klass, options)
   if options[:class_titleize]
      klass = klass.titleize
   end
   if options[:class_camelize]
      klass = camelize(klass)
   end
   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.



32
33
34
35
36
37
38
39
40
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 32

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
       raise NameError.new("\"#{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.



59
60
61
62
63
64
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 59

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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 83

def to_modules_string(klass_modules = [], options = {})
   klass_modules = validate_klass_modules(klass_modules)

   options = ensure_options(options)

   klass_modules_string = ''
   if !klass_modules.empty?
      case klass_modules
         when String
            klass_modules_string = klass_modules
         when Array
            klass_modules_string = "#{klass_modules.join('::')}"
         when Hash
            klass_modules_string = ''
            klass_modules.to_a.each_with_index.map { | value, index | 
               klass_modules_string = index == 0 ? value[1].to_s : "#{klass_modules_string}::#{value[1]}"
            }
         else
            raise ArgumentError.new('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.



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 200

def validate_klass(klass, options)
   if !(klass.is_a?(Symbol) || klass.is_a?(String))
      raise ArgumentError.new('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

   if klass.empty?
      raise ArgumentError.new('Class is empty?')
   end

   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.



230
231
232
233
234
235
236
237
238
# File 'lib/simple_command_dispatcher/klass_transform.rb', line 230

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.new('Class modules is not a String, Hash or Array.')
   end

   klass_modules
end