Class: IiifPrint::Data::WorkFile

Inherits:
Object
  • Object
show all
Defined in:
lib/iiif_print/data/work_file.rb

Overview

WorkFile is a read-only convenience wrapper for just-in-time

file operations, and is the type of values returned by
IiifPrint::Data::WorkFiles (container) adapter.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(work, fileset = nil, parent = nil) ⇒ WorkFile

Returns a new instance of WorkFile.



21
22
23
24
25
26
27
28
# File 'lib/iiif_print/data/work_file.rb', line 21

def initialize(work, fileset = nil, parent = nil)
  @work = work
  # If fileset is nil, presume *first* fileset of work, as in
  #   the single-file-per-work use-case:
  @fileset = fileset
  # Parent is WorkFiles (container) object, if applciable:
  @parent = parent
end

Instance Attribute Details

#filesetObject

accessors for adaptation relationships:



12
13
14
# File 'lib/iiif_print/data/work_file.rb', line 12

def fileset
  @fileset
end

#parentObject

accessors for adaptation relationships:



12
13
14
# File 'lib/iiif_print/data/work_file.rb', line 12

def parent
  @parent
end

#workObject

accessors for adaptation relationships:



12
13
14
# File 'lib/iiif_print/data/work_file.rb', line 12

def work
  @work
end

Class Method Details

.of(work, fileset = nil, parent = nil) ⇒ Object

alternate constructor spelling:



17
18
19
# File 'lib/iiif_print/data/work_file.rb', line 17

def self.of(work, fileset = nil, parent = nil)
  new(work, fileset, parent)
end

Instance Method Details

#==(other) ⇒ Object



37
38
39
40
# File 'lib/iiif_print/data/work_file.rb', line 37

def ==(other)
  return false if @fileset.nil?
  unwrapped.id == other.unwrapped.id
end

#dataString

Read data from working copy of file on local filesystem;

checkout file from repository/source as needed.

Returns:

  • (String)

    byte data of binary/file payload



53
54
55
56
# File 'lib/iiif_print/data/work_file.rb', line 53

def data
  return '' if @fileset.nil?
  File.read(path, mode: 'rb')
end

#derivativesIiifPrint::Data::WorkDerviatives

Derivatives for fileset associated with this primary file object

Returns:

  • (IiifPrint::Data::WorkDerviatives)

    derivatives adapter



76
77
78
# File 'lib/iiif_print/data/work_file.rb', line 76

def derivatives
  IiifPrint::Data::WorkDerivatives.of(work, fileset, self)
end

#nameString

Get filename from stored metadata

Returns:

  • (String)

    file name stored in repository metadata for file



69
70
71
72
# File 'lib/iiif_print/data/work_file.rb', line 69

def name
  return nil if @fileset.nil?
  unwrapped.original_name
end

#pathString

Get path to working copy of file on local filesystem;

checkout file from repository/source as needed.

Returns:

  • (String)

    path to working copy of binary



45
46
47
48
# File 'lib/iiif_print/data/work_file.rb', line 45

def path
  return nil if @fileset.nil?
  checkout
end

#unwrappedActiveFedora::File

Get original repository object representing file (not fileset).

Returns:

  • (ActiveFedora::File)

    repository file persistence object



32
33
34
35
# File 'lib/iiif_print/data/work_file.rb', line 32

def unwrapped
  return nil if @fileset.nil?
  @fileset.original_file
end

#with_io {|io| ... } ⇒ Object

Run block/proc upon data of file;

checkout file from repository/source as needed.

Yields:

  • (io)

    read-only IO or File object to block/proc.



61
62
63
64
65
# File 'lib/iiif_print/data/work_file.rb', line 61

def with_io(&block)
  filepath = path
  return if filepath.nil?
  File.open(filepath, 'rb', &block)
end