Class: DorIndexing::Builders::NameBuilder

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

Overview

class methods return the name values to go in Solr document fields

used for both contributors and for topics

Class Method Summary collapse

Class Method Details

.build_all(cocina_contributors) ⇒ Array<String>

Returns names.

Parameters:

  • strategy (Symbol)

    “:first” is the strategy for how to choose a name if primary and display name is not found

Returns:

  • (Array<String>)

    names



10
11
12
13
# File 'lib/dor_indexing/builders/name_builder.rb', line 10

def self.build_all(cocina_contributors)
  flat_names = cocina_contributors.filter_map { |cocina_contributor| flat_names_for(cocina_contributor) }.flatten
  flat_names.filter_map { |name| build_name(name) }
end

.build_name(name) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dor_indexing/builders/name_builder.rb', line 29

def self.build_name(name)
  if name.groupedValue.present?
    name.groupedValue.find { |grouped_value| grouped_value.type == 'name' }&.value
  elsif name.structuredValue.present?
    name_part = joined_name_parts(name, 'name', '. ').presence
    surname = joined_name_parts(name, 'surname', ' ')
    forename = joined_name_parts(name, 'forename', ' ')
    terms_of_address = joined_name_parts(name, 'term of address', ', ')
    life_dates = joined_name_parts(name, 'life dates', ', ')
    activity_dates = joined_name_parts(name, 'activity dates', ', ')
    joined_name = name_part || join_parts([surname, forename], ', ')
    joined_name = join_parts([joined_name, terms_of_address], ' ')
    joined_name = join_parts([joined_name, life_dates], ', ')
    join_parts([joined_name, activity_dates], ', ')
  else
    name.value
  end
end

.build_primary_name(names, strategy: :first) ⇒ String

Returns name.

Parameters:

  • strategy (Symbol) (defaults to: :first)

    “:first” is the strategy for how to choose a name if primary and display name is not found

Returns:

  • (String)

    name



17
18
19
20
21
22
23
24
25
# File 'lib/dor_indexing/builders/name_builder.rb', line 17

def self.build_primary_name(names, strategy: :first)
  names = Array(names) unless names.is_a?(Array)
  flat_names = flat_names_for(names)
  name = display_name_for(flat_names) || primary_name_for(flat_names)
  name ||= flat_names.first if strategy == :first
  return build_name(name) if name

  flat_names.filter_map { |one| build_name(one) }.first
end

.display_name_for(names) ⇒ Object

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize



50
51
52
# File 'lib/dor_indexing/builders/name_builder.rb', line 50

def self.display_name_for(names)
  names.find { |name| name.type == 'display' }
end

.flat_names_for(names) ⇒ Object



58
59
60
# File 'lib/dor_indexing/builders/name_builder.rb', line 58

def self.flat_names_for(names)
  names.flat_map { |name| name.parallelValue.presence || name }
end

.join_parts(parts, joiner) ⇒ Object



66
67
68
# File 'lib/dor_indexing/builders/name_builder.rb', line 66

def self.join_parts(parts, joiner)
  parts.compact_blank.join(joiner)
end

.joined_name_parts(name, type, joiner) ⇒ Object



62
63
64
# File 'lib/dor_indexing/builders/name_builder.rb', line 62

def self.joined_name_parts(name, type, joiner)
  join_parts(name.structuredValue.select { |structured_value| structured_value.type == type }.map(&:value), joiner)
end

.primary_name_for(names) ⇒ Object



54
55
56
# File 'lib/dor_indexing/builders/name_builder.rb', line 54

def self.primary_name_for(names)
  names.find { |name| name.status == 'primary' }
end