class NewspaperWorks::Data::WorkFile

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

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

Attributes

fileset[RW]

accessors for adaptation relationships:

parent[RW]

accessors for adaptation relationships:

work[RW]

accessors for adaptation relationships:

Public Class Methods

new(work, fileset = nil, parent = nil) click to toggle source
# File lib/newspaper_works/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
of(work, fileset = nil, parent = nil) click to toggle source

alternate constructor spelling:

# File lib/newspaper_works/data/work_file.rb, line 17
def self.of(work, fileset = nil, parent = nil)
  new(work, fileset, parent)
end

Public Instance Methods

==(other) click to toggle source
# File lib/newspaper_works/data/work_file.rb, line 37
def ==(other)
  return false if @fileset.nil?
  unwrapped.id == other.unwrapped.id
end
data() click to toggle source

Read data from working copy of file on local filesystem;

checkout file from repository/source as needed.

@return [String] byte data of binary/file payload

# File lib/newspaper_works/data/work_file.rb, line 53
def data
  return '' if @fileset.nil?
  File.read(path, mode: 'rb')
end
derivatives() click to toggle source

Derivatives for fileset associated with this primary file object @return [NewspaperWorks::Data::WorkDerviatives] derivatives adapter

# File lib/newspaper_works/data/work_file.rb, line 76
def derivatives
  NewspaperWorks::Data::WorkDerivatives.of(work, fileset, self)
end
name() click to toggle source

Get filename from stored metadata @return [String] file name stored in repository metadata for file

# File lib/newspaper_works/data/work_file.rb, line 69
def name
  return nil if @fileset.nil?
  unwrapped.original_name
end
path() click to toggle source

Get path to working copy of file on local filesystem;

checkout file from repository/source as needed.

@return [String] path to working copy of binary

# File lib/newspaper_works/data/work_file.rb, line 45
def path
  return nil if @fileset.nil?
  checkout
end
unwrapped() click to toggle source

Get original repository object representing file (not fileset). @return [ActiveFedora::File] repository file persistence object

# File lib/newspaper_works/data/work_file.rb, line 32
def unwrapped
  return nil if @fileset.nil?
  @fileset.original_file
end
with_io(&block) click to toggle source

Run block/proc upon data of file;

checkout file from repository/source as needed.

@yield [io] read-only IO or File object to block/proc.

# File lib/newspaper_works/data/work_file.rb, line 61
def with_io(&block)
  filepath = path
  return if filepath.nil?
  File.open(filepath, 'rb', &block)
end

Private Instance Methods

checkout() click to toggle source
# File lib/newspaper_works/data/work_file.rb, line 82
def checkout
  file = @fileset.original_file
  # find_or_retrieve returns path to working copy, but only
  #   fetches from Fedora if no working copy exists on filesystem.
  # NOTE: there may be some benefit to memoizing to avoid
  #   call and File.exist? IO operation, but YAGNI for now.
  Hyrax::WorkingDirectory.find_or_retrieve(file.id, @fileset.id)
end