Class: CocinaDisplay::DisplayData
- Inherits:
-
Object
- Object
- CocinaDisplay::DisplayData
- Defined in:
- lib/cocina_display/display_data.rb
Overview
A data structure to be rendered into HTML by the consumer.
Instance Attribute Summary collapse
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#objects ⇒ Object
readonly
Returns the value of attribute objects.
Class Method Summary collapse
-
.descriptive_values_from_strings(strings, label: nil) ⇒ Array<DescriptiveValue>
Create an array containing a descriptive object from string values.
-
.from_cocina(cocina, label: nil) ⇒ Array<DisplayData>
Given an array of Cocina hashes, group them into DisplayData.
-
.from_objects(objects) ⇒ Array<DisplayData>
Given objects that support #to_s and #label, group them into DisplayData.
-
.from_strings(values, label: nil) ⇒ Array<DisplayData>
Create display data from string values.
-
.to_hash(display_data) ⇒ Hash{String => Array<String>}
Take one or several DisplayData and merge into a single hash.
Instance Method Summary collapse
-
#initialize(label:, objects:) ⇒ DisplayData
constructor
Create a DisplayData object from a list of objects that share a label.
-
#to_h ⇒ Hash{String => Array<String>}
Express the display data as a hash mapping the label to its values.
-
#values ⇒ Array<String>
The unique, non-blank values for display.
Constructor Details
#initialize(label:, objects:) ⇒ DisplayData
Create a DisplayData object from a list of objects that share a label
76 77 78 79 |
# File 'lib/cocina_display/display_data.rb', line 76 def initialize(label:, objects:) @label = label @objects = objects end |
Instance Attribute Details
#label ⇒ Object (readonly)
Returns the value of attribute label.
81 82 83 |
# File 'lib/cocina_display/display_data.rb', line 81 def label @label end |
#objects ⇒ Object (readonly)
Returns the value of attribute objects.
81 82 83 |
# File 'lib/cocina_display/display_data.rb', line 81 def objects @objects end |
Class Method Details
.descriptive_values_from_strings(strings, label: nil) ⇒ Array<DescriptiveValue>
Create an array containing a descriptive object from string values. Can be used to combine a string derived value with other metadata objects.
40 41 42 |
# File 'lib/cocina_display/display_data.rb', line 40 def descriptive_values_from_strings(strings, label: nil) strings.map { |string| DescriptiveValue.new(label: label, value: string) } end |
.from_cocina(cocina, label: nil) ⇒ Array<DisplayData>
Given an array of Cocina hashes, group them into CocinaDisplay::DisplayData. Uses label
as the label if provided, but honors displayLabel
if set. Keeps the unique, non-blank values under each label.
23 24 25 |
# File 'lib/cocina_display/display_data.rb', line 23 def from_cocina(cocina, label: nil) from_objects(descriptive_values_from_cocina(cocina, label: label)) end |
.from_objects(objects) ⇒ Array<DisplayData>
Given objects that support #to_s and #label, group them into CocinaDisplay::DisplayData. Groups by each object’s label
and keeps unique, non-blank values.
11 12 13 14 15 |
# File 'lib/cocina_display/display_data.rb', line 11 def from_objects(objects) objects.group_by(&:label) .map { |label, objs| new(label: label, objects: objs) } .reject { |data| data.values.empty? } end |
.from_strings(values, label: nil) ⇒ Array<DisplayData>
Create display data from string values.
31 32 33 |
# File 'lib/cocina_display/display_data.rb', line 31 def from_strings(values, label: nil) from_objects(descriptive_values_from_strings(values, label: label)) end |
.to_hash(display_data) ⇒ Hash{String => Array<String>}
Take one or several DisplayData and merge into a single hash. Keys are labels; values are the merged array of values for that label.
48 49 50 |
# File 'lib/cocina_display/display_data.rb', line 48 def to_hash(display_data) Array(display_data).map(&:to_h).reduce(:merge) end |
Instance Method Details
#to_h ⇒ Hash{String => Array<String>}
Express the display data as a hash mapping the label to its values.
91 92 93 |
# File 'lib/cocina_display/display_data.rb', line 91 def to_h {label => values} end |
#values ⇒ Array<String>
The unique, non-blank values for display
85 86 87 |
# File 'lib/cocina_display/display_data.rb', line 85 def values objects.flat_map { |object| split_string_on_newlines(object.to_s) }.compact_blank.uniq end |