Class: CocinaDisplay::Contributors::Name

Inherits:
Object
  • Object
show all
Defined in:
lib/cocina_display/contributors/name.rb

Overview

A name associated with a contributor.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cocina) ⇒ Name

Initialize a Name object with Cocina structured data.

Parameters:

  • cocina (Hash)

    The Cocina structured data for the name.



21
22
23
24
25
# File 'lib/cocina_display/contributors/name.rb', line 21

def initialize(cocina)
  @cocina = cocina
  @type = cocina["type"]
  @status = cocina["status"]
end

Instance Attribute Details

#cocinaHash (readonly)

The underlying Cocina structured data for the name.

Returns:

  • (Hash)


9
10
11
# File 'lib/cocina_display/contributors/name.rb', line 9

def cocina
  @cocina
end

#statusString?

The status of the name, if any (e.g., “primary”).

Returns:

  • (String, nil)


17
18
19
# File 'lib/cocina_display/contributors/name.rb', line 17

def status
  @status
end

#typeString?

The type of name, if any (e.g., “personal”, “corporate”, etc.).

Returns:

  • (String, nil)


13
14
15
# File 'lib/cocina_display/contributors/name.rb', line 13

def type
  @type
end

Instance Method Details

#dates_strString

Flatten all life and activity dates into a comma-delimited string.

Returns:

  • (String)


78
79
80
# File 'lib/cocina_display/contributors/name.rb', line 78

def dates_str
  Utils.compact_and_join(Array(name_values["life dates"]) + Array(name_values["activity dates"]), delimiter: ", ")
end

#forename_strString

Flatten all forename values and ordinals into a whitespace-delimited string.

Returns:

  • (String)


66
67
68
# File 'lib/cocina_display/contributors/name.rb', line 66

def forename_str
  Utils.compact_and_join(Array(name_values["forename"]) + Array(name_values["ordinal"]), delimiter: " ")
end

#full_name_strString

The full name as a string, combining all name components and terms of address.

Returns:

  • (String)


46
47
48
# File 'lib/cocina_display/contributors/name.rb', line 46

def full_name_str
  Utils.compact_and_join(name_components.push(terms_of_address_str), delimiter: ", ")
end

#name_componentsArray<String>

List of all name components. If any of forename, surname, or term of address are present, those are used. Otherwise, fall back to any names explicitly marked as “name” or untyped.

Returns:

  • (Array<String>)


54
55
56
# File 'lib/cocina_display/contributors/name.rb', line 54

def name_components
  [surname_str, forename_str].compact_blank.presence || Array(name_values["name"])
end

#primary?Boolean

Is this a primary name for the contributor?

Returns:

  • (Boolean)


84
85
86
# File 'lib/cocina_display/contributors/name.rb', line 84

def primary?
  status == "primary"
end

#surname_strString

Flatten all surname values into a whitespace-delimited string.

Returns:

  • (String)


72
73
74
# File 'lib/cocina_display/contributors/name.rb', line 72

def surname_str
  Utils.compact_and_join(Array(name_values["surname"]), delimiter: " ")
end

#terms_of_address_strString

Flatten all terms of address into a comma-delimited string.

Returns:

  • (String)


60
61
62
# File 'lib/cocina_display/contributors/name.rb', line 60

def terms_of_address_str
  Utils.compact_and_join(Array(name_values["term of address"]), delimiter: ", ")
end

#to_s(with_date: false) ⇒ String

The display string for the name, optionally including life dates.

Examples:

no dates

name.to_s # => "King, Martin Luther, Jr."

with dates

name.to_s(with_date: true) # => "King, Martin Luther, Jr., 1929-1968"

Parameters:

  • with_date (Boolean) (defaults to: false)

    Include life dates, if present

Returns:

  • (String)


34
35
36
37
38
39
40
41
42
# File 'lib/cocina_display/contributors/name.rb', line 34

def to_s(with_date: false)
  if cocina["value"].present?
    cocina["value"]
  elsif dates_str.present? && with_date
    Utils.compact_and_join([full_name_str, dates_str], delimiter: ", ")
  else
    full_name_str
  end
end