Module: Doing::StringTransform

Included in:
String
Defined in:
lib/doing/string/transform.rb

Overview

String helpers

Instance Method Summary collapse

Instance Method Details

#cap_firstObject

Capitalize on the first character on string

Returns:

  • Capitalized string



116
117
118
# File 'lib/doing/string/transform.rb', line 116

def cap_first
  sub(/^\w/, &:upcase)
end

#compressObject

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.

Parameters:

  • kind (String) (defaults to: nil)

    specify string, array, integer, float, symbol, or boolean (falls back to string if value is not recognized)

Returns:

  • Converted object type



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
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/doing/string/transform.rb', line 142

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

#titlecaseObject



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/doing/string/transform.rb', line 180

def titlecase
  tr('_', ' ')
    .gsub(/\s+/, ' ')
    .gsub(/\b\w/) do
    if ::Regexp.last_match.pre_match[-1,
                                     1] == "'"
      ::Regexp.last_match(0)
    else
      ::Regexp.last_match(0).upcase
    end
  end
end

#to_p(number) ⇒ Object

Pluralize a string based on quantity

Parameters:

  • number (Integer)

    the quantity of the object the string represents



126
127
128
# File 'lib/doing/string/transform.rb', line 126

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

Parameters:

  • len (Integer)

    The length

  • offset (Integer) (defaults to: 0)

    (Optional) The width to pad each subsequent line

  • prefix (String) (defaults to: '')

    (Optional) A prefix to add to each line



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/doing/string/transform.rb', line 47

def wrap(len, pad: 0, indent: '  ', offset: 0, prefix: '', color: '', after: '', reset: '', pad_first: false)
  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
  after.sub!(/[ \t]+\z/, '') unless note.empty?

  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 = ''
  if pad_first && pad.positive?
    out.map!.with_index do |l, idx|
      suffix = idx.zero? ? "#{last_color}#{after}" : last_color
      format("%-#{pad}s%s", l, suffix)
    end
  else
    out[0] = format("%-#{pad}s%s%s", out[0], last_color, after)
  end

  suffix = if note.empty?
             pad_first && pad.positive? ? '' : ' '
           else
             " #{note}".chomp
           end

  out.map.with_index do |l, idx|
    if !pad_first && idx.zero?
      "#{color}#{prefix}#{l}#{last_color}"
    else
      "#{left_pad}#{color}#{prefix}#{l}#{last_color}"
    end
  end.join("\n") + suffix
  # res.join("\n").strip + last_color + " #{note}".chomp
end