Class: CocinaDisplay::Parallel::ParallelValue

Inherits:
Object
  • Object
show all
Defined in:
lib/cocina_display/parallel/parallel_value.rb

Overview

A base class for values representing one of several siblings describing the same thing in different languages or scripts.

Constant Summary collapse

PARALLEL_TYPES =

Value types (in Cocina) that are used to indicate sibling relationships.

["parallel", "translated", "translation", "transliterated", "transliteration", "display"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cocina, parent:) ⇒ ParallelValue

Create a new ParallelValue object and set the appropriate role and type.

Parameters:

  • cocina (Hash)
  • parent (Object)

    the parent object, used for inheritance



28
29
30
31
32
# File 'lib/cocina_display/parallel/parallel_value.rb', line 28

def initialize(cocina, parent:)
  @cocina = cocina
  @parent = parent
  @role = PARALLEL_TYPES.find { |role| cocina["type"] == role }
end

Instance Attribute Details

#cocinaHash (readonly)

The underlying Cocina hash.

Returns:

  • (Hash)


11
12
13
# File 'lib/cocina_display/parallel/parallel_value.rb', line 11

def cocina
  @cocina
end

#parentObject (readonly)

The parent object that contains this value and its potential siblings.

Returns:

  • (Object)


15
16
17
# File 'lib/cocina_display/parallel/parallel_value.rb', line 15

def parent
  @parent
end

#roleString? (readonly)

What relationship does this value have to its siblings?

Returns:

  • (String, nil)


19
20
21
# File 'lib/cocina_display/parallel/parallel_value.rb', line 19

def role
  @role
end

Instance Method Details

#display?Boolean

Is this value explicitly intended for display?

Returns:

  • (Boolean)


42
43
44
# File 'lib/cocina_display/parallel/parallel_value.rb', line 42

def display?
  role == "display"
end

#languageCocinaDisplay::Languages::Language?

The language of the value, if specified.



107
108
109
# File 'lib/cocina_display/parallel/parallel_value.rb', line 107

def language
  @language ||= CocinaDisplay::Languages::Language.new(cocina["valueLanguage"]) if cocina["valueLanguage"].present?
end

#main_valueCocinaDisplay::ParallelValue

The main value among the siblings, which could be this value.

Returns:

  • (CocinaDisplay::ParallelValue)


23
# File 'lib/cocina_display/parallel/parallel_value.rb', line 23

delegate :main_value, to: :parent

#own_typeString?

Note:

PARALLEL_TYPES are types in Cocina, but we treat them as “roles”.

The type of this object in the Cocina, unless it’s one of PARALLEL_TYPES.

Returns:

  • (String, nil)


75
76
77
# File 'lib/cocina_display/parallel/parallel_value.rb', line 75

def own_type
  cocina["type"] unless PARALLEL_TYPES.include?(cocina["type"])
end

#own_typed?Boolean

Note:

PARALLEL_TYPES are ignored since they indicate role, not type.

Does this value have a type in the Cocina?

Returns:

  • (Boolean)


88
89
90
# File 'lib/cocina_display/parallel/parallel_value.rb', line 88

def own_typed?
  own_type.present?
end

#primary?Boolean

Is this value marked as primary?

Returns:

  • (Boolean)


101
102
103
# File 'lib/cocina_display/parallel/parallel_value.rb', line 101

def primary?
  status == "primary"
end

#siblingsArray<CocinaDisplay::ParallelValue>

Sibling values with the same parent.

Returns:

  • (Array<CocinaDisplay::ParallelValue>)


36
37
38
# File 'lib/cocina_display/parallel/parallel_value.rb', line 36

def siblings
  parent.parallel_values.reject { |value| value == self }
end

#statusString?

Note:

We treat this the same as “role”.

The status of the value relative to siblings, if any (e.g., “primary”).

Returns:

  • (String, nil)


95
96
97
# File 'lib/cocina_display/parallel/parallel_value.rb', line 95

def status
  cocina["status"]
end

#translated?Boolean

Is this value translated? True if the role is “translated” or “translation”, or if there is a parallel vernacular value but this one is in English.

Returns:

  • (Boolean)


56
57
58
# File 'lib/cocina_display/parallel/parallel_value.rb', line 56

def translated?
  role == "translated" || role == "translation" || (siblings.any?(&:vernacular?) && language&.english?)
end

#transliterated?Boolean

Is this value transliterated?

Returns:

  • (Boolean)


62
63
64
# File 'lib/cocina_display/parallel/parallel_value.rb', line 62

def transliterated?
  role == "transliterated" || role == "transliteration" || language&.transliterated?
end

#typeString?

The type, which can be inherited from the main sibling or parent object.

Returns:

  • (String, nil)


68
69
70
# File 'lib/cocina_display/parallel/parallel_value.rb', line 68

def type
  own_type || main_value.own_type || parent.own_type
end

#typed?Boolean

Does this value have a type?

Returns:

  • (Boolean)


81
82
83
# File 'lib/cocina_display/parallel/parallel_value.rb', line 81

def typed?
  type.present?
end

#vernacular?Boolean

Is this value in a native (non-English) language?

Returns:

  • (Boolean)


48
49
50
# File 'lib/cocina_display/parallel/parallel_value.rb', line 48

def vernacular?
  language.present? && !language.english?
end