class Namae::Name

A Name represents a single personal name, exposing its constituent parts (e.g., family name, given name etc.). Name instances are typically created and returned from {Namae.parse Namae.parse}.

name = Namae.parse('Yukihiro "Matz" Matsumoto')[0]

name.family #=> Matsumoto
name.nick #=> Matz
name.given #=> Yukihiro

Attributes

defaults[R]
parts[R]

Public Class Methods

new(attributes = {}, sanitize = false) click to toggle source

@param attributes [Hash] the individual parts of the name @param sanitize [Boolean] whether or not to apply extra

sanitation rules

@example

Name.new(:family => 'Matsumoto')
Calls superclass method
    # File lib/namae/name.rb
127 def initialize(attributes = {}, sanitize = false)
128   super(*attributes.values_at(*Name.parts))
129 
130   if sanitize && suffix && !given && family
131     tokens = family.split(/\s+/)
132 
133     # Display-order plus comma suffix special case
134     if tokens.length > 1
135       self.family = tokens.pop
136       self.given = tokens.join(' ')
137     end
138   end
139 end
parse(name) click to toggle source

@param name [String] the name to be parsed @return [Name] the parsed name

    # File lib/namae/name.rb
114 def parse(name)
115   parse!(name)
116 rescue
117   new
118 end
parse!(name) click to toggle source

@param name [String] the name to be parsed @raise [ArgumentError] if the name cannot be parsed or if the input

contains more than a single name

@return [Name] the parsed name

    # File lib/namae/name.rb
108 def parse!(name)
109   Parser.instance.parse!(name)[0] || new
110 end

Public Instance Methods

empty?() click to toggle source

@return [Boolean] whether or not all the name components are nil.

    # File lib/namae/name.rb
142 def empty?
143   values.compact.empty?
144 end
inspect() click to toggle source

@return [String] a string representation of the name

    # File lib/namae/name.rb
194 def inspect
195   "#<Name #{each_pair.map { |k,v| [k,v.inspect].join('=') if v }.compact.join(' ')}>"
196 end
merge(other) click to toggle source

Merges the name with the passed-in name or hash.

@param other [#each_pair] the other name or hash @return [self]

    # File lib/namae/name.rb
150 def merge(other)
151   raise ArgumentError, "failed to merge #{other.class} into Name" unless
152     other.respond_to?(:each_pair)
153 
154   other.each_pair do |part, value|
155     writer = "#{part}="
156     send(writer, value) if !value.nil? && respond_to?(writer)
157   end
158 
159   self
160 end
merge_particles!() click to toggle source
    # File lib/namae/name.rb
186 def merge_particles!
187   self.family = [dropping_particle, particle, family].compact.join(' ')
188   self.dropping_particle = nil
189   self.particle = nil
190   self
191 end
normalize_initials(options = {}) click to toggle source
    # File lib/namae/name.rb
178 def normalize_initials(options = {})
179   return self if given.nil?
180 
181   options = Name.defaults[:initials].merge(options)
182   self.given = existing_initials_of given, options
183   self
184 end
values_at(*arguments) click to toggle source

@overload values_at(selector, … )

Returns an array containing the elements in self corresponding to
the given selector(s). The selectors may be either integer indices,
ranges (functionality inherited from Struct) or symbols
idenifying valid keys.

@example

name.values_at(:family, :nick) #=> ['Matsumoto', 'Matz']

@see Struct#values_at @return [Array] the list of values

Calls superclass method
    # File lib/namae/name.rb
174 def values_at(*arguments)
175   super(*arguments.flatten.map { |k| k.is_a?(Symbol) ? Name.parts.index(k) : k })
176 end