Class: CocinaDisplay::Forms::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/cocina_display/forms/form.rb

Overview

A form associated with part or all of a Cocina object.

Direct Known Subclasses

Genre, ResourceType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cocina) ⇒ Form

Create a Form object from Cocina structured data.

Parameters:

  • cocina (Hash)


27
28
29
# File 'lib/cocina_display/forms/form.rb', line 27

def initialize(cocina)
  @cocina = cocina
end

Instance Attribute Details

#cocinaObject (readonly)

Returns the value of attribute cocina.



8
9
10
# File 'lib/cocina_display/forms/form.rb', line 8

def cocina
  @cocina
end

Class Method Details

.from_cocina(cocina) ⇒ Form

Create a Form object from Cocina structured data. Delegates to subclasses for specific types.

Parameters:

  • cocina (Hash)

Returns:



14
15
16
17
18
19
20
21
22
23
# File 'lib/cocina_display/forms/form.rb', line 14

def self.from_cocina(cocina)
  case cocina["type"]
  when "genre"
    Genre.new(cocina)
  when "resource type"
    ResourceType.new(cocina)
  else
    new(cocina)
  end
end

Instance Method Details

#flat_valueString

Single concatenated string value for the form.

Returns:

  • (String)


39
40
41
# File 'lib/cocina_display/forms/form.rb', line 39

def flat_value
  Utils.compact_and_join(values, delimiter: " > ")
end

#labelString

The label to use for display. Uses a displayLabel if available, otherwise looks up via type.

Returns:

  • (String)


52
53
54
# File 'lib/cocina_display/forms/form.rb', line 52

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

#to_sString

The value to use for display.

Returns:

  • (String)


33
34
35
# File 'lib/cocina_display/forms/form.rb', line 33

def to_s
  flat_value
end

#typeString?

The type of form, such as “genre”, “extent”, etc.

Returns:

  • (String, nil)


58
59
60
# File 'lib/cocina_display/forms/form.rb', line 58

def type
  cocina["type"]
end

#valuesString

The raw values from the Cocina data, flattened if nested.

Returns:

  • (String)


45
46
47
# File 'lib/cocina_display/forms/form.rb', line 45

def values
  Utils.flatten_nested_values(cocina).pluck("value").compact_blank
end