Module: CocinaDisplay::Concerns::Structural

Included in:
CocinaDisplay::CocinaRecord
Defined in:
lib/cocina_display/concerns/structural.rb

Overview

Methods for inspecting structural metadata (e.g. file hierarchy)

Instance Method Summary collapse

Instance Method Details

#containing_collectionsArray<String>

DRUIDs of collections this object is a member of.

Examples:

“sj775xm6965”

Returns:

  • (Array<String>)


93
94
95
# File 'lib/cocina_display/concerns/structural.rb', line 93

def containing_collections
  path("$.structural.isMemberOf.*").map { |druid| druid.delete_prefix("druid:") }
end

#file_mime_typesArray<String>

All unique MIME types of files in this object.

Examples:

“image/jpeg”, “application/pdf”

Returns:

  • (Array<String>)


47
48
49
# File 'lib/cocina_display/concerns/structural.rb', line 47

def file_mime_types
  files.map(&:mime_type).compact.uniq
end

#files(filename: nil, mime_type: nil, use: nil) ⇒ Array<CocinaDisplay::Structural::File>

Structured data for all individual files in the object. Traverses nested FileSet structure to return a flattened array. Optionally filter by filename or MIME type regex, or by role. Filters are combined with AND logic (all must match).

Examples:

record.files.each do |file|
 puts file.filename #=> "image1.jpg"
 puts file.size #=> 123456
end

Filter to only thumbnail JP2 files

record.files(filename: /\.jp2$/, use: "thumbnail")

Parameters:

  • filename (String, nil) (defaults to: nil)

    Regular expression to match against filenames.

  • mime_type (String, nil) (defaults to: nil)

    Regular expression to match against MIME types.

  • use (String, nil) (defaults to: nil)

    Usage type to match against (e.g., “thumbnail”).

Returns:



36
37
38
39
40
41
42
# File 'lib/cocina_display/concerns/structural.rb', line 36

def files(filename: nil, mime_type: nil, use: nil)
  results = filesets.flat_map(&:files)
  results.filter! { |file| file.filename =~ filename } if filename
  results.filter! { |file| file.mime_type =~ mime_type } if mime_type
  results.filter! { |file| file.use == use } if use
  results
end

#fileset_typesArray<String>

All unique types of filesets in this object.

Examples:

“image”, “document”

Returns:

  • (Array<String>)


54
55
56
# File 'lib/cocina_display/concerns/structural.rb', line 54

def fileset_types
  filesets.map(&:type).compact.uniq
end

#filesetsArray<CocinaDisplay::Structural::FileSet>

Structured data for all file sets in the object. Each fileset contains one or more files.

Examples:

record.filesets.each do |fileset|
 puts fileset.type #=> "image"
 puts fileset.label #=> "High Resolution Images"
end

Returns:



15
16
17
18
19
# File 'lib/cocina_display/concerns/structural.rb', line 15

def filesets
  @filesets ||= path("$.structural.contains.*").map do |fileset|
    CocinaDisplay::Structural::FileSet.new(fileset, druid: bare_druid, base_url: stacks_base_url)
  end
end

#thumbnail?Boolean

Note:

Does not attempt to crawl virtual object members for thumbnails.

True if the object has a usable thumbnail file.

Returns:

  • (Boolean)


86
87
88
# File 'lib/cocina_display/concerns/structural.rb', line 86

def thumbnail?
  thumbnail_file.present?
end

#thumbnail_fileCocinaDisplay::Structural::File?

The thumbnail file for this object, if any. Prefers files marked as thumbnails; falls back to any JP2 image.



125
126
127
# File 'lib/cocina_display/concerns/structural.rb', line 125

def thumbnail_file
  files.find(&:thumbnail?) || files.find(&:jp2_image?)
end

#thumbnail_url(region: "full", width: "!400", height: "400") ⇒ String?

Note:

Uses the IIIF image server to generate an image of the given size.

URL to a thumbnail image for this object, if any.

Examples:

Parameters:

  • region (String) (defaults to: "full")

    Desired region of the image (e.g., “full”, “square”, “x,y,w,h”, “pct:x,y,w,h”).

  • width (String) (defaults to: "!400")

    Desired width of the image in pixels (use “!” prefix to preserve aspect ratio).

  • height (String) (defaults to: "400")

    Desired height of the image in pixels.

Returns:

  • (String, nil)


79
80
81
# File 'lib/cocina_display/concerns/structural.rb', line 79

def thumbnail_url(region: "full", width: "!400", height: "400")
  thumbnail_file&.iiif_url(region: region, width: width, height: height)
end

#total_file_size_intInteger

Summed size of all files in bytes.

Examples:

2621440

Returns:

  • (Integer)


68
69
70
# File 'lib/cocina_display/concerns/structural.rb', line 68

def total_file_size_int
  files.map(&:size).compact.sum
end

#total_file_size_strString

Human-readable string representation of #total_file_size_int.

Examples:

“2.5 MB”

Returns:

  • (String)


61
62
63
# File 'lib/cocina_display/concerns/structural.rb', line 61

def total_file_size_str
  ActiveSupport::NumberHelper.number_to_human_size(total_file_size_int)
end

#virtual_object?Boolean

Whether this object is a virtual object.

Returns:

  • (Boolean)


99
100
101
102
103
# File 'lib/cocina_display/concerns/structural.rb', line 99

def virtual_object?
  return false if filesets.any?

  path("$.structural.hasMemberOrders.*.members.*").any?
end

#virtual_object_membersArray<String>

DRUIDs of members of this virtual object.

Examples:

“ts786ny5936”, “tp006ms8736”, “tj297ys4758”

Returns:

  • (Array<String>)


108
109
110
111
112
# File 'lib/cocina_display/concerns/structural.rb', line 108

def virtual_object_members
  return [] unless virtual_object?

  path("$.structural.hasMemberOrders.*.members.*").map { |druid| druid.delete_prefix("druid:") }
end

#virtual_object_parentsArray<String>

DRUIDs of virtual objects this object is a part of. NOTE: not all virtual objects have this relationship in the description.related_resources.

Examples:

“hj097bm8879”

Returns:

  • (Array<String>)


118
119
120
# File 'lib/cocina_display/concerns/structural.rb', line 118

def virtual_object_parents
  related_resources.filter_map { |res| res.purl_url&.split("/")&.last if res.type == "part of" }.compact_blank
end