Class: TTY::Shell::Question::Modifier
- Inherits:
-
Object
- Object
- TTY::Shell::Question::Modifier
- Defined in:
- lib/tty/shell/question/modifier.rb
Overview
A class representing String modifications.
Class Method Summary collapse
-
.letter_case(mod, value) ⇒ String
Changes letter casing in a string according to valid modifications.
-
.whitespace(mod, value) ⇒ Object
Changes whitespace in a string according to valid modifications.
Instance Method Summary collapse
-
#apply_to(value) ⇒ String
private
Change supplied value according to the given string transformation.
-
#initialize(*modifiers) ⇒ Modifier
constructor
Initialize a Modifier.
Constructor Details
#initialize(*modifiers) ⇒ Modifier
Initialize a Modifier
16 17 18 |
# File 'lib/tty/shell/question/modifier.rb', line 16 def initialize(*modifiers) @modifiers = Array(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.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tty/shell/question/modifier.rb', line 52 def self.letter_case(mod, value) 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.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/tty/shell/question/modifier.rb', line 77 def self.whitespace(mod, value) 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:
29 30 31 32 33 34 |
# File 'lib/tty/shell/question/modifier.rb', line 29 def apply_to(value) modifiers.inject(value) do |result, mod| result = Modifier.letter_case mod, result result = Modifier.whitespace mod, result end end |