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.
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) ⇒ 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) ⇒ Form
Create a Form object from Cocina structured data.
27 28 29 |
# File 'lib/cocina_display/forms/form.rb', line 27 def initialize(cocina) @cocina = cocina 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 |
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.
39 40 41 |
# File 'lib/cocina_display/forms/form.rb', line 39 def flat_value Utils.compact_and_join(values, delimiter: " > ") end |
#label ⇒ String
The label to use for display. Uses a displayLabel if available, otherwise looks up via type.
52 53 54 |
# File 'lib/cocina_display/forms/form.rb', line 52 def label cocina["displayLabel"].presence || type_label end |
#to_s ⇒ String
The value to use for display.
33 34 35 |
# File 'lib/cocina_display/forms/form.rb', line 33 def to_s flat_value end |
#type ⇒ String?
The type of form, such as “genre”, “extent”, etc.
58 59 60 |
# File 'lib/cocina_display/forms/form.rb', line 58 def type cocina["type"] end |
#values ⇒ String
The raw values from the Cocina data, flattened if nested.
45 46 47 |
# File 'lib/cocina_display/forms/form.rb', line 45 def values Utils.flatten_nested_values(cocina).pluck("value").compact_blank end |