class Darlingtonia::HyraxBasicMetadataMapper

A mapper for Hyrax metadata.

Maps from hash accessor syntax (`['title']`) to method call dot syntax (`.title`).

The fields provided by this mapper are the same as the properties defined in `Hyrax::CoreMetadata` and `Hyrax::BasicMetadata`.

@note This mapper allows you to set values for all the Hyrax fields, but depending on how you create the records, some of the values might get clobbered. For example, if you use Hyrax's actor stack to create records, it might overwrite fields like `date_modified` or `depositor`.

@see HashMapper Parent class for more info and examples.

Constants

CSV_HEADERS

If your CSV headers don't exactly match the the method name for the property's setter method, add a mapping here. Example: the method name is work.resource_type, but in the CSV file, the header is 'resource type' (without the underscore).

Attributes

delimiter[W]

Public Instance Methods

date_modified() click to toggle source
# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 44
def date_modified
  single_value('date_modified')
end
delimiter() click to toggle source

@return [String] The delimiter that will be used to split a metadata field into separate values. @example

mapper = HyraxBasicMetadataMapper.new
mapper.metadata = { 'language' => 'English|~|French|~|Japanese' }
mapper.language = ['English', 'French', 'Japanese']
# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 96
def delimiter
  @delimiter ||= '|~|'
end
depositor() click to toggle source

Properties defined with `multiple: false` in Hyrax should return a single value instead of an Array of values.

# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 40
def depositor
  single_value('depositor')
end
fields() click to toggle source

@return [Enumerable<Symbol>] The fields the mapper can process.

# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 33
def fields
  core_fields + basic_fields + [:visibility]
end
files() click to toggle source
# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 85
def files
  map_field('files')
end
import_url() click to toggle source
# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 56
def import_url
  single_value('import_url')
end
label() click to toggle source
# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 48
def label
  single_value('label')
end
map_field(name) click to toggle source

@see MetadataMapper#map_field

# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 103
def map_field(name)
  method_name = name.to_s
  method_name = CSV_HEADERS[name] if CSV_HEADERS.keys.include?(name)
  key = matching_header(method_name)
  Array(metadata[key]&.split(delimiter))
end
relative_path() click to toggle source
# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 52
def relative_path
  single_value('relative_path')
end
visibility() click to toggle source

We should accept visibility values that match the UI and transform them into the controlled vocabulary term expected by Hyrax

# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 62
def visibility
  case metadata[matching_header('visibility')]&.downcase&.gsub(/\s+/, "")
  when 'public'
    'open'
  when 'open'
    'open'
  when 'registered'
    'authenticated'
  when "authenticated"
    'authenticated'
  when ::Hyrax::Institution.name&.downcase&.gsub(/\s+/, "")
    'authenticated'
  when ::Hyrax::Institution.name_full&.downcase&.gsub(/\s+/, "")
    'authenticated'
  when 'private'
    'restricted'
  when 'restricted'
    'restricted'
  else
    'restricted' # This is the default if nothing else matches
  end
end

Protected Instance Methods

basic_fields() click to toggle source

Properties defined in Hyrax::BasicMetadata

# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 139
def basic_fields
  [:label, :relative_path, :import_url,
   :resource_type, :creator, :contributor,
   :description, :keyword, :license,
   :rights_statement, :publisher, :date_created,
   :subject, :language, :identifier,
   :based_near, :related_url,
   :bibliographic_citation, :source]
end
core_fields() click to toggle source

Properties defined in Hyrax::CoreMetadata Note that date_uploaded is NOT set here, even though it is defined in Hyrax::CoreMetadata. Hyrax expects to set date_uploaded itself, and sending a metadata value for that field interferes with Hyrax expected behavior.

# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 134
def core_fields
  [:depositor, :title, :date_modified]
end
matching_header(field_name) click to toggle source

Lenient matching for headers. If the user has headers like:

'Title' or 'TITLE' or 'Title  '

it should match the :title field.

# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 122
def matching_header(field_name)
  metadata.keys.find do |key|
    next unless key
    key.downcase.strip == field_name
  end
end
single_value(field_name) click to toggle source

Some fields should have single values instead of array values.

# File lib/darlingtonia/hyrax_basic_metadata_mapper.rb, line 114
def single_value(field_name)
  metadata[matching_header(field_name)]
end