app/models/bulkrax/csv_entry.rb in bulkrax-5.2.1 vs app/models/bulkrax/csv_entry.rb in bulkrax-5.3.0

- old
+ new

@@ -18,11 +18,11 @@ # there's a risk that this reads the whole file into memory and could cause a memory leak def self.read_data(path) raise StandardError, 'CSV path empty' if path.blank? options = { headers: true, - header_converters: ->(h) { h.to_sym }, + header_converters: ->(h) { h.to_s.strip.to_sym }, encoding: 'utf-8' }.merge(csv_read_data_options) results = CSV.read(path, **options) csv_wrapper_class.new(results) @@ -241,33 +241,38 @@ data = data.to_a if data.is_a?(ActiveTriples::Relation) object_metadata(Array.wrap(data)) end - def build_value(key, value) - return unless hyrax_record.respond_to?(key.to_s) + def build_value(property_name, mapping_config) + return unless hyrax_record.respond_to?(property_name.to_s) - data = hyrax_record.send(key.to_s) - if data.is_a?(ActiveTriples::Relation) - if value['join'] - self.parsed_metadata[key_for_export(key)] = data.map { |d| prepare_export_data(d) }.join(Bulkrax.multi_value_element_join_on).to_s - else - data.each_with_index do |d, i| - self.parsed_metadata["#{key_for_export(key)}_#{i + 1}"] = prepare_export_data(d) - end - end + data = hyrax_record.send(property_name.to_s) + + if mapping_config['join'] || !data.is_a?(Enumerable) + self.parsed_metadata[key_for_export(property_name)] = prepare_export_data_with_join(data) else - self.parsed_metadata[key_for_export(key)] = prepare_export_data(data) + data.each_with_index do |d, i| + self.parsed_metadata["#{key_for_export(property_name)}_#{i + 1}"] = prepare_export_data(d) + end end end # On export the key becomes the from and the from becomes the destination. It is the opposite of the import because we are moving data the opposite direction # metadata that does not have a specific Bulkrax entry is mapped to the key name, as matching keys coming in are mapped by the csv parser automatically def key_for_export(key) clean_key = key_without_numbers(key) unnumbered_key = mapping[clean_key] ? mapping[clean_key]['from'].first : clean_key # Bring the number back if there is one "#{unnumbered_key}#{key.sub(clean_key, '')}" + end + + def prepare_export_data_with_join(data) + # Yes...it's possible we're asking to coerce a multi-value but only have a single value. + return data.to_s unless data.is_a?(Enumerable) + return "" if data.empty? + + data.map { |d| prepare_export_data(d) }.join(Bulkrax.multi_value_element_join_on).to_s end def prepare_export_data(datum) if datum.is_a?(ActiveTriples::Resource) datum.to_uri.to_s