Class: TTY2::Prompt::Question::Modifier
- Inherits:
-
Object
- Object
- TTY2::Prompt::Question::Modifier
- Defined in:
- lib/tty2/prompt/question/modifier.rb
Overview
A class representing String modifications.
Instance Attribute Summary collapse
- #modifiers ⇒ Object readonly
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
13 14 15 |
# File 'lib/tty2/prompt/question/modifier.rb', line 13 def initialize(modifiers) @modifiers = modifiers end |
Instance Attribute Details
#modifiers ⇒ Object (readonly)
8 9 10 |
# File 'lib/tty2/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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tty2/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.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/tty2/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 |
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:
26 27 28 29 30 31 |
# File 'lib/tty2/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 |