Sha256: 8fda231d5676fec3a73a180b05127780d9d0668098b460ef95b44d8d6c4829c3

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module Ddr
  module Datastreams
    class MetadataDatastream < ActiveFedora::NtriplesRDFDatastream

      def self.term_names
        properties.keys.map(&:to_sym).sort
      end

      # Returns ActiveTriplesTerm now that this is an RDF datastream
      def values(term)
        term == :format ? self.format : self.send(term)
      end

      # Update term with values
      # Note that empty values (nil or "") are rejected from values array
      def set_values(term, values)
        vals = Array(values)
               .map    { |v| sanitize_value(v) }
               .reject { |v| reject_value?(v)  }
        begin
          self.send("#{term}=", vals)
        rescue NoMethodError
          raise ArgumentError, "No such term: #{term}"
        end
      end

      # Add value to term
      # Note that empty value (nil or "") is not added
      def add_value(term, value)
        vals = values(term).to_a << value
        set_values(term, vals)
      end

      def content_changed?
        # Patches a bug in AF RDF datastreams where
        # Content appears to change from nil to empty string
        super && !empty?
      end

      private

      def sanitize_value(value)
        value
          .to_s
          .strip
          .gsub(/[[:cntrl:]]/, "")
      end

      def reject_value?(value)
        value.blank?
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ddr-models-2.10.0.rc1 lib/ddr/datastreams/metadata_datastream.rb