Module: CoreExtensions::String

Defined in:
lib/core_extensions/string.rb

Overview

Extension of Ruby’s standard library String class.

Instance Method Summary collapse

Instance Method Details

#humanizeString

Turns underscores into spaces and titleize a string.

Returns:



46
47
48
# File 'lib/core_extensions/string.rb', line 46

def humanize
  tr('_', ' ').titleize
end

#unaccentizeString

Replaces all accented characters in a string with their non-accented version.

Returns:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/core_extensions/string.rb', line 10

def unaccentize # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  tr('ÀÁÂÃÄÅàáâãäåĀāĂ㥹',       'AAAAAAaaaaaaAaAaAa')
    .tr('ÇçĆćĈĉĊċČčÐðĎďĐđ',      'CcCcCcCcCcDdDdDd')
    .tr('ÈÉÊËèéêëĒēĔĕĖėĘęĚě',    'EEEEeeeeEeEeEeEeEe')
    .tr('ĜĝĞğĠġĢģĤĥĦħ',          'GgGgGgGgHhHh')
    .tr('ÌÍÎÏìíîïĨĩĪīĬĭĮįİı',    'IIIIiiiiIiIiIiIiIi')
    .tr('ĴĵĶķĸĹĺĻļĽľĿŀŁł',       'JjKkkLlLlLlLlLl')
    .tr('ÑñŃńŅņŇňʼnŊŋ',           'NnNnNnNnnNn')
    .tr('ÒÓÔÕÖØòóôõöøŌōŎŏŐő',    'OOOOOOooooooOoOoOo')
    .tr('ŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧ', 'RrRrRrSsSsSsSssTtTtTt')
    .tr('ÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲų',  'UUUUuuuuUuUuUuUuUuUu')
    .tr('ŴŵÝýÿŶŷŸŹźŻżŽž',        'WwYyyYyYZzZzZz')
    .gsub(/ß/, 'ss')
    .gsub(/Æ/, 'AE')
    .gsub(/æ/, 'ae')
    .gsub(/Œ/, 'OE')
    .gsub(/œ/, 'oe')
    .gsub(/IJ/, 'IJ')
    .gsub(/ij/, 'ij')
end

#urlizeString

Converts a string to a snake-cased format suitable for URL and/or CSS attributes.

Returns:



33
34
35
# File 'lib/core_extensions/string.rb', line 33

def urlize
  strip.unaccentize.downcase.tr(' \'', '_').delete('^-_a-z0-9')
end

#urlized?Boolean

Tests whether a string is urlize-d, ie. it is only constituted of characters suitable for URL and/or CSS attributes.

Returns:

  • (Boolean)


40
41
42
# File 'lib/core_extensions/string.rb', line 40

def urlized?
  scan(/[^-_a-z0-9]/).empty?
end