module Namae::NameFormatting

NameFormatting can be mixed in by an object providing individual name parts (family, given, suffix, particle, etc.) to add support for name formatting.

Public Instance Methods

display_order() click to toggle source

@return [String] the name in display order

# File lib/namae/name.rb, line 14
def display_order
  [given_part, family_part, suffix].compact.reject(&:empty?).join(' ')
end
initials(options = {}) click to toggle source

@param options [Hash] the options to create the initials

@option options [true,false] :expand (false) whether or not to expand the family name @option options [true,false] :dots (true) whether or not to print dots between the initials @option options [true,false] :spaces (false) whether or not to print spaces between the initals

@return [String] the name's initials.

# File lib/namae/name.rb, line 25
def initials(options = {})
  options = Name.defaults[:initials].merge(options)

  if options[:expand]
    [initials_of(given_part, options), family].compact.join(' ')
  else
    initials_of([given_part, family_part].join(' '), options)
  end
end
sort_order(delimiter = ', ') click to toggle source

@return [String] the name in sort order

# File lib/namae/name.rb, line 9
def sort_order(delimiter = ', ')
  [family_part, suffix, given_part].compact.reject(&:empty?).join(delimiter)
end

Private Instance Methods

existing_initials_of(name, options = {}) click to toggle source

@param name [String] a name or part of a name @return [String] the passed-in name with normalized initials

# File lib/namae/name.rb, line 55
def existing_initials_of(name, options = {})
  return unless name

  i = name.dup

  i.gsub!(/\.+/, '')
  i.gsub!(/\b[[:upper:]]+\b/) { |m| m.chars.to_a.join(' ') }

  if options[:dots]
    i.gsub!(/\b([[:upper:]])\b/, '\1.')
  end

  if !options[:spaces]
    i.gsub!(/\b([[:upper:]]\.?)\s+/, '\1')
  end

  i
end
family_part() click to toggle source
# File lib/namae/name.rb, line 37
def family_part
  [particle, family].compact.join(' ')
end
given_part() click to toggle source
# File lib/namae/name.rb, line 41
def given_part
  [given, dropping_particle].compact.join(' ')
end
initials_of(name, options = {}) click to toggle source

@param name [String] a name or part of a name @return [String] the initials of the passed-in name

# File lib/namae/name.rb, line 47
def initials_of(name, options = {})
  i = name.gsub(/([[:upper:]])[[:lower:]]+/, options[:dots] ? '\1.' : '\1')
  i.gsub!(/\s+/, '') unless options[:spaces]
  i
end