Class: Cocina::Models::Builders::TitleBuilder

Inherits:
Object
  • Object
show all
Extended by:
Deprecation
Defined in:
lib/cocina/models/builders/title_builder.rb

Overview

TitleBuilder selects the prefered title from the cocina object for solr indexing rubocop:disable Metrics/ClassLength

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strategy:, add_punctuation:) ⇒ TitleBuilder

Returns a new instance of TitleBuilder.



27
28
29
30
# File 'lib/cocina/models/builders/title_builder.rb', line 27

def initialize(strategy:, add_punctuation:)
  @strategy = strategy
  @add_punctuation = add_punctuation
end

Class Method Details

.build(titles, strategy: :first, add_punctuation: true) ⇒ String

Returns the title value for Solr.

Parameters:

  • [Array<Cocina::Models::Title,Cocina::Models::DescriptiveValue>] ([Array<Cocina::Models::Title,Cocina::Models::DescriptiveValue>] titles the titles to consider)

    titles the titles to consider

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

    “:first” is the strategy for selection when primary or display title are missing

  • add_punctuation (Boolean) (defaults to: true)

    determines if the title should be formmated with punctuation

Returns:

  • (String)

    the title value for Solr



17
18
19
20
21
22
23
24
25
# File 'lib/cocina/models/builders/title_builder.rb', line 17

def self.build(titles, strategy: :first, add_punctuation: true)
  if titles.respond_to?(:description)
    Deprecation.warn(self,
                     "Calling TitleBuilder.build with a #{titles.class} is deprecated. " \
                     'It must be called with an array of titles')
    titles = titles.description.title
  end
  new(strategy: strategy, add_punctuation: add_punctuation).build(titles)
end

Instance Method Details

#build(titles) ⇒ String

Returns the title value for Solr.

Parameters:

  • [Array<Cocina::Models::Title>] ([Array<Cocina::Models::Title>] titles the titles to consider)

    titles the titles to consider

Returns:

  • (String)

    the title value for Solr



34
35
36
37
38
39
40
41
42
43
# File 'lib/cocina/models/builders/title_builder.rb', line 34

def build(titles)
  cocina_title = primary_title(titles) || untyped_title(titles)
  cocina_title = other_title(titles) if cocina_title.blank?

  if strategy == :first
    extract_title(cocina_title)
  else
    cocina_title.map { |one| extract_title(one) }
  end
end