class Hydra::Works::AddExternalFileToFileSet::Updater

Attributes

current_file[R]
file_set[R]

Public Class Methods

new(file_set, type, update_existing) click to toggle source
# File lib/hydra/works/services/add_external_file_to_file_set.rb, line 30
def initialize(file_set, type, update_existing)
  @file_set = file_set
  @current_file = find_or_create_file(type, update_existing)
end

Public Instance Methods

update(external_file_url, filename = nil) click to toggle source

@param [#read] file object that will be interrogated using the methods: :path, :original_name, :original_filename, :mime_type, :content_type None of the attribute description methods are required.

# File lib/hydra/works/services/add_external_file_to_file_set.rb, line 37
def update(external_file_url, filename = nil)
  attach_attributes(external_file_url, filename)
  persist
end

Private Instance Methods

attach_attributes(external_file_url, filename = nil) click to toggle source
# File lib/hydra/works/services/add_external_file_to_file_set.rb, line 53
def attach_attributes(external_file_url, filename = nil)
  current_file.content = StringIO.new('')
  current_file.original_name = filename
  current_file.mime_type = "message/external-body; access-type=URL; URL=\"#{external_file_url}\""
end
find_or_create_file(type, update_existing) click to toggle source

@param [Symbol, RDF::URI] the type of association or filter to use @param [true, false] update_existing when true, try to retrieve existing element before building one

# File lib/hydra/works/services/add_external_file_to_file_set.rb, line 61
def find_or_create_file(type, update_existing)
  if type.instance_of? Symbol
    find_or_create_file_for_symbol(type, update_existing)
  else
    find_or_create_file_for_rdf_uri(type, update_existing)
  end
end
find_or_create_file_for_rdf_uri(type, update_existing) click to toggle source
# File lib/hydra/works/services/add_external_file_to_file_set.rb, line 76
def find_or_create_file_for_rdf_uri(type, update_existing)
  current_file = file_set.filter_files_by_type(type_to_uri(type)).first if update_existing
  unless current_file
    file_set.files.build
    current_file = file_set.files.last
    Hydra::PCDM::AddTypeToFile.call(current_file, type_to_uri(type))
  end
  current_file
end
find_or_create_file_for_symbol(type, update_existing) click to toggle source
# File lib/hydra/works/services/add_external_file_to_file_set.rb, line 69
def find_or_create_file_for_symbol(type, update_existing)
  association = file_set.association(type)
  fail ArgumentError, "you're attempting to add a file to a file_set using '#{type}' association but the file_set does not have an association called '#{type}''" unless association
  current_file = association.reader if update_existing
  current_file || association.build
end
persist() click to toggle source

Persist a new file with its containing file set; otherwise, just save the file itself

# File lib/hydra/works/services/add_external_file_to_file_set.rb, line 45
def persist
  if current_file.new_record?
    file_set.save
  else
    current_file.save
  end
end
type_to_uri(type) click to toggle source

Returns appropriate URI for the requested type

* Converts supported symbols to corresponding URIs
* Converts URI strings to RDF::URI
* Returns RDF::URI objects as-is
# File lib/hydra/works/services/add_external_file_to_file_set.rb, line 90
def type_to_uri(type)
  case type
  when ::RDF::URI
    type
  when String
    ::RDF::URI(type)
  else
    fail ArgumentError, 'Invalid file type.  You must submit a URI or a symbol.'
  end
end