Module: TTY::Option::Inflection

Included in:
DSL, ErrorAggregator
Defined in:
lib/tty/option/inflection.rb

Class Method Summary collapse

Class Method Details

.dasherize(name) ⇒ Object

Convert class name to dashed case

Examples:

dasherize("ErrorAggregator")
# => "error-aggregator"


44
45
46
# File 'lib/tty/option/inflection.rb', line 44

def dasherize(name)
  underscore(name).tr("_", "-")
end

.demodulize(name) ⇒ String

Remove all modules/class names

Examples:

demodulize("TTY::Option::ErrorAggregator")
# => "ErrorAggregator"

Returns:

  • (String)


15
16
17
# File 'lib/tty/option/inflection.rb', line 15

def demodulize(name)
  name.to_s.split("::").last
end

.underscore(name) ⇒ String

Convert class name to underscore

Examples:

underscore("ErrorAggregator")
# => "error_aggregator"

Returns:

  • (String)


29
30
31
32
33
34
# File 'lib/tty/option/inflection.rb', line 29

def underscore(name)
  name.to_s
      .gsub(/([A-Z\d]+)([A-Z][a-z])/, "\\1_\\2")
      .gsub(/([a-z\d]+)([A-Z])/, "\\1_\\2")
      .downcase
end