Class: NameOfPerson::PersonName

Inherits:
String
  • Object
show all
Defined in:
lib/name_of_person/person_name.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first, last = nil) ⇒ PersonName

Returns a new instance of PersonName.

Raises:

  • (ArgumentError)


13
14
15
16
17
# File 'lib/name_of_person/person_name.rb', line 13

def initialize(first, last = nil)
  raise ArgumentError, "First name is required" unless first.present?
  @first, @last = first, last
  super full
end

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



6
7
8
# File 'lib/name_of_person/person_name.rb', line 6

def first
  @first
end

#lastObject (readonly)

Returns the value of attribute last.



6
7
8
# File 'lib/name_of_person/person_name.rb', line 6

def last
  @last
end

Class Method Details

.full(full_name) ⇒ Object



8
9
10
11
# File 'lib/name_of_person/person_name.rb', line 8

def self.full(full_name)
  first, last = full_name.to_s.squish.split(/\s/, 2)
  new(first, last) if first.present?
end

Instance Method Details

#abbreviatedObject

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



30
31
32
# File 'lib/name_of_person/person_name.rb', line 30

def abbreviated
  @abbreviated ||= last.present? ? "#{first.first}. #{last}" : first
end

#encode_with(coder) ⇒ Object

Override to_yaml to serialize as a plain string.



63
64
65
# File 'lib/name_of_person/person_name.rb', line 63

def encode_with(coder)
  coder.represent_scalar nil, to_s
end

#familiarObject

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



25
26
27
# File 'lib/name_of_person/person_name.rb', line 25

def familiar
  @familiar ||= last.present? ? "#{first} #{last.first}." : first
end

#fullObject

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



20
21
22
# File 'lib/name_of_person/person_name.rb', line 20

def full
  @full ||= last.present? ? "#{first} #{last}" : first
end

#initialsObject

Returns just the initials.



53
54
55
# File 'lib/name_of_person/person_name.rb', line 53

def initials
  @initials ||= remove(/(\(|\[).*(\)|\])/).scan(/([[:word:]])[[:word:]]*/i).join
end

#mentionableObject

Returns a mentionable version of the familiar name



58
59
60
# File 'lib/name_of_person/person_name.rb', line 58

def mentionable
  @mentionable ||= familiar.chop.delete(' ').downcase
end

#possessive(method = :full) ⇒ Object

Returns full name with with trailing ‘s or ’ if name ends in s.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/name_of_person/person_name.rb', line 40

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

#sortedObject

Returns last + first for sorting.



35
36
37
# File 'lib/name_of_person/person_name.rb', line 35

def sorted
  @sorted ||= last.present? ? "#{last}, #{first}" : first
end