Class: DorIndexing::Builders::PublisherNameBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dor_indexing/builders/publisher_name_builder.rb

Overview

Builds the publisher name for a solr document

Class Method Summary collapse

Class Method Details

.build(events) ⇒ Object



7
8
9
10
11
# File 'lib/dor_indexing/builders/publisher_name_builder.rb', line 7

def self.build(events)
  roles = publisher_roles(events)

  publisher_names_for(roles)
end

.contributor_name(contributor) ⇒ Object



31
32
33
# File 'lib/dor_indexing/builders/publisher_name_builder.rb', line 31

def self.contributor_name(contributor)
  contributor.name.flat_map { |name| flat_name(name) }
end

.flat_name(value) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/dor_indexing/builders/publisher_name_builder.rb', line 35

def self.flat_name(value)
  primary_name = value.parallelValue&.find { |role| role.status == 'primary' }
  return parallel_name(value.parallelValue) if !primary_name && value.parallelValue.present?

  return name_for(primary_name) if primary_name

  name_for(value)
end

.name_for(name) ⇒ Object



44
45
46
# File 'lib/dor_indexing/builders/publisher_name_builder.rb', line 44

def self.name_for(name)
  name.structuredValue.present? ? name.structuredValue.map(&:value).join('. ') : name.value
end

.parallel_name(names) ⇒ Object



48
49
50
# File 'lib/dor_indexing/builders/publisher_name_builder.rb', line 48

def self.parallel_name(names)
  names.map { |single_name| name_for(single_name) }.join(' : ')
end

.publisher_names_for(publisher_roles) ⇒ Object

Returns the primary publisher if available.



21
22
23
24
25
26
27
28
29
# File 'lib/dor_indexing/builders/publisher_name_builder.rb', line 21

def self.publisher_names_for(publisher_roles)
  return if publisher_roles.blank?

  primary_publisher = publisher_roles.find { |role| role.status == 'primary' }

  return contributor_name(primary_publisher).first if primary_publisher

  publisher_roles.flat_map { |contributor| contributor_name(contributor) }.join(' : ')
end

.publisher_roles(events) ⇒ Object



13
14
15
16
17
18
# File 'lib/dor_indexing/builders/publisher_name_builder.rb', line 13

def self.publisher_roles(events)
  contributors = events.flat_map(&:contributor).compact
  return [] if contributors.blank?

  contributors.select { |contributor| Array(contributor.role).any? { |role| role.value&.downcase == 'publisher' } }
end