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
Public Class Methods
@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')
# 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
@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
@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
@return [Boolean] whether or not all the name components are nil.
# File lib/namae/name.rb, line 142 def empty? values.compact.empty? end
@return [String] a string representation of the name
# File lib/namae/name.rb, line 187 def inspect "#<Name #{each_pair.map { |k,v| [k,v.inspect].join('=') if v }.compact.join(' ')}>" end
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
# 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
@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
# 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