Class: CocinaDisplay::Forms::Form
- Inherits:
-
Object
- Object
- CocinaDisplay::Forms::Form
- Defined in:
- lib/cocina_display/forms/form.rb
Overview
A form associated with part or all of a Cocina object.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cocina ⇒ Object
readonly
Returns the value of attribute cocina.
-
#delimiter ⇒ Object
readonly
Returns the value of attribute delimiter.
Class Method Summary collapse
-
.from_cocina(cocina) ⇒ Form
Create a Form object from Cocina structured data.
Instance Method Summary collapse
-
#flat_value ⇒ String
Single concatenated string value for the form.
-
#initialize(cocina, delimiter: " > ") ⇒ Form
constructor
Create a Form object from Cocina structured data.
-
#label ⇒ String
The label to use for display.
-
#to_s ⇒ String
The value to use for display.
-
#type ⇒ String?
The type of form, such as “genre”, “extent”, etc.
-
#values ⇒ String
The raw values from the Cocina data, flattened if nested.
Constructor Details
#initialize(cocina, delimiter: " > ") ⇒ Form
Create a Form object from Cocina structured data.
28 29 30 31 |
# File 'lib/cocina_display/forms/form.rb', line 28 def initialize(cocina, delimiter: " > ") @cocina = cocina @delimiter = delimiter end |
Instance Attribute Details
#cocina ⇒ Object (readonly)
Returns the value of attribute cocina.
8 9 10 |
# File 'lib/cocina_display/forms/form.rb', line 8 def cocina @cocina end |
#delimiter ⇒ Object (readonly)
Returns the value of attribute delimiter.
8 9 10 |
# File 'lib/cocina_display/forms/form.rb', line 8 def delimiter @delimiter end |
Class Method Details
.from_cocina(cocina) ⇒ Form
Create a Form object from Cocina structured data. Delegates to subclasses for specific types.
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_value ⇒ String
Single concatenated string value for the form.
41 42 43 |
# File 'lib/cocina_display/forms/form.rb', line 41 def flat_value Utils.compact_and_join(values, delimiter: delimiter) end |
#label ⇒ String
The label to use for display. Uses a displayLabel if available, otherwise looks up via type.
54 55 56 |
# File 'lib/cocina_display/forms/form.rb', line 54 def label cocina["displayLabel"].presence || type_label end |
#to_s ⇒ String
The value to use for display.
35 36 37 |
# File 'lib/cocina_display/forms/form.rb', line 35 def to_s flat_value end |
#type ⇒ String?
The type of form, such as “genre”, “extent”, etc.
60 61 62 |
# File 'lib/cocina_display/forms/form.rb', line 60 def type cocina["type"] end |
#values ⇒ String
The raw values from the Cocina data, flattened if nested.
47 48 49 |
# File 'lib/cocina_display/forms/form.rb', line 47 def values Utils.flatten_nested_values(cocina).pluck("value").compact_blank end |