Class: Nomener::Name

Inherits:
Struct
  • Object
show all
Defined in:
lib/nomener/name.rb

Overview

name class for general purposes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nomen = '') ⇒ Name

Public: Create an instance!



11
12
13
14
15
# File 'lib/nomener/name.rb', line 11

def initialize(nomen = '')
  return @original = '' unless nomen.is_a?(String)
  @original = Cleaner.reformat nomen
  parse
end

Instance Attribute Details

#firstObject

Returns the value of attribute first

Returns:

  • (Object)

    the current value of first



6
7
8
# File 'lib/nomener/name.rb', line 6

def first
  @first
end

#lastObject

Returns the value of attribute last

Returns:

  • (Object)

    the current value of last



6
7
8
# File 'lib/nomener/name.rb', line 6

def last
  @last
end

#middleObject

Returns the value of attribute middle

Returns:

  • (Object)

    the current value of middle



6
7
8
# File 'lib/nomener/name.rb', line 6

def middle
  @middle
end

#nickObject

Returns the value of attribute nick

Returns:

  • (Object)

    the current value of nick



6
7
8
# File 'lib/nomener/name.rb', line 6

def nick
  @nick
end

#originalObject (readonly)

we don’t want to change what we were instantiated with



8
9
10
# File 'lib/nomener/name.rb', line 8

def original
  @original
end

#suffixObject

Returns the value of attribute suffix

Returns:

  • (Object)

    the current value of suffix



6
7
8
# File 'lib/nomener/name.rb', line 6

def suffix
  @suffix
end

#titleObject

Returns the value of attribute title

Returns:

  • (Object)

    the current value of title



6
7
8
# File 'lib/nomener/name.rb', line 6

def title
  @title
end

Instance Method Details

#capit(nomen) ⇒ Object

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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

#fullObject Also known as: fullname

Public: Shortcut for name format

can also be called by the method fullname

Returns the full name



134
135
136
# File 'lib/nomener/name.rb', line 134

def full
  name Nomener.config.format
end

#givenObject

Public: Return the first name

Returns a string of the first name



99
100
101
# File 'lib/nomener/name.rb', line 99

def given
  first
end

#inspectObject

Public: Make inspect … informative

Returns a nicely formatted string



81
82
83
84
85
86
# 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) ⇒ Object

Internal: merge another Nomener::Name to this one

other - hash to merge into self

Returns nothing



151
152
153
154
# 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) ⇒ Object

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



119
120
121
122
123
124
125
126
127
128
# 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

#parseObject

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



24
25
26
27
28
# File 'lib/nomener/name.rb', line 24

def parse
  parsed = Parser.parse(@original.dup)
  merge(parsed) unless parsed.nil?
  self
end

#properlikeObject

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

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



33
34
35
36
37
38
39
40
41
# 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

#surnameObject Also known as: family

Public: an alias for the last name

Returns a string of the last name



91
92
93
# File 'lib/nomener/name.rb', line 91

def surname
  last
end

#to_hObject

Public: return self as a hash. For ruby 1.9.3

Returns a hash of the name parts



159
160
161
# File 'lib/nomener/name.rb', line 159

def to_h
  Hash[each_pair.to_a]
end

#to_sObject

Public: See name

Returns the name as a string



142
143
144
# File 'lib/nomener/name.rb', line 142

def to_s
  name '%f %l'
end