Class: CocinaDisplay::Titles::Title

Inherits:
Object
  • Object
show all
Defined in:
lib/cocina_display/titles/title.rb

Overview

A Title represented by one or more TitleValues in various languages/scripts.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cocina, part_label: nil, part_numbers: nil) ⇒ Title

Create a new Title object.

Parameters:

  • cocina (Hash)
  • part_label (String, nil) (defaults to: nil)

    part label for digital serials

  • part_numbers (Array<String>) (defaults to: nil)

    part numbers for related resources



20
21
22
23
24
# File 'lib/cocina_display/titles/title.rb', line 20

def initialize(cocina, part_label: nil, part_numbers: nil)
  @cocina = cocina
  @part_label = part_label
  @part_numbers = part_numbers
end

Instance Attribute Details

#cocinaObject (readonly)

The underlying Cocina hash.



6
7
8
# File 'lib/cocina_display/titles/title.rb', line 6

def cocina
  @cocina
end

#statusString?

Status of the title, e.g. “primary”.

Returns:

  • (String, nil)


10
11
12
# File 'lib/cocina_display/titles/title.rb', line 10

def status
  @status
end

Instance Method Details

#has_translation?Boolean

Is there a translated version of the title?

Returns:

  • (Boolean)


78
79
80
# File 'lib/cocina_display/titles/title.rb', line 78

def has_translation?
  translated_value.present?
end

#has_transliteration?Boolean

Is there a transliterated version of the title?

Returns:

  • (Boolean)


90
91
92
# File 'lib/cocina_display/titles/title.rb', line 90

def has_transliteration?
  transliterated_value.present?
end

#labelString

Label used when displaying the title.

Returns:

  • (String)


28
29
30
# File 'lib/cocina_display/titles/title.rb', line 28

def label
  cocina["displayLabel"].presence || type_label
end

#main_valueTitleValue

The main value of the title, ignoring parallel values if any. If no value is marked as parallel, returns the first value.

Returns:



60
61
62
# File 'lib/cocina_display/titles/title.rb', line 60

def main_value
  title_values.find(&:main_value?) || title_values.first
end

#parallel_valuesArray<TitleValue>

All values except the main value.

Returns:



66
67
68
# File 'lib/cocina_display/titles/title.rb', line 66

def parallel_values
  title_values.reject { |value| value.to_s == main_value.to_s }
end

#primary?Boolean

Is this marked as a primary title?

Returns:

  • (Boolean)


47
48
49
# File 'lib/cocina_display/titles/title.rb', line 47

def primary?
  status == "primary"
end

#title_valuesArray<TitleValue>

Individual values in different languages/scripts composing this title.

Returns:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cocina_display/titles/title.rb', line 96

def title_values
  @title_values ||= begin
    # Create TitleValue objects for all parallelValue nodes or just value if none
    values = (Array(cocina["parallelValue"]).presence || [cocina]).map do |node|
      TitleValue.new(node, part_label: @part_label, part_numbers: @part_numbers)
    end

    # If there's only one value, we're done
    return values if values.one?

    # Set the type of the parallel values to either their sibling main value's type
    # or the parent title's type.
    main_type = values.find(&:main_value?)&.type || cocina["type"].presence
    values.each { |value| value.type ||= main_type }
    values
  end
end

#to_sString?

The string representation of the title, for display.

Returns:

  • (String, nil)


53
54
55
# File 'lib/cocina_display/titles/title.rb', line 53

def to_s
  display_title
end

#translated_valueTitleValue?

The translated version of the title, if any.

Returns:



72
73
74
# File 'lib/cocina_display/titles/title.rb', line 72

def translated_value
  title_values.find(&:translated?)
end

#transliterated_valueTitleValue?

The transliterated version of the title, if any.

Returns:



84
85
86
# File 'lib/cocina_display/titles/title.rb', line 84

def transliterated_value
  title_values.find(&:transliterated?)
end

#typeString?

Type of the title, e.g. “uniform”, “alternative”, etc.



35
36
37
# File 'lib/cocina_display/titles/title.rb', line 35

def type
  cocina["type"].presence || main_value.type
end

#type?Boolean

Does this title have a type?

Returns:

  • (Boolean)


41
42
43
# File 'lib/cocina_display/titles/title.rb', line 41

def type?
  type.present?
end