Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/clir/String.ext.rb

Overview

String extension for CLIR

Constant Summary collapse

CHIFFRE_HAUT =
{
  0 => '',
  1 => '¹',
  2 => '²',
  3 => '³',
  4 => '',
  5 => '',
  6 => '',
  7 => '',
  8 => '',
  9 => ''
}
CHIFFRE_BAS =
{
  0 => '',
  1 => '',
  2 => '',
  3 => '',
  4 => '',
  5 => '',
  6 => '',
  7 => '',
  8 => '',
  9 => ''
}
DATA_NORMALIZE =
{
  :from => "ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž",
  :to   => "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz"
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.columnize(lines, delimitor = ',', gutter = ' ') ⇒ Object



37
38
39
40
41
42
43
44
45
46
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
# File 'lib/clir/String.ext.rb', line 37

def self.columnize(lines, delimitor = ',', gutter = '    ')
  # lines = lines.join("\n") if lines.is_a?(Array)
  lines = lines.split("\n") if lines.is_a?(String)
  # 
  # Nombre de colonnes
  # 
  nombre_colonnes = 0
  colonnes_widths = []
  lines = lines.map do |line|
    line.strip.split(delimitor).map {|e| e.strip}
  end.each do |line|
    nb = line.count # nombre de colonnes
    nombre_colonnes = nb if nb > nombre_colonnes
  end
  # 
  # On met le même nombre de colonnes à toutes les lignes
  # 
  lines.map do |line|
    while line.count < nombre_colonnes
      line << ''
    end
    line
  end.each do |line|
    line.each_with_index do |str, col_idx|
      colonnes_widths[col_idx] = 0 if colonnes_widths[col_idx].nil?
      colonnes_widths[col_idx] = str.length if str.length > colonnes_widths[col_idx]
    end
  end.each do |line|
    # 
    # Mettre toutes les colonnes à la même taille
    # 
    line.each_with_index do |str, col_idx|
      line[col_idx] = str.ljust(colonnes_widths[col_idx])
    end
  end

  lines.map do |line|
    line.join(gutter)
  end.join("\n").strip
  
end

Instance Method Details

#as_title(sous = '=', indent = 2) ⇒ Object

Si le texte est :

Mon titre

… cette méthode retourne :

Mon titre
---------


173
174
175
176
177
178
# File 'lib/clir/String.ext.rb', line 173

def as_title(sous = '=', indent = 2)
  len = self.length
  ind = ' ' * indent
  del = ind + sous * (len + 2)
  "\n#{del}\n#{ind} #{self.upcase}\n#{del}"
end

#blancObject Also known as: white



192
193
194
# File 'lib/clir/String.ext.rb', line 192

def blanc
  "\033[0;38m#{self}\033[0m"
end

#blanc_Object



196
197
198
# File 'lib/clir/String.ext.rb', line 196

def blanc_
  "\033[0;38m#{self}"
end

#blanc_clairObject



200
201
202
# File 'lib/clir/String.ext.rb', line 200

def blanc_clair
  "\033[0;37m#{self}\033[0m"
end

#blanc_clair_Object



203
204
205
# File 'lib/clir/String.ext.rb', line 203

def blanc_clair_
  "\033[0;37m#{self}"
end

#bleuObject Also known as: blue



207
208
209
# File 'lib/clir/String.ext.rb', line 207

def bleu
  "\033[0;96m#{self}\033[0m"
end

#bleu_Object



212
213
214
# File 'lib/clir/String.ext.rb', line 212

def bleu_
  "\033[0;96m#{self}"
end

#bleu_clairObject



216
217
218
# File 'lib/clir/String.ext.rb', line 216

def bleu_clair
  "\033[0;36m#{self}\033[0m"
end

#bleu_clair_Object



219
220
221
# File 'lib/clir/String.ext.rb', line 219

def bleu_clair_
  "\033[0;36m#{self}"
end

#camelizeObject

— Transform Methods —



298
299
300
301
302
303
304
# File 'lib/clir/String.ext.rb', line 298

def camelize
  str = "#{self}"
  str[0] = str[0].upcase
  str.split(' ').map do |seg|
    seg.gsub(/(?:_+([a-z]))/i){$1.upcase}
  end.join(' ')
end

#cjust(length, fill_with = ' ') ⇒ Object

As ljust (which align to the left) ans rjust (which align to the right), cjust align to the center depending length

Examples:

"good".cjust(10) # => "   good   "
"good".cjust(10, '+') # => "+++good+++"


150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/clir/String.ext.rb', line 150

def cjust(length, fill_with = ' ')
  if self.length == length
    return self
  elsif self.length > length 
    return self[0...length]
  else
    nombre_moitie = (length - self.length) / 2
    ret = (fill_with * nombre_moitie) + self + (fill_with * nombre_moitie)
    ret = ret + fill_with if ret.length < length
    return ret
  end
end

#decamelizeObject



306
307
308
309
310
311
312
# File 'lib/clir/String.ext.rb', line 306

def decamelize
  str = self
  str[0] = str[0].downcase
  str.split(' ').map do |seg|
    seg.gsub(/([A-Z])/){ "_#{$1.downcase}"}
  end.join(' ')
end

#fond_bleuObject



224
225
226
# File 'lib/clir/String.ext.rb', line 224

def fond_bleu
  "\033[0;44m#{self}\033[0m"
end

#fond_bleu_clairObject



228
229
230
# File 'lib/clir/String.ext.rb', line 228

def fond_bleu_clair
  "\033[0;46m#{self}\033[0m"
end

#fond_vertObject



248
249
250
# File 'lib/clir/String.ext.rb', line 248

def fond_vert
  "\033[0;42m#{self}\033[0m"
end

#grisObject Also known as: grey



260
261
262
# File 'lib/clir/String.ext.rb', line 260

def gris
  "\033[0;90m#{self}\033[0m"
end

#gris_Object



264
265
266
# File 'lib/clir/String.ext.rb', line 264

def gris_
  "\033[0;90m#{self}"
end

#in?(ary) ⇒ Boolean

self. If it’s an Hash, has key self.

Returns:

  • (Boolean)

    TRUE if ary, as a String or an Array, includes



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/clir/String.ext.rb', line 88

def in?(ary)
  case ary
  when Array
    ary.include?(self)
  when String
    ary.match?(self)
  when Hash
    ary.key?(self)
  else
    raise "in? waits for a String, an Hash or a Array. Given: #{ary.class}."
  end
end

#italicObject



187
188
189
# File 'lib/clir/String.ext.rb', line 187

def italic
  "\033[3m#{self}\033[0m"
end

#jauneObject Also known as: yellow



275
276
277
# File 'lib/clir/String.ext.rb', line 275

def jaune
  "\033[0;93m#{self}\033[0m"
end

#jaune_Object



279
280
281
# File 'lib/clir/String.ext.rb', line 279

def jaune_
  "\033[0;93m#{self}"
end

#jaune_darkObject



283
284
285
# File 'lib/clir/String.ext.rb', line 283

def jaune_dark
  "\033[0;33m#{self}\033[0m"
end

#mauveObject Also known as: purple



287
288
289
# File 'lib/clir/String.ext.rb', line 287

def mauve
  "\033[1;94m#{self}\033[0m"
end

#mauve_Object



291
292
293
# File 'lib/clir/String.ext.rb', line 291

def mauve_
  "\033[1;94m#{self}"
end

#max(len) ⇒ Object

Note:

Up to 10, cut at the end with ‘…’ separator Up to 15, cut at the middle and if diff < 5, the separator is ‘…’

Returns self with len length. Cut it if necessary.

Examples:

"Sentence".max(5) # => "Sent…"
"Long sentence".max(10) # => "Long…tence"
"Very long and long sentence".max(16)
# => "Very lo[…]ntence"

Parameters:

  • len (Integer)

    Lenght required (> 1)

Returns:

  • self with len length. Cut it if necessary.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/clir/String.ext.rb', line 117

def max(len)
  len.is_a?(Integer) || raise(ArgumentError.new("Argument should be a Integer"))
  len > 1 || raise(ArgumentError.new("Minimum length should be 2. You give #{len}."))
  return "#{self}"            if self.length <= len
  return self[0...len-1]+''  if len <= 10
  cur_len = self.length
  diff    = cur_len - len

  sep, moitie = 
    if len > 15 && diff > 4
      ['[…]', len / 2 - 2]
    else
      ['', len / 2]
    end

  midav = self[0..moitie-1] + sep
  reste = len - midav.length
  midap = self[-reste..-1]

  return midav + midap
end

#max!(len) ⇒ Object



139
140
141
142
# File 'lib/clir/String.ext.rb', line 139

def max!(len)
  self.replace(self.max(len))
  return true
end

#normalizeObject Also known as: normalized



331
332
333
334
335
336
# File 'lib/clir/String.ext.rb', line 331

def normalize
  self
    .force_encoding('utf-8')
    .gsub(/[œŒæÆ]/,{'œ'=>'oe', 'Œ' => 'Oe', 'æ'=> 'ae', 'Æ' => 'Ae'})
    .tr(DATA_NORMALIZE[:from], DATA_NORMALIZE[:to])
end

#numeric?Boolean

Returns TRUE is str is a number (integer or float) in a string.

Returns:

  • (Boolean)

    TRUE is str is a number (integer or float) in a string.



82
83
84
# File 'lib/clir/String.ext.rb', line 82

def numeric?
  self.match?(/^[0-9.]+$/)
end

#orangeObject



268
269
270
# File 'lib/clir/String.ext.rb', line 268

def orange
  "\033[38;5;214m#{self}\033[0m"
end

#orange_Object



271
272
273
# File 'lib/clir/String.ext.rb', line 271

def orange_
  "\033[38;5;214m#{self}"
end

#patronizeObject



319
320
321
322
323
324
325
326
327
328
329
# File 'lib/clir/String.ext.rb', line 319

def patronize
  str = self
  str.split(/( |\-)/).map do |n|
    n = n.downcase
    if n == 'de' 
      'de'
    else
      n.capitalize 
    end
  end.join('')
end

#rougeObject Also known as: red



252
253
254
# File 'lib/clir/String.ext.rb', line 252

def rouge
  "\033[0;91m#{self}\033[0m"
end

#rouge_Object



256
257
258
# File 'lib/clir/String.ext.rb', line 256

def rouge_
  "\033[0;91m#{self}"
end

#strikeObject



181
182
183
# File 'lib/clir/String.ext.rb', line 181

def strike
  "\033[9m#{self}\033[0m"
end

#titleizeObject



314
315
316
317
# File 'lib/clir/String.ext.rb', line 314

def titleize
  str = self
  str.split(' ').map { |n| n[0].upcase + n[1..-1].downcase }.join(' ')
end

#underlineObject



184
185
186
# File 'lib/clir/String.ext.rb', line 184

def underline
  "\033[4m#{self}\033[0m"
end

#vertObject Also known as: green



232
233
234
# File 'lib/clir/String.ext.rb', line 232

def vert
  "\033[0;92m#{self}\033[0m"
end

#vert_Object



236
237
238
# File 'lib/clir/String.ext.rb', line 236

def vert_
  "\033[0;92m#{self}"
end

#vert_clairObject Also known as: ligth_green



240
241
242
# File 'lib/clir/String.ext.rb', line 240

def vert_clair
  "\033[0;32m#{self}\033[0m"
end

#vert_clair_Object



244
245
246
# File 'lib/clir/String.ext.rb', line 244

def vert_clair_
  "\033[0;32m#{self}"
end