class TTY::Prompt::Question::Modifier

A class representing String modifications.

Attributes

modifiers[R]

Public Class Methods

letter_case(mod, value) click to toggle source

Changes letter casing in a string according to valid modifications. For invalid modification option the string is preserved.

@param [Symbol] mod

the modification to change the string

@option mod [Symbol] :up change to upper case @option mod [Symbol] :upcase change to upper case @option mod [Symbol] :uppercase change to upper case @option mod [Symbol] :down change to lower case @option mod [Symbol] :downcase change to lower case @option mod [Symbol] :capitalize change all words to start

with uppercase case letter

@return [String]

@api public

# File lib/tty/prompt/question/modifier.rb, line 50
def self.letter_case(mod, value)
  return value unless value.is_a?(String)

  case mod
  when :up, :upcase, :uppercase
    value.upcase
  when :down, :downcase, :lowercase
    value.downcase
  when :capitalize
    value.capitalize
  else
    value
  end
end
new(modifiers) click to toggle source

Initialize a Modifier

@api public

# File lib/tty/prompt/question/modifier.rb, line 13
def initialize(modifiers)
  @modifiers = modifiers
end
whitespace(mod, value) click to toggle source

Changes whitespace in a string according to valid modifications.

@param [Symbol] mod

the modification to change the string

@option mod [String] :trim, :strip

remove whitespace for the start and end

@option mod [String] :chomp remove record separator from the end @option mod [String] :collapse remove any duplicate whitespace @option mod [String] :remove remove all whitespace

@api public

# File lib/tty/prompt/question/modifier.rb, line 77
def self.whitespace(mod, value)
  return value unless value.is_a?(String)

  case mod
  when :trim, :strip
    value.strip
  when :chomp
    value.chomp
  when :collapse
    value.gsub(/\s+/, " ")
  when :remove
    value.gsub(/\s+/, "")
  else
    value
  end
end

Public Instance Methods

apply_to(value) click to toggle source

Change supplied value according to the given string transformation. Valid settings are:

@param [String] value

the string to be modified

@return [String]

@api private

# File lib/tty/prompt/question/modifier.rb, line 26
def apply_to(value)
  modifiers.reduce(value) do |result, mod|
    result = Modifier.letter_case(mod, result)
    Modifier.whitespace(mod, result)
  end
end