Class: CocinaDisplay::Contributors::Contributor

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

Overview

A contributor to a work, such as an author or publisher.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cocina) ⇒ Contributor

Initialize a Contributor object with Cocina structured data.

Parameters:

  • cocina (Hash)

    The Cocina structured data for the contributor.



11
12
13
# File 'lib/cocina_display/contributors/contributor.rb', line 11

def initialize(cocina)
  @cocina = cocina
end

Instance Attribute Details

#cocinaObject (readonly)

Returns the value of attribute cocina.



7
8
9
# File 'lib/cocina_display/contributors/contributor.rb', line 7

def cocina
  @cocina
end

Instance Method Details

#==(other) ⇒ Object

Support equality based on the underlying Cocina data.

Parameters:

  • other (Object)


23
24
25
# File 'lib/cocina_display/contributors/contributor.rb', line 23

def ==(other)
  other.is_a?(Contributor) && other.cocina == cocina
end

#author?Boolean

Does this contributor have a role that indicates they are an author?

Returns:

  • (Boolean)


59
60
61
# File 'lib/cocina_display/contributors/contributor.rb', line 59

def author?
  roles.any?(&:author?)
end

#conference?Boolean

Is this contributor a conference?

Returns:

  • (Boolean)


47
48
49
# File 'lib/cocina_display/contributors/contributor.rb', line 47

def conference?
  cocina["type"] == "conference"
end

#display_name(with_date: false) ⇒ String?

The primary display name for the contributor as a string.

Parameters:

  • with_date (Boolean) (defaults to: false)

    Include life dates, if present

Returns:

  • (String, nil)


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

def display_name(with_date: false)
  primary_name&.to_s(with_date: with_date)
end

#display_names(with_date: false) ⇒ Array<String>

String renderings of all names for the contributor.

Parameters:

  • with_date (Boolean) (defaults to: false)

    Include life dates, if present

Returns:

  • (Array<String>)


91
92
93
# File 'lib/cocina_display/contributors/contributor.rb', line 91

def display_names(with_date: false)
  names.map { |name| name.to_s(with_date: with_date) }.compact_blank
end

#forenameString?

The forename for the contributor, if structured name info is available.

Returns:

  • (String, nil)

See Also:

  • Contributor::Name::forename_str


107
108
109
# File 'lib/cocina_display/contributors/contributor.rb', line 107

def forename
  names.map(&:forename_str).first.presence
end

#funder?Boolean

Does this contributor have a role that indicates they are a funder?

Returns:

  • (Boolean)


71
72
73
# File 'lib/cocina_display/contributors/contributor.rb', line 71

def funder?
  roles.any?(&:funder?)
end

#identifiersArray<Identifier>

Identifiers for the contributor.

Returns:



29
30
31
# File 'lib/cocina_display/contributors/contributor.rb', line 29

def identifiers
  Array(cocina["identifier"]).map { |id| Identifier.new(id) }
end

#namesArray<Name>

All names in the Cocina as Name objects. Flattens parallel values into separate Name objects.

Returns:



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cocina_display/contributors/contributor.rb', line 121

def names
  @names ||= Array(cocina["name"]).flat_map do |name|
    (Array(name["parallelValue"]).presence || [name]).filter_map do |name_value|
      unless name_value.blank?
        Name.new(name_value).tap do |name_obj|
          name_obj.type ||= name["type"]
          name_obj.status ||= name["status"]
        end
      end
    end
  end
end

#organization?Boolean

Is this contributor an organization?

Returns:

  • (Boolean)


41
42
43
# File 'lib/cocina_display/contributors/contributor.rb', line 41

def organization?
  cocina["type"] == "organization"
end

#person?Boolean

Is this contributor a human?

Returns:

  • (Boolean)


35
36
37
# File 'lib/cocina_display/contributors/contributor.rb', line 35

def person?
  cocina["type"] == "person"
end

#primary?Boolean

Is this contributor marked as primary?

Returns:

  • (Boolean)


53
54
55
# File 'lib/cocina_display/contributors/contributor.rb', line 53

def primary?
  cocina["status"] == "primary"
end

#primary_nameContributor::Name?

A single primary name for the contributor. Prefers a name of type “display” or one marked primary.

Returns:

  • (Contributor::Name, nil)


98
99
100
101
102
# File 'lib/cocina_display/contributors/contributor.rb', line 98

def primary_name
  names.find { |name| name.type == "display" }.presence ||
    names.find(&:primary?).presence ||
    names.first
end

#publisher?Boolean

Does this contributor have a role that indicates they are a publisher?

Returns:

  • (Boolean)


65
66
67
# File 'lib/cocina_display/contributors/contributor.rb', line 65

def publisher?
  roles.any?(&:publisher?)
end

#role?Boolean

Does this contributor have any roles defined?

Returns:

  • (Boolean)


77
78
79
# File 'lib/cocina_display/contributors/contributor.rb', line 77

def role?
  roles.any?
end

#rolesArray<Hash>

All roles in the Cocina structured data.

Returns:

  • (Array<Hash>)


136
137
138
# File 'lib/cocina_display/contributors/contributor.rb', line 136

def roles
  @roles ||= Array(cocina["role"]).map { |role| Role.new(role) }
end

#surnameString?

The surname for the contributor, if structured name info is available.

Returns:

  • (String, nil)

See Also:

  • Contributor::Name::surname_str


114
115
116
# File 'lib/cocina_display/contributors/contributor.rb', line 114

def surname
  names.map(&:surname_str).first.presence
end

#to_sString?

String representation of the contributor, using display name with date.

Returns:

  • (String, nil)


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

def to_s
  display_name(with_date: true)
end