Module: CommandKit::Inflector

Defined in:
lib/command_kit/inflector.rb

Overview

Note:

If you need something more powerful, checkout dry-inflector

A very simple inflector.

Class Method Summary collapse

Class Method Details

.camelize(name) ⇒ String

Converts an under_scored name to a CamelCased name.

Parameters:

  • name (String)

    The under_scored name.

Returns:

  • (String)

    The CamelCased name.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/command_kit/inflector.rb', line 70

def self.camelize(name)
  name = name.to_s.dup

  # sourced from: https://github.com/dry-rb/dry-inflector/blob/c918f967ff82611da374eb0847a77b7e012d3fa8/lib/dry/inflector.rb#L329-L334
  name.sub!(/^[a-z\d]*/,&:capitalize)
  name.gsub!(%r{(?:[_-]|(/))([a-z\d]*)}i) do |match|
    slash = Regexp.last_match(1)
    word  = Regexp.last_match(2)

    "#{slash}#{word.capitalize}"
  end

  name.gsub!('/','::')
  name
end

.dasherize(name) ⇒ String

Replaces all underscores with dashes.

Parameters:

  • name (#to_s)

    The under_scored name.

Returns:

  • (String)

    The dasherized name.



57
58
59
# File 'lib/command_kit/inflector.rb', line 57

def self.dasherize(name)
  name.to_s.tr('_','-')
end

.demodularize(name) ⇒ String

Removes the namespace from a constant name.

Parameters:

  • name (#to_s)

    The constant name.

Returns:

  • (String)

    The class or module's name, without the namespace.



23
24
25
# File 'lib/command_kit/inflector.rb', line 23

def self.demodularize(name)
  name.to_s.split('::').last
end

.underscore(name) ⇒ String

Converts a CamelCased name to an under_scored name.

Parameters:

  • name (#to_s)

    The CamelCased name.

Returns:

  • (String)

    The resulting under_scored name.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/command_kit/inflector.rb', line 36

def self.underscore(name)
  # sourced from: https://github.com/dry-rb/dry-inflector/blob/c918f967ff82611da374eb0847a77b7e012d3fa8/lib/dry/inflector.rb#L286-L287
  name = name.to_s.dup

  name.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  name.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  name.tr!('-','_')
  name.downcase!

  name
end