capitalize-names 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73e890e61b19c63e68a8602c12d3142cee3ca99f
4
- data.tar.gz: 97ecd4a272ec1243111f19760f5622e1cee0135d
3
+ metadata.gz: 952173c8584a6003c41afbb6fed489192388cd33
4
+ data.tar.gz: 0d041067c287a94e95b37793a937049cf1968dcd
5
5
  SHA512:
6
- metadata.gz: 363f3b82f8ddab0779b590e16c9b8d4b0e730ca7046b04f04ff2805bb6d45471594e8b8de0db109817bfef1506f021626c0fdf0fb10227455eedc283866ddfad
7
- data.tar.gz: dee6a9caba153004ae269987ccf080ac6a0a536bc54aeb2aa7f35170b845880db85dc817c1503ed77cfbfc15d34646d49de88dcfdbefe177069af21349f8277d
6
+ metadata.gz: 13356663ebb5fc5153cecb45dfe0222061bcefe27a21ec4c93c9e5abad001dac72f235215c2ec1927690a4e0904952675096661df6e0b49a96793b03e6238f4e
7
+ data.tar.gz: 9733023e31a9859ba77b4c4f04e9ff872be83d7cb2c13b4801ecf29c6f705529e7dc4620e10a0a2206075c29b2283794f69ce4a280b8aa78bcf2dbd39d6a1456
@@ -3,7 +3,7 @@ require 'capitalize_names/errors'
3
3
  require 'capitalize_names/capitalizer'
4
4
  require 'capitalize_names/suffixes'
5
5
  require 'capitalize_names/surnames'
6
- require 'active_support/all'
6
+ require 'active_support/core_ext/string/multibyte'
7
7
 
8
8
  module CapitalizeNames
9
9
 
@@ -2,6 +2,10 @@ module CapitalizeNames
2
2
  class Capitalizer
3
3
  attr_accessor :name
4
4
 
5
+ MC = /^Mc(\w)(?=\w)/i
6
+ MAC = /^Mac(\w)(?=\w)/i
7
+ O_APOSTROPHE = /^O'(\w)(?=\w)/i
8
+
5
9
  def initialize(name)
6
10
  @name = name
7
11
  end
@@ -14,7 +18,7 @@ module CapitalizeNames
14
18
  def capitalize
15
19
  begin
16
20
  capitalize!
17
- rescue *CapitalizeNames::Errors.all_exceptions
21
+ rescue CapitalizeNames::Errors::GenericError
18
22
  name
19
23
  end
20
24
  end
@@ -25,59 +29,56 @@ module CapitalizeNames
25
29
  true
26
30
  end
27
31
 
32
+ def surname_suffix_position?(position, name, surname_or_suffix)
33
+ # surname/suffix must be:
34
+ # 1. at start of name or after a space or a -
35
+ # -and-
36
+ # 2. followed by the end of string or a space or a -
37
+ (
38
+ (position == 0) || \
39
+ (
40
+ position > 0 && (name[position-1] == ' ' || name[position-1] == '-')
41
+ )
42
+ ) && (
43
+ (name.length == position+surname_or_suffix.length) || \
44
+ (name[position+surname_or_suffix.length] == ' ') || (name[position+surname_or_suffix.length] == '-')
45
+ )
46
+ end
47
+
28
48
  def _capitalize
29
- nm = name.mb_chars
30
- mc = /^Mc(\w)(?=\w)/i
31
- mac = /^Mac(\w)(?=\w)/i
32
- oap = /^O'(\w)(?=\w)/i
49
+ _name = name
50
+
33
51
  hyphens_index = []
34
- while nm.index('-') do
35
- index = nm.index('-')
52
+
53
+ while _name.index('-') do
54
+ index = _name.index('-')
36
55
  hyphens_index << index
37
- nm = nm[0...index] + ' ' + nm[index+1..-1]
56
+ _name = _name[0...index] << ' ' << _name[index+1..-1]
38
57
  end
39
58
 
40
- nm = nm.split.map{|w| w.capitalize}.map{ |w|
41
- begin
42
- w.gsub(mc, "Mc" + w[2].upcase)
43
- rescue
44
- w
45
- end
46
- }.map{|w|
47
- begin
48
- w.gsub(mac, "Mac" + w[3].upcase)
49
- rescue
50
- w
51
- end
52
- }.map{|w|
53
- begin
54
- w.gsub(oap, "O'" + w[2].upcase)
55
- rescue
56
- w
57
- end
59
+ _name = _name.split.map{ |w|
60
+ w.mb_chars.capitalize.to_str
61
+ }.map{ |w|
62
+ w.gsub(MC) { "Mc#{$1.upcase}" }\
63
+ .gsub(MAC) { "Mac#{$1.upcase}" }\
64
+ .gsub(O_APOSTROPHE) { "O'#{$1.upcase}" }
58
65
  }
59
66
 
60
- nm = nm.join(' ')
67
+ _name = _name.join(' ')
61
68
  hyphens_index.each do |idx|
62
- nm = nm[0...idx] + '-' + (nm[idx+1..-1] || "")
69
+ _name = _name[0...idx] << '-' << (_name[idx+1..-1] || "")
63
70
  end
64
71
 
65
- nm = nm.gsub("Van ", "van ").gsub("De ", "de ").gsub("Dit ", "dit ")
66
- nm += ' '
72
+ _name = _name.gsub("Van ", "van ").gsub("De ", "de ").gsub("Dit ", "dit ")
73
+ _name << ' '
67
74
 
68
- (CapitalizeNames::SURNAMES + CapitalizeNames::SUFFIXES).each do |surname|
69
- pos = nm.downcase.index(surname.downcase)
70
- if pos
71
- # surname/suffix must be:
72
- # 1. at start of name or after a space or a -
73
- # -and-
74
- # 2. followed by the end of string or a space or a -
75
- if ( ((pos == 0) or (pos > 0 and (nm[pos-1] == ' ' or nm[pos-1] == '-'))) and ((nm.length == pos+surname.length) or (nm[pos+surname.length] == ' ') or (nm[pos+surname.length] == '-')) )
76
- nm = nm[0...pos] + surname + nm[pos+surname.length..-1]
77
- end
75
+ (CapitalizeNames::SURNAMES + CapitalizeNames::SUFFIXES).each do |surname_or_suffix|
76
+ position = _name.downcase.index(surname_or_suffix.downcase)
77
+ if position and surname_suffix_position?(position, _name, surname_or_suffix)
78
+ _name = _name[0...position] << surname_or_suffix << _name[position+surname_or_suffix.length..-1]
78
79
  end
79
80
  end
80
- nm.strip
81
+ _name.strip
81
82
  end
82
83
  end
83
84
  end
@@ -1,13 +1,6 @@
1
1
  module CapitalizeNames
2
2
  module Errors
3
- class << self
4
- def all_exceptions
5
- self.constants
6
- .map { |c| self.const_get(c) }
7
- .select{ |c| c < Exception }
8
- end
9
- end
10
-
11
- class InvalidName < Exception; end;
3
+ class GenericError < Exception; end
4
+ class InvalidName < GenericError; end
12
5
  end
13
- end
6
+ end
@@ -0,0 +1,3 @@
1
+ module CapitalizeNames
2
+ VERSION = "1.0.5"
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capitalize-names
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Tate
@@ -49,6 +49,7 @@ files:
49
49
  - lib/capitalize_names/errors.rb
50
50
  - lib/capitalize_names/suffixes.rb
51
51
  - lib/capitalize_names/surnames.rb
52
+ - lib/capitalize_names/version.rb
52
53
  homepage: http://github.com/infiton/capitalize-names
53
54
  licenses:
54
55
  - MIT