Module: Doing::StringTransform
- Included in:
- String
- Defined in:
- lib/doing/string/transform.rb
Overview
String helpers
Instance Method Summary collapse
-
#cap_first ⇒ Object
Capitalize on the first character on string.
-
#compress ⇒ Object
Compress multiple spaces to single space.
- #compress! ⇒ Object
-
#set_type(kind = nil) ⇒ Object
Convert a string value to an appropriate type.
- #simple_wrap(width) ⇒ Object
- #titlecase ⇒ Object
-
#to_p(number) ⇒ Object
Pluralize a string based on quantity.
-
#wrap(len, pad: 0, indent: ' ', offset: 0, prefix: '', color: '', after: '', reset: '', pad_first: false) ⇒ Object
Wrap string at word breaks, respecting tags.
Instance Method Details
#cap_first ⇒ Object
Capitalize on the first character on string
102 103 104 105 106 |
# File 'lib/doing/string/transform.rb', line 102 def cap_first sub(/^\w/) do |m| m.upcase end end |
#compress ⇒ Object
Compress multiple spaces to single space
9 10 11 |
# File 'lib/doing/string/transform.rb', line 9 def compress gsub(/ +/, ' ').strip end |
#compress! ⇒ Object
13 14 15 |
# File 'lib/doing/string/transform.rb', line 13 def compress! replace compress end |
#set_type(kind = nil) ⇒ Object
Convert a string value to an appropriate type. If kind is not specified, '[one, two]' becomes an Array, '1' becomes Integer, '1.5' becomes Float, 'true' or 'yes' becomes TrueClass, 'false' or 'no' becomes FalseClass.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/doing/string/transform.rb', line 130 def set_type(kind = nil) if kind case kind.to_s when /^a/i gsub(/^\[ *| *\]$/, '').split(/ *, */) when /^i/i to_i when /^(fa|tr)/i to_bool when /^f/i to_f when /^sy/i sub(/^:/, '').to_sym when /^b/i self =~ /^(true|yes)$/ ? true : false else to_s end else case self when /(^\[.*?\]$| *, *)/ gsub(/^\[ *| *\]$/, '').split(/ *, */) when /^[0-9]+$/ to_i when /^[0-9]+\.[0-9]+$/ to_f when /^:\w+/ sub(/^:/, '').to_sym when /^(true|yes)$/i true when /^(false|no)$/i false else to_s end end end |
#simple_wrap(width) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/doing/string/transform.rb', line 17 def simple_wrap(width) str = gsub(/@\S+\(.*?\)/) { |tag| tag.gsub(/\s/, '%%%%') } words = str.split(/ /).map { |word| word.gsub(/%%%%/, ' ') } out = [] line = [] words.each do |word| if word.uncolor.length >= width chars = word.uncolor.split('') out << chars.slice!(0, width - 1).join('') while chars.count >= width line << chars.join('') next elsif line.join(' ').uncolor.length + word.uncolor.length + 1 > width out.push(line.join(' ')) line.clear end line << word.uncolor end out.push(line.join(' ')) out.join("\n") end |
#titlecase ⇒ Object
168 169 170 171 172 |
# File 'lib/doing/string/transform.rb', line 168 def titlecase tr('_', ' '). gsub(/\s+/, ' '). gsub(/\b\w/){ $`[-1,1] == "'" ? $& : $&.upcase } end |
#to_p(number) ⇒ Object
Pluralize a string based on quantity
114 115 116 |
# File 'lib/doing/string/transform.rb', line 114 def to_p(number) number == 1 ? self : "#{self}s" end |
#wrap(len, pad: 0, indent: ' ', offset: 0, prefix: '', color: '', after: '', reset: '', pad_first: false) ⇒ Object
Wrap string at word breaks, respecting tags
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/doing/string/transform.rb', line 47 def wrap(len, pad: 0, indent: ' ', offset: 0, prefix: '', color: '', after: '', reset: '', pad_first: false) last_color = color.empty? ? '' : after.last_color note_rx = /(?mi)(?<!\\)%(?<width>-?\d+)?(?:\^(?<mchar>.))?(?:(?<ichar>[ _t]|[^a-z0-9])(?<icount>\d+))?(?<prefix>.[ _t]?)?note/ note = '' after = after.dup if after.frozen? after.sub!(note_rx) do note = Regexp.last_match(0) '' end left_pad = ' ' * offset left_pad += indent # return "#{left_pad}#{prefix}#{color}#{self}#{last_color} #{note}" unless len.positive? # Don't break inside of tag values str = gsub(/@\S+\(.*?\)/) { |tag| tag.gsub(/\s/, '%%%%') }.gsub(/\n/, ' ') words = str.split(/ /).map { |word| word.gsub(/%%%%/, ' ') } out = [] line = [] words.each do |word| if word.uncolor.length >= len chars = word.uncolor.split('') out << chars.slice!(0, len - 1).join('') while chars.count >= len line << chars.join('') next elsif line.join(' ').uncolor.length + word.uncolor.length + 1 > len out.push(line.join(' ')) line.clear end line << word.uncolor end out.push(line.join(' ')) last_color = '' out[0] = format("%-#{pad}s%s%s", out[0], last_color, after) out.map.with_index { |l, idx| if !pad_first && idx == 0 "#{color}#{prefix}#{l}#{last_color}" else "#{left_pad}#{color}#{prefix}#{l}#{last_color}" end }.join("\n") + " #{note}".chomp # res.join("\n").strip + last_color + " #{note}".chomp end |