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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strategy:, add_punctuation:) ⇒ TitleBuilder

Returns a new instance of TitleBuilder.



51
52
53
54
# File 'lib/cocina/models/builders/title_builder.rb', line 51

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

Class Method Details

.additional_titles(titles) ⇒ Array<String>

“additional titles” are all title data except for full_title. We want to able able to index it separately so

we can boost matches on it in search results (boost matching these strings lower than other titles present)

Parameters:

Returns:

  • (Array<String>)

    the values for Solr



47
48
49
# File 'lib/cocina/models/builders/title_builder.rb', line 47

def self.additional_titles(titles)
  new(strategy: :all, add_punctuation: false).build(titles) - [full_title(titles)]
end

.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



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

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

.full_title(titles) ⇒ String

the “full title” is the title WITH subtitle, part name, etc. We want to able able to index it separately so

we can boost matches on it in search results (boost matching this string higher than other titles present)

Parameters:

Returns:

  • (String)

    the title value for Solr



39
40
41
# File 'lib/cocina/models/builders/title_builder.rb', line 39

def self.full_title(titles)
  new(strategy: :first, add_punctuation: false).build(titles)
end

.main_title(titles) ⇒ String

the “main title” is the title withOUT subtitle, part name, etc. We want to index it separately so

we can boost matches on it in search results (boost matching this string higher than matching full title string)
e.g. "The Hobbit" (main_title) vs "The Hobbit, or, There and Back Again (full_title)

Parameters:

Returns:

  • (String)

    the main title value for Solr



31
32
33
# File 'lib/cocina/models/builders/title_builder.rb', line 31

def self.main_title(titles)
  new(strategy: :first, add_punctuation: false).main_title(titles)
end

Instance Method Details

#build(cocina_titles) ⇒ String

Returns the title value for Solr.

Parameters:

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

    cocina_titles the titles to consider

Returns:

  • (String)

    the title value for Solr



58
59
60
61
62
63
64
65
66
67
# File 'lib/cocina/models/builders/title_builder.rb', line 58

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

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

#main_title(titles) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/cocina/models/builders/title_builder.rb', line 69

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

  cocina_title = cocina_title.first if cocina_title.is_a?(Array)
  extract_main_title(cocina_title)
end