Class: CapitalizeNames::Capitalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/capitalize_names/capitalizer.rb

Constant Summary collapse

NAME =
%r{
  (                                                 # start capture
    (?:                                             # start first name token
      (?:(?:van\ )|(?:de\ (?:la\ )?)|(?:dit\ ))?    # optionally match one of van, de, de la, dit and space
      (?:[[:alnum:]]|'|\(|\))+                      # match any unicode character, number, apostrophe or bracket
    )                                               # end first name token
    (?:                                             # start optional additional name tokens
      -                                             # additional name tokens start with -
      (?:                                           # start additional name token
        (?:(?:van\ )|(?:de\ (?:la\ )?)|(?:dit\ ))?  # optionally match one of van, de, de la, dit and space
        (?:[[:alnum:]]|'|\(|\))+                    # match any unicode character, number, apostrophe or bracket
      )                                             # end additional name token
    )*                                              # end optional additional name tokens
  )                                                 # end capture
}ix
MC =
/(?<=\A|-)Mc(\w)(?=\w)/i
MAC =
/(?<=\A|-)Mac(\w)(?=\w)/i
O_APOSTROPHE =
/(?<=\A|-)O'(\w)(?=\w)/i
VAN_SPACE =
/(?<=\A|-)Van /i
DE_LA_SPACE =
/(?<=\A|-)De La /i
DE_SPACE =
/(?<=\A|-)De /i
DIT_SPACE =
/(?<=\A|-)Dit /i
VALID_FORMATS =
[:fullname, :firstname, :givenname, :lastname, :surname]
DEFAULT_OPTIONS =
{
  format: :fullname,
  skip_mc: false,
  skip_mac: false,
  skip_o_apostrophe: false,
  skip_van_space: false,
  skip_de_space: false,
  skip_de_la_space: false,
  skip_dit_space: false,
}
SUFFIX_MAP =
CapitalizeNames::SUFFIXES.each_with_object({}) { |suffix, map| map[suffix.downcase] = suffix }
SURNAME_MAP =
CapitalizeNames::SURNAMES.each_with_object({}) { |surname, map| map[surname.downcase] = surname }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Capitalizer

Returns a new instance of Capitalizer.



47
48
49
50
# File 'lib/capitalize_names/capitalizer.rb', line 47

def initialize(name, options = {})
  @name = name
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#capitalizeObject



57
58
59
60
61
# File 'lib/capitalize_names/capitalizer.rb', line 57

def capitalize
  capitalize!
rescue CapitalizeNames::Errors::GenericError
  name
end

#capitalize!Object



52
53
54
55
# File 'lib/capitalize_names/capitalizer.rb', line 52

def capitalize!
  can_process?
  capitalize_name
end