class Nomener::Name

name class for general purposes

Attributes

original[R]

Public Class Methods

new(nomen = '') click to toggle source

Public: Create an instance!

# File lib/nomener/name.rb, line 11
def initialize(nomen = '')
  return @original = '' unless nomen.is_a?(String)
  @original = Cleaner.reformat nomen
  parse
end

Public Instance Methods

capit(nomen) click to toggle source

Internal: try to capitalize names including tough ones like Mac and Mc and D’ and such

nomen - string of the name to capitalize

Returns a string of the capitalized name

# File lib/nomener/name.rb, line 48
def capit(nomen)
  # if there are multiple names separated by a dash
  fix = nomen.to_s.dup.split('-').map do |outer|
    outer.split(' ').map(&:capitalize).join ' '
  end.join '-'

  # anything begining with Mac and not ending in [aciozj], except for a few
  fix.sub!(/Mac(?!
    hin|hlen|
    har|
    kle|
    klin|
    kie|
    hado|     # Portugese
    evicius|  # Lithuanian
    iulis|    # Lithuanian
    ias       # Lithuanian
  )([\p{Alpha}]{2,}[^aAcCiIoOzZjJ])\b/x) { "Mac#{$1.capitalize}" }

  fix.sub!(/\bMacmurdo\b/, 'MacMurdo') # fix MacMurdo

  # anything beginning with Mc, Mcdonald == McDonald
  fix.sub!(/Mc(\p{Alpha}{2,})/) { |s| "Mc#{s[2..-1].capitalize}" }

  # names like D'Angelo or Van 't Hooft, no cap 't
  fix.gsub!(/('\p{Alpha})(?=\p{Alpha})/) { |s| "'#{s[(1..-1)].capitalize}" }

  fix
end
family()
Alias for: surname
full() click to toggle source

Public: Shortcut for name format

can also be called by the method fullname

Returns the full name

# File lib/nomener/name.rb, line 134
def full
  name Nomener.config.format
end
Also aliased as: fullname
fullname()
Alias for: full
given() click to toggle source

Public: Return the first name

Returns a string of the first name

# File lib/nomener/name.rb, line 99
def given
  first
end
inspect() click to toggle source

Public: Make inspect … informative

Returns a nicely formatted string

# File lib/nomener/name.rb, line 81
def inspect
  "#<Nomener::Name #{
    each_pair.map { |k, v| [k, v.inspect].join('=') unless v.to_s.empty? }
    .compact
    .join(' ') }>"
end
merge(other) click to toggle source

Internal: merge another Nomener::Name to this one

other - hash to merge into self

Returns nothing

# File lib/nomener/name.rb, line 151
def merge(other)
  return self unless other.is_a?(Hash)
  each_pair { |k, _| self[k] = other[k] }
end
name(format = '%f %l', _propercase = true) click to toggle source

Public: Make the name a string.

format - a string using symbols for the format of the name to return

defaults to "%f %l"
  %f -> first name
  %l -> last/surname/family name
  %m -> middle name
  %n -> nick name
  %m -> middle name
  %s -> suffix
  %t -> title/prefix

propercase - boolean on whether to (try to) fix the case of the name

defaults to true

Returns the name as a string

# File lib/nomener/name.rb, line 119
def name(format = '%f %l', _propercase = true)
  nomen = to_h
  nomen[:nick] = (nick.nil? || nick.empty?) ? '' : "\"#{nick}\""

  format = format.gsub(/%[flmnst]/,
    '%f' => '%{first}', '%l' => '%{last}', '%m' => '%{middle}',
    '%n' => '%{nick}', '%s' => '%{suffix}', '%t' => '%{title}'
  )
  (format % nomen).strip.squeeze ' '
end
parse() click to toggle source

Public: Break down a string into parts of a persons name

As of 0.2.5 parse no longer needs to be called after initialization,
it's done automatically. Recalling it doesn't hurt though.

name - A string of name to parse

Returns self populated with name or empty

# File lib/nomener/name.rb, line 24
def parse
  parsed = Parser.parse(@original.dup)
  merge(parsed) unless parsed.nil?
  self
end
properlike() click to toggle source

Public: make the name proper case-like, suffix and nickname ignored

Returns a string of the full name in a proper (western) case

# File lib/nomener/name.rb, line 33
def properlike
  [capit(title),
    capit(first),
    (nick.to_s.empty? ? '' : "#{Nomener.config.left}#{nick}#{Nomener.config.right}"),
    capit(middle),
    capit(last),
    suffix
  ].join(' ').strip.squeeze ' '
end
surname() click to toggle source

Public: an alias for the last name

Returns a string of the last name

# File lib/nomener/name.rb, line 91
def surname
  last
end
Also aliased as: family
to_h() click to toggle source

Public: return self as a hash. For ruby 1.9.3

Returns a hash of the name parts

# File lib/nomener/name.rb, line 159
def to_h
  Hash[each_pair.to_a]
end
to_s() click to toggle source

Public: See name

Returns the name as a string

# File lib/nomener/name.rb, line 142
def to_s
  name '%f %l'
end