class NameOfPerson::PersonName

Attributes

first[R]
last[R]

Public Class Methods

full(full_name) click to toggle source
# File lib/name_of_person/person_name.rb, line 7
def self.full(full_name)
  first, last = full_name.to_s.squish.split(/\s/, 2)
  new(first, last) if first.present?
end
new(first, last = nil) click to toggle source
Calls superclass method
# File lib/name_of_person/person_name.rb, line 12
def initialize(first, last = nil)
  raise ArgumentError, "First name is required" unless first.present?
  @first, @last = first, last
  super full
end

Public Instance Methods

abbreviated() click to toggle source

Returns first initial + last, such as “J. Fried”.

# File lib/name_of_person/person_name.rb, line 29
def abbreviated
  @abbreviated ||= last.present? ? "#{first.first}. #{last}" : first
end
encode_with(coder) click to toggle source

Override to_yaml to serialize as a plain string.

# File lib/name_of_person/person_name.rb, line 54
def encode_with(coder)
  coder.represent_scalar nil, to_s
end
familiar() click to toggle source

Returns first + last initial, such as “Jason F.”.

# File lib/name_of_person/person_name.rb, line 24
def familiar
  @familiar ||= last.present? ? "#{first} #{last.first}." : first
end
full() click to toggle source

Returns first + last, such as “Jason Fried”.

# File lib/name_of_person/person_name.rb, line 19
def full
  @full ||= last.present? ? "#{first} #{last}" : first
end
initials() click to toggle source

Returns just the initials.

# File lib/name_of_person/person_name.rb, line 44
def initials
  @initials ||= remove(/(\(|\[).*(\)|\])/).scan(/([[:word:]])[[:word:]]+/i).join
end
mentionable() click to toggle source

Returns a mentionable version of the familiar name

# File lib/name_of_person/person_name.rb, line 49
def mentionable
  @mentionable ||= familiar.chop.delete(' ').downcase
end
possessive() click to toggle source

Returns full name with with trailing 's or ' if name ends in s.

# File lib/name_of_person/person_name.rb, line 39
def possessive
  @possessive ||= "#{self}'#{"s" unless end_with?("s")}"
end
sorted() click to toggle source

Returns last + first for sorting.

# File lib/name_of_person/person_name.rb, line 34
def sorted
  @sorted ||= last.present? ? "#{last}, #{first}" : first
end