class Hydra::Works::AddFileToFileSet::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_file_to_file_set.rb, line 26
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(file) 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_file_to_file_set.rb, line 33
def update(file)
  attach_attributes(file)
  persist
end

Private Instance Methods

attach_attributes(file) click to toggle source
# File lib/hydra/works/services/add_file_to_file_set.rb, line 49
def attach_attributes(file)
  current_file.content = file
  current_file.original_name = DetermineOriginalName.call(file)
  current_file.mime_type = DetermineMimeType.call(file, current_file.original_name)
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_file_to_file_set.rb, line 57
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_file_to_file_set.rb, line 72
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_file_to_file_set.rb, line 65
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_file_to_file_set.rb, line 41
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_file_to_file_set.rb, line 86
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