Module: Clack::Transformers
- Defined in:
- lib/clack/transformers.rb
Overview
Built-in transformers for normalizing user input. Use these with the ‘transform:` option on prompts.
Transforms are applied after validation passes, so you can validate the raw input and transform it into a normalized form.
Constant Summary collapse
- REGISTRY =
Lookup table for symbol shortcuts
{}
Class Method Summary collapse
-
.capitalize ⇒ Proc
Capitalize first letter, lowercase rest.
-
.chain(*transformers) ⇒ Proc
Combine multiple transformers, applied in order.
-
.compact ⇒ Proc
Remove all whitespace.
-
.digits_only ⇒ Proc
Extract only digits.
-
.downcase ⇒ Proc
Convert to lowercase.
-
.resolve(transformer) ⇒ #call?
Resolve a transformer from a symbol, callable, or return as-is.
-
.squish ⇒ Proc
Strip and collapse whitespace to single spaces.
-
.strip ⇒ Proc
Strip leading/trailing whitespace.
-
.titlecase ⇒ Proc
Capitalize first letter of each word.
-
.to_float ⇒ Proc
Parse as float.
-
.to_integer ⇒ Proc
Parse as integer.
-
.trim ⇒ Proc
Alias for strip (for JS developers).
-
.upcase ⇒ Proc
Convert to uppercase.
Class Method Details
.capitalize ⇒ Proc
Capitalize first letter, lowercase rest.
77 78 79 |
# File 'lib/clack/transformers.rb', line 77 def capitalize REGISTRY[:capitalize] end |
.chain(*transformers) ⇒ Proc
Combine multiple transformers, applied in order. Accepts symbols or procs.
126 127 128 129 |
# File 'lib/clack/transformers.rb', line 126 def chain(*transformers) resolved = transformers.map { |xform| resolve(xform) } ->(value) { resolved.reduce(value) { |val, xform| xform.call(val) } } end |
.compact ⇒ Proc
Remove all whitespace.
95 96 97 |
# File 'lib/clack/transformers.rb', line 95 def compact REGISTRY[:compact] end |
.digits_only ⇒ Proc
Extract only digits.
113 114 115 |
# File 'lib/clack/transformers.rb', line 113 def digits_only REGISTRY[:digits_only] end |
.downcase ⇒ Proc
Convert to lowercase.
65 66 67 |
# File 'lib/clack/transformers.rb', line 65 def downcase REGISTRY[:downcase] end |
.resolve(transformer) ⇒ #call?
Resolve a transformer from a symbol, callable, or return as-is.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/clack/transformers.rb', line 37 def resolve(transformer) case transformer when Symbol REGISTRY[transformer] || raise(ArgumentError, "Unknown transformer: #{transformer.inspect}. Available: #{REGISTRY.keys.map(&:inspect).join(", ")}") when nil nil else return transformer if transformer.respond_to?(:call) raise ArgumentError, "Transform must be a Symbol or respond to #call, got #{transformer.class}" end end |
.squish ⇒ Proc
Strip and collapse whitespace to single spaces.
89 90 91 |
# File 'lib/clack/transformers.rb', line 89 def squish REGISTRY[:squish] end |
.strip ⇒ Proc
Strip leading/trailing whitespace.
53 54 55 |
# File 'lib/clack/transformers.rb', line 53 def strip REGISTRY[:strip] end |
.titlecase ⇒ Proc
Capitalize first letter of each word.
83 84 85 |
# File 'lib/clack/transformers.rb', line 83 def titlecase REGISTRY[:titlecase] end |
.to_float ⇒ Proc
Parse as float.
107 108 109 |
# File 'lib/clack/transformers.rb', line 107 def to_float REGISTRY[:to_float] end |
.to_integer ⇒ Proc
Parse as integer.
101 102 103 |
# File 'lib/clack/transformers.rb', line 101 def to_integer REGISTRY[:to_integer] end |
.trim ⇒ Proc
Alias for strip (for JS developers).
59 60 61 |
# File 'lib/clack/transformers.rb', line 59 def trim REGISTRY[:trim] end |
.upcase ⇒ Proc
Convert to uppercase.
71 72 73 |
# File 'lib/clack/transformers.rb', line 71 def upcase REGISTRY[:upcase] end |