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, line 127
def initialize(attributes = {}, sanitize = false)
  super(*attributes.values_at(*Name.parts))

  if sanitize && suffix && !given && family
    tokens = family.split(/\s+/)

    # Display-order plus comma suffix special case
    if tokens.length > 1
      self.family = tokens.pop
      self.given = tokens.join(' ')
    end
  end
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, line 114
def parse(name)
  parse!(name)
rescue
  new
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, line 108
def parse!(name)
  Parser.instance.parse!(name)[0] || new
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, line 142
def empty?
  values.compact.empty?
end
inspect() click to toggle source

@return [String] a string representation of the name

# File lib/namae/name.rb, line 194
def inspect
  "#<Name #{each_pair.map { |k,v| [k,v.inspect].join('=') if v }.compact.join(' ')}>"
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, line 150
def merge(other)
  raise ArgumentError, "failed to merge #{other.class} into Name" unless
    other.respond_to?(:each_pair)

  other.each_pair do |part, value|
    writer = "#{part}="
    send(writer, value) if !value.nil? && respond_to?(writer)
  end

  self
end
merge_particles!() click to toggle source
# File lib/namae/name.rb, line 186
def merge_particles!
  self.family = [dropping_particle, particle, family].compact.join(' ')
  self.dropping_particle = nil
  self.particle = nil
  self
end
normalize_initials(options = {}) click to toggle source
# File lib/namae/name.rb, line 178
def normalize_initials(options = {})
  return self if given.nil?

  options = Name.defaults[:initials].merge(options)
  self.given = existing_initials_of given, options
  self
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, line 174
def values_at(*arguments)
  super(*arguments.flatten.map { |k| k.is_a?(Symbol) ? Name.parts.index(k) : k })
end