Class: IiifPrint::Data::WorkFiles

Inherits:
Object
  • Object
show all
Includes:
PathHelper
Defined in:
lib/iiif_print/data/work_files.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PathHelper

#isuri?, #normalize_path, #path_to_uri, #registered_ingest_path, #validate_path

Constructor Details

#initialize(work) ⇒ WorkFiles

Returns a new instance of WorkFiles.



32
33
34
35
36
37
# File 'lib/iiif_print/data/work_files.rb', line 32

def initialize(work)
  @work = work
  @assigned = []
  @unassigned = []
  @derivatives = nil
end

Instance Attribute Details

#assignedObject

Returns the value of attribute assigned.



6
7
8
# File 'lib/iiif_print/data/work_files.rb', line 6

def assigned
  @assigned
end

#unassignedObject

Returns the value of attribute unassigned.



6
7
8
# File 'lib/iiif_print/data/work_files.rb', line 6

def unassigned
  @unassigned
end

#workObject

Returns the value of attribute work.



6
7
8
# File 'lib/iiif_print/data/work_files.rb', line 6

def work
  @work
end

Class Method Details

.assign!(to:, path:, derivative_paths: [], commit: true) ⇒ Object

A convenience method to associate files (original and derivatives) to the given work.

Parameters:

  • to (Object)

    the work to which we’re assigning the file(s) for the given paths.

  • path (String)

    the path of the file we’re assignging to the given work.

  • derivative_paths (Array<String>) (defaults to: [])

    the path(s) to derivatives we’ll assign to the given work.

  • commit (Boolean) (defaults to: true)

    when true, commit the changes to the attachment.

Returns:

  • void



23
24
25
26
27
28
29
30
# File 'lib/iiif_print/data/work_files.rb', line 23

def self.assign!(to:, path:, derivative_paths: [], commit: true)
  attachment = new(to)
  attachment.assign(path)
  Array.wrap(derivative_paths).each do |derivative_path|
    attachment.derivatives.assign(derivative_path)
  end
  attachment.commit! if commit
end

.of(work) ⇒ Object

alternate constructor spelling:



10
11
12
# File 'lib/iiif_print/data/work_files.rb', line 10

def self.of(work)
  new(work)
end

Instance Method Details

#assign(path) ⇒ Object

Assign a path to assigned queue for attachment

Parameters:

  • path (String)

    Path to source file



110
111
112
113
114
# File 'lib/iiif_print/data/work_files.rb', line 110

def assign(path)
  path = normalize_path(path)
  validate_path(path)
  @assigned.push(path)
end

#commit!Object

commit pending changes to work files

beginning with removals, then with new assignments


128
129
130
131
# File 'lib/iiif_print/data/work_files.rb', line 128

def commit!
  commit_unassigned
  commit_assigned
end

#derivatives(fileset: nil) ⇒ IiifPrint::Data::WorkDerviatives

Derivatives for specified fileset or first fileset found.

The `WorkDerivatives` adapter as assign/commmit! semantics just
like `WorkFiles`, and also acts like a hash/mapping of
destination names (usually file extension) to path of saved
derviative.  Always returns same instance (memoized after first
use) of `WorkDerivatives`.

Returns:

  • (IiifPrint::Data::WorkDerviatives)

    derivatives adapter



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/iiif_print/data/work_files.rb', line 46

def derivatives(fileset: nil)
  fileset ||= @fileset
  return @derivatives unless @derivatives.nil?
  if fileset.nil?
    # for the deferred assignement case, we have no fileset yet...
    work_file = IiifPrint::Data::WorkFile.of(work, nil, self)
    return work_file.derivatives
  end
  # Otherwise, delegate actual construction to WorkFile.derivatives:
  @derivatives = values[0].derivatives
end

#entriesArray<Array>

Array of [id, WorkFile] for each primary file

Returns:

  • (Array<Array>)

    key/value pairs for primary files of work



88
89
90
# File 'lib/iiif_print/data/work_files.rb', line 88

def entries
  filesets.map { |fs| [fs.id, self[fs.id]] }
end

#get(name_or_id) ⇒ IiifPrint::Data::WorkFile Also known as: []

Get a WorkFile adapter representing primary file, either by name or id

Parameters:

  • name_or_id (String)

    Fileset id or work-local file name

Returns:



103
104
105
106
# File 'lib/iiif_print/data/work_files.rb', line 103

def get(name_or_id)
  return get_by_fileset_id(name_or_id) if keys.include?(name_or_id)
  get_by_filename(name_or_id)
end

#keysString

List of fileset (not file) id keys, presumes system like Hyrax

is only keeping a 1:1 between fileset and contained PCDM file,
because derivatives are not stored in the FileSet.

Returns:

  • (String)

    fileset ids



75
76
77
# File 'lib/iiif_print/data/work_files.rb', line 75

def keys
  filesets.map(&:id)
end

#namesArray<String>

List of local file names for attachments, based on original ingested

or uploaded file name.

Returns:

  • (Array<String>)


95
96
97
# File 'lib/iiif_print/data/work_files.rb', line 95

def names
  filesets.map(&method(:original_name))
end

#stateString

Assignment state

Returns:

  • (String)

    A label describing the state of assignment queues



60
61
62
63
64
65
66
67
68
69
# File 'lib/iiif_print/data/work_files.rb', line 60

def state
  return 'dirty' unless @assigned.empty? && @unassigned.empty?
  return 'empty' if keys.empty?
  # TODO: implement 'pending' as intermediate state between 'dirty'
  #   and saved, where we look for saved state that matches what was
  #   previously assigned in THIS instance.  We can only know that
  #   changes initiated by this instance in this thread are pending
  #   because there's no global storage for the assignment queue.
  'saved'
end

#unassign(name_or_id) ⇒ Object

Assign a name or id to unassigned queue for deletion – OR – remove a

path from queue of assigned items

Parameters:

  • name_or_id (String)

    Fileset id, local file name, or source path



119
120
121
122
123
124
# File 'lib/iiif_print/data/work_files.rb', line 119

def unassign(name_or_id)
  # if name_or_id is queued path, remove from @assigned queue:
  @assigned.delete(name_or_id) if @assigned.include?(name_or_id)
  # if name_or_id is known id or name, remove
  @unassigned.push(name_or_id) if include?(name_or_id)
end

#valuesArray<IiifPrint::Data::WorkFile>

List of WorkFile for each primary file

Returns:



82
83
84
# File 'lib/iiif_print/data/work_files.rb', line 82

def values
  keys.map(&method(:get))
end