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
14 def display_order
15   [given_part, family_part, suffix].compact.reject(&:empty?).join(' ')
16 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
25 def initials(options = {})
26   options = Name.defaults[:initials].merge(options)
27 
28   if options[:expand]
29     [initials_of(given_part, options), family].compact.join(' ')
30   else
31     initials_of([given_part, family_part].join(' '), options)
32   end
33 end
sort_order(delimiter = ', ') click to toggle source

@return [String] the name in sort order

   # File lib/namae/name.rb
 9 def sort_order(delimiter = ', ')
10   [family_part, suffix, given_part].compact.reject(&:empty?).join(delimiter)
11 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
55 def existing_initials_of(name, options = {})
56   return unless name
57 
58   i = name.dup
59 
60   i.gsub!(/\.+/, '')
61   i.gsub!(/\b[[:upper:]]+\b/) { |m| m.chars.to_a.join(' ') }
62 
63   if options[:dots]
64     i.gsub!(/\b([[:upper:]])\b/, '\1.')
65   end
66 
67   if !options[:spaces]
68     i.gsub!(/\b([[:upper:]]\.?)\s+/, '\1')
69   end
70 
71   i
72 end
family_part() click to toggle source
   # File lib/namae/name.rb
37 def family_part
38   [particle, family].compact.join(' ')
39 end
given_part() click to toggle source
   # File lib/namae/name.rb
41 def given_part
42   [given, dropping_particle].compact.join(' ')
43 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
47 def initials_of(name, options = {})
48   i = name.gsub(/([[:upper:]])[[:lower:]]+/, options[:dots] ? '\1.' : '\1')
49   i.gsub!(/\s+/, '') unless options[:spaces]
50   i
51 end