Module: CocinaDisplay::Concerns::Contributors
- Included in:
- CocinaDisplay::CocinaRecord, RelatedResource
- Defined in:
- lib/cocina_display/concerns/contributors.rb
Overview
Methods for finding and formatting names for contributors
Instance Method Summary collapse
-
#additional_contributor_names(with_date: false) ⇒ Array<String>
All contributor names except the main one, formatted for display.
-
#additional_contributors ⇒ Array<Contributor>
All contributors except the main one.
-
#author_contributors ⇒ Array<Contributor>
All contributors with an “author” role.
-
#author_names ⇒ Array<String>
All names of authors, formatted for display.
-
#conference_contributor_names ⇒ Array<String>
All names of contributors that are conferences, formatted for display.
-
#contributor_display_data ⇒ Array<DisplayData>
DisplayData for Contributors, one per role (excluding publisher).
-
#contributor_names_by_role(with_date: false) ⇒ Hash<String, Array<String>>
A hash mapping role names to the names of contributors with that role.
-
#contributors ⇒ Array<Contributor>
All contributors for the object, including authors, editors, etc.
-
#contributors_by_role(with_date: false) ⇒ Hash<[String,NilClass], Array<Contributor>>
A hash mapping role names to the names of contributors with that role.
-
#impersonal_contributor_names ⇒ Array<String>
All names of non-person contributors, formatted for display.
-
#main_contributor ⇒ Contributor?
Object representing the main contributor.
-
#main_contributor_name(with_date: false) ⇒ String?
The main contributor’s name, formatted for display.
-
#organization_contributor_names ⇒ Array<String>
All names of contributors that are organizations, formatted for display.
-
#person_contributor_names(with_date: false) ⇒ Array<String>
All names of contributors who are people, formatted for display.
-
#publisher_contributors ⇒ Array<Contributor>
All contributors with a “publisher” role.
-
#publisher_names ⇒ Array<String>
All names of publishers, formatted for display.
-
#sort_contributor_name ⇒ String
A string value for sorting by contributor that sorts missing values last.
Instance Method Details
#additional_contributor_names(with_date: false) ⇒ Array<String>
All contributor names except the main one, formatted for display.
20 21 22 |
# File 'lib/cocina_display/concerns/contributors.rb', line 20 def additional_contributor_names(with_date: false) additional_contributors.flat_map { |c| c.display_names(with_date: with_date) }.compact end |
#additional_contributors ⇒ Array<Contributor>
All contributors except the main one.
147 148 149 150 |
# File 'lib/cocina_display/concerns/contributors.rb', line 147 def additional_contributors return [] if contributors.empty? || contributors.one? contributors - [main_contributor] end |
#author_contributors ⇒ Array<Contributor>
All contributors with an “author” role.
123 124 125 |
# File 'lib/cocina_display/concerns/contributors.rb', line 123 def contributors.filter(&:author?) end |
#author_names ⇒ Array<String>
All names of authors, formatted for display.
32 33 34 |
# File 'lib/cocina_display/concerns/contributors.rb', line 32 def .flat_map(&:display_names).compact end |
#conference_contributor_names ⇒ Array<String>
All names of contributors that are conferences, formatted for display.
59 60 61 |
# File 'lib/cocina_display/concerns/contributors.rb', line 59 def conference_contributor_names contributors.filter(&:conference?).flat_map(&:display_names).compact end |
#contributor_display_data ⇒ Array<DisplayData>
DisplayData for Contributors, one per role (excluding publisher). Contributors with no role are grouped under a default heading.
92 93 94 95 96 97 98 |
# File 'lib/cocina_display/concerns/contributors.rb', line 92 def contributor_display_data contributors_by_role.except("publisher").map do |role, contributors| label = I18n.t(role, scope: "cocina_display.contributor.role", default: role&.capitalize || I18n.t("default", scope: "cocina_display.contributor.role")) DisplayData.new(label: label, objects: contributors) end end |
#contributor_names_by_role(with_date: false) ⇒ Hash<String, Array<String>>
A hash mapping role names to the names of contributors with that role.
66 67 68 69 70 |
# File 'lib/cocina_display/concerns/contributors.rb', line 66 def contributor_names_by_role(with_date: false) contributors_by_role(with_date: with_date) .transform_values { |contributor_list| contributor_list.flat_map { |contributor| contributor.display_names(with_date: with_date) }.compact_blank } .compact_blank end |
#contributors ⇒ Array<Contributor>
All contributors for the object, including authors, editors, etc. Checks both description.contributor and description.event.contributor.
113 114 115 116 117 118 |
# File 'lib/cocina_display/concerns/contributors.rb', line 113 def contributors @contributors ||= Enumerator::Chain.new( path("$.description.contributor.*"), path("$.description.event.*.contributor.*") ).map { |c| CocinaDisplay::Contributors::Contributor.new(c) } end |
#contributors_by_role(with_date: false) ⇒ Hash<[String,NilClass], Array<Contributor>>
A hash mapping role names to the names of contributors with that role.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/cocina_display/concerns/contributors.rb', line 75 def contributors_by_role(with_date: false) @contributors_by_role ||= contributors.each_with_object({}) do |contributor, hash| if contributor.roles.empty? hash[nil] ||= [] hash[nil] << contributor else contributor.roles.each do |role| hash[role.to_s] ||= [] hash[role.to_s] << contributor end end end end |
#impersonal_contributor_names ⇒ Array<String>
All names of non-person contributors, formatted for display. This includes organizations, conferences, families, etc.
47 48 49 |
# File 'lib/cocina_display/concerns/contributors.rb', line 47 def impersonal_contributor_names contributors.reject(&:person?).flat_map(&:display_names).compact end |
#main_contributor ⇒ Contributor?
Object representing the main contributor. Selected according to the following rules:
-
If there are contributors marked as primary, use the first one.
-
If there are no primary contributors, use the first contributor with no role.
-
If there are no contributors without a role, use the first contributor.
141 142 143 |
# File 'lib/cocina_display/concerns/contributors.rb', line 141 def main_contributor contributors.find(&:primary?).presence || contributors.find { |c| !c.role? }.presence || contributors.first end |
#main_contributor_name(with_date: false) ⇒ String?
The main contributor’s name, formatted for display.
13 14 15 |
# File 'lib/cocina_display/concerns/contributors.rb', line 13 def main_contributor_name(with_date: false) main_contributor&.display_name(with_date: with_date) end |
#organization_contributor_names ⇒ Array<String>
All names of contributors that are organizations, formatted for display.
53 54 55 |
# File 'lib/cocina_display/concerns/contributors.rb', line 53 def organization_contributor_names contributors.filter(&:organization?).flat_map(&:display_names).compact end |
#person_contributor_names(with_date: false) ⇒ Array<String>
All names of contributors who are people, formatted for display.
39 40 41 |
# File 'lib/cocina_display/concerns/contributors.rb', line 39 def person_contributor_names(with_date: false) contributors.filter(&:person?).flat_map { |c| c.display_names(with_date: with_date) }.compact end |
#publisher_contributors ⇒ Array<Contributor>
All contributors with a “publisher” role.
130 131 132 |
# File 'lib/cocina_display/concerns/contributors.rb', line 130 def publisher_contributors contributors.filter(&:publisher?) end |
#publisher_names ⇒ Array<String>
All names of publishers, formatted for display.
26 27 28 |
# File 'lib/cocina_display/concerns/contributors.rb', line 26 def publisher_names publisher_contributors.flat_map(&:display_names).compact end |
#sort_contributor_name ⇒ String
A string value for sorting by contributor that sorts missing values last. Appends the sort title to break ties between contributor names. Ignores punctuation and leading/trailing spaces.
104 105 106 107 108 |
# File 'lib/cocina_display/concerns/contributors.rb', line 104 def sort_contributor_name sort_name = main_contributor&.display_name || "\u{10FFFF}" sort_name_title = [sort_name, sort_title].join(" ") sort_name_title.gsub(/[[:punct:]]*/, "").strip end |