Class: TTY::Prompt::Question::Modifier

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/prompt/question/modifier.rb

Overview

A class representing String modifications.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modifiers) ⇒ Modifier

Initialize a Modifier


13
14
15
# File 'lib/tty/prompt/question/modifier.rb', line 13

def initialize(modifiers)
  @modifiers = modifiers
end

Instance Attribute Details

#modifiersObject (readonly)


8
9
10
# File 'lib/tty/prompt/question/modifier.rb', line 8

def modifiers
  @modifiers
end

Class Method Details

.letter_case(mod, value) ⇒ String

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

Parameters:

  • mod (Symbol)

    the modification to change the string

Options Hash (mod):

  • :up (Symbol)

    change to upper case

  • :upcase (Symbol)

    change to upper case

  • :uppercase (Symbol)

    change to upper case

  • :down (Symbol)

    change to lower case

  • :downcase (Symbol)

    change to lower case

  • :capitalize (Symbol)

    change all words to start with uppercase case letter

Returns:

  • (String)

50
51
52
53
54
55
56
57
58
59
60
61
62
# 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

.whitespace(mod, value) ⇒ Object

Changes whitespace in a string according to valid modifications.

Parameters:

  • mod (Symbol)

    the modification to change the string

Options Hash (mod):

  • :trim, (String)

    :strip remove whitespace for the start and end

  • :chomp (String)

    remove record separator from the end

  • :collapse (String)

    remove any duplicate whitespace

  • :remove (String)

    remove all whitespace


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tty/prompt/question/modifier.rb', line 76

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

Instance Method Details

#apply_to(value) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • value (String)

    the string to be modified

Returns:

  • (String)

26
27
28
29
30
31
# 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