class CapitalizeNames::Capitalizer
Constants
- DEFAULT_OPTIONS
- DE_LA_SPACE
- DE_SPACE
- DIT_SPACE
- MAC
- MC
- NAME
- O_APOSTROPHE
- SUFFIX_MAP
- SURNAME_MAP
- VALID_FORMATS
- VAN_SPACE
Attributes
name[R]
options[R]
Public Class Methods
new(name, options = {})
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 47 def initialize(name, options = {}) @name = name @options = DEFAULT_OPTIONS.merge(options) end
Public Instance Methods
capitalize()
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 57 def capitalize capitalize! rescue CapitalizeNames::Errors::GenericError name end
capitalize!()
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 52 def capitalize! can_process? capitalize_name end
Private Instance Methods
can_process?()
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 65 def can_process? raise CapitalizeNames::Errors::InvalidName, "Cannot capitalize nil" unless name true end
capitalize_givenname(str)
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 117 def capitalize_givenname(str) capitalize_str(str, false) end
capitalize_lastname(str)
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 129 def capitalize_lastname(str) return capitalize_suffix(str) if suffix?(str) output = capitalize_str(str, true) output = output.gsub(VAN_SPACE, "van ") unless options[:skip_van_space] output = output.gsub(DIT_SPACE, "dit ") unless options[:skip_dit_space] if output.match?(DE_LA_SPACE) output = output.gsub(DE_LA_SPACE, "de la ") unless options[:skip_de_la_space] else output = output.gsub(DE_SPACE, "de ") unless options[:skip_de_space] end output end
capitalize_name()
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 146 def capitalize_name tokens = tokenize_name has_capitalized_last_name = false tokens.reverse.map do |token| token_value = token[:value] next token_value unless token[:is_name] case name_format when :firstname, :givenname capitalize_givenname(token_value) when :lastname, :surname capitalize_lastname(token_value) else if has_capitalized_last_name capitalize_givenname(token_value) else has_capitalized_last_name = !suffix?(token_value) capitalize_lastname(token_value) end end end.reverse.join("") end
capitalize_str(str, surname_rules)
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 101 def capitalize_str(str, surname_rules) str.split(/(\s|-)/).map do |word| next word if word.match?(/(\s|-)/) output = word.capitalize next output unless surname_rules next capitalize_surname(output) if surname?(output) output = output.gsub(MC) { "Mc#{Regexp.last_match(1).upcase}" } unless options[:skip_mc] output = output.gsub(MAC) { "Mac#{Regexp.last_match(1).upcase}" } unless options[:skip_mac] output = output.gsub(O_APOSTROPHE) { "O'#{Regexp.last_match(1).upcase}" } unless options[:skip_o_apostrophe] output end.join("") end
capitalize_suffix(str)
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 121 def capitalize_suffix(str) SUFFIX_MAP[str.downcase] end
capitalize_surname(str)
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 125 def capitalize_surname(str) SURNAME_MAP[str.downcase] end
name_format()
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 71 def name_format @name_format ||= validate_name_format end
suffix?(str)
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 93 def suffix?(str) SUFFIX_MAP.key?(str.downcase) end
surname?(str)
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 97 def surname?(str) SURNAME_MAP.key?(str.downcase) end
tokenize_name()
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 84 def tokenize_name name.split(NAME).map do |token| { value: token, is_name: token.match?(NAME), } end end
validate_name_format()
click to toggle source
# File lib/capitalize_names/capitalizer.rb, line 75 def validate_name_format unless VALID_FORMATS.include?(options[:format]) raise CapitalizeNames::Errors::InvalidOption, "Invalid format: #{@options[:format]}, must be one of #{VALID_FORMATS.join(", ")}" end options[:format] end