Module: NameCase

Defined in:
lib/namecase.rb

Constant Summary collapse

VERSION =
'2.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.nc(str, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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/namecase.rb', line 18

def self.nc str, options = {}
  options = { :lazy => true, :irish => true }.merge options

  # Skip if string is mixed case
  if options[:lazy]
    first_letter_lower = str[0] == str.downcase[0]
    all_lower_or_upper = (str.downcase == str || str.upcase == str)

    return str unless first_letter_lower || all_lower_or_upper
  end

  localstring = str.downcase
  localstring.gsub!(/\b\w/) { |first| first.upcase }
  localstring.gsub!(/\'\w\b/) { |c| c.downcase } # Lowercase 's

  if options[:irish]
    if localstring =~ /\bMac[A-Za-z]{2,}[^aciozj]\b/ or localstring =~ /\bMc/
      match = localstring.match(/\b(Ma?c)([A-Za-z]+)/)
      localstring.gsub!(/\bMa?c[A-Za-z]+/) { match[1] + match[2].capitalize }

      # Now fix "Mac" exceptions
      localstring.gsub!(/\bMacEdo/, 'Macedo')
      localstring.gsub!(/\bMacEvicius/, 'Macevicius')
      localstring.gsub!(/\bMacHado/, 'Machado')
      localstring.gsub!(/\bMacHar/, 'Machar')
      localstring.gsub!(/\bMacHin/, 'Machin')
      localstring.gsub!(/\bMacHlin/, 'Machlin')
      localstring.gsub!(/\bMacIas/, 'Macias')
      localstring.gsub!(/\bMacIulis/, 'Maciulis')
      localstring.gsub!(/\bMacKie/, 'Mackie')
      localstring.gsub!(/\bMacKle/, 'Mackle')
      localstring.gsub!(/\bMacKlin/, 'Macklin')
      localstring.gsub!(/\bMacKmin/, 'Mackmin')
      localstring.gsub!(/\bMacQuarie/, 'Macquarie')
    end
    localstring.gsub!('Macmurdo','MacMurdo')
  end

  # Fixes for "son (daughter) of" etc
  localstring.gsub!(/\bAl(?=\s+\w)/, 'al')  # al Arabic or forename Al.
  localstring.gsub!(/\bAp\b/, 'ap')         # ap Welsh.
  localstring.gsub!(/\bBen(?=\s+\w)/,'ben') # ben Hebrew or forename Ben.
  localstring.gsub!(/\bDell([ae])\b/,'dell\1')  # della and delle Italian.
  localstring.gsub!(/\bD([aeiou])\b/,'d\1')   # da, de, di Italian; du French; do Brasil
  localstring.gsub!(/\bD([ao]s)\b/,'d\1')   # das, dos Brasileiros
  localstring.gsub!(/\bDe([lr])\b/,'de\1')   # del Italian; der Dutch/Flemish.
  localstring.gsub!(/\bEl\b/,'el')   # el Greek or El Spanish.
  localstring.gsub!(/\bLa\b/,'la')   # la French or La Spanish.
  localstring.gsub!(/\bL([eo])\b/,'l\1')      # lo Italian; le French.
  localstring.gsub!(/\bVan(?=\s+\w)/,'van')  # van German or forename Van.
  localstring.gsub!(/\bVon\b/,'von')  # von Dutch/Flemish

  # Fix roman numeral names
  localstring.gsub!(
    / \b ( (?: [Xx]{1,3} | [Xx][Ll]   | [Ll][Xx]{0,3} )?
           (?: [Ii]{1,3} | [Ii][VvXx] | [Vv][Ii]{0,3} )? ) \b /x
  ) { |m| m.upcase }

  localstring
end

.nc!(str, options = {}) ⇒ Object



14
15
16
# File 'lib/namecase.rb', line 14

def self.nc! str, options = {}
  str.replace NameCase.nc(str, options)
end

Instance Method Details

#nc(options = {}) ⇒ Object

Returns a new String with the contents properly namecased



5
6
7
# File 'lib/namecase.rb', line 5

def nc(options = {})
  NameCase.nc self, options
end

#nc!(options = {}) ⇒ Object

Modifies str in place and properly namecases the string.



10
11
12
# File 'lib/namecase.rb', line 10

def nc!(options = {})
  NameCase.nc! self, options
end