Class: CocinaDisplay::DisplayData

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

Overview

A data structure to be rendered into HTML by the consumer.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, objects:) ⇒ DisplayData

Create a DisplayData object from a list of objects that share a label

Parameters:

  • label (String)
  • objects (Array<#to_s>)


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

#labelObject (readonly)

Returns the value of attribute label.



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

def label
  @label
end

#objectsObject (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.

Parameters:

  • strings (Array<String>)

    The string values to display

  • label (String) (defaults to: nil)

    The label for the display data

Returns:

  • (Array<DescriptiveValue>)

    The descriptive values



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.

Parameters:

  • cocina (Array<Hash>)
  • label (String) (defaults to: nil)

Returns:



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.

Parameters:

  • objects (Array<Object>)

Returns:



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.

Parameters:

  • value (String)

    The string values to display

  • label (String) (defaults to: nil)

    The label for the display data

Returns:



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.

Parameters:

Returns:

  • (Hash{String => Array<String>})

    The merged hash



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_hHash{String => Array<String>}

Express the display data as a hash mapping the label to its values.

Returns:

  • (Hash{String => Array<String>})

    The label and values



91
92
93
# File 'lib/cocina_display/display_data.rb', line 91

def to_h
  {label => values}
end

#valuesArray<String>

The unique, non-blank values for display

Returns:

  • (Array<String>)


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