class NameOfPerson::PersonName
def self.full(full_name)
def self.full(full_name) first, last = full_name.to_s.squish.split(/\s/, 2) new(first, last) if first.present? end
def abbreviated
def abbreviated @abbreviated ||= last.present? ? "#{first.first}. #{last}" : first end
def encode_with(coder)
def encode_with(coder) coder.represent_scalar nil, to_s end
def familiar
def familiar @familiar ||= last.present? ? "#{first} #{last.first}." : first end
def full
def full @full ||= last.present? ? "#{first} #{last}" : first end
def initialize(first, last = nil)
def initialize(first, last = nil) raise ArgumentError, "First name is required" unless first.present? @first, @last = first, last super full end
def initials
def initials @initials ||= remove(/(\(|\[).*(\)|\])/).scan(/([[:word:]])[[:word:]]*/i).join end
def mentionable
def mentionable @mentionable ||= familiar.chop.delete(' ').downcase end
def possessive(method = :full)
def possessive(method = :full) whitelist = %i[full first last abbreviated sorted initials] unless whitelist.include?(method.to_sym) raise ArgumentError, 'Please provide a valid method' end name = public_send(method) @possessive ||= "#{name}'#{'s' unless name.downcase.end_with?('s')}" end
def sorted
def sorted @sorted ||= last.present? ? "#{last}, #{first}" : first end