Sha256: c03350162dd5f4b2d08800d2327bafc7a787024a48324eebace263278da191e4

Contents?: true

Size: 1.51 KB

Versions: 14

Compression:

Stored size: 1.51 KB

Contents

module Hybag
  class BagWriter
    attr_reader :bag
    attr_reader :object
    def initialize(object)
      @object = object
      @bag = object.bag
    end
    def write!
      # add the datastreams to the bag, then manifest
      @object.datastreams.each do |label, ds|
        unless ds.content.nil?
          label = label + mime_extension(ds)
          if bag_tags.include? ds
            bag.add_tag_file(label) { |f|
              f.puts ds.content
            }
          elsif bag_fedora_tags.values.include? ds
            bag.add_tag_file('fedora/' + label) { |f|
              f.puts ds.content
            }
          else
            bag.add_file(label) { |f|
              content = ds.content
              content = ds.content.read if ds.content.respond_to?(:read)
              f.puts content.force_encoding('UTF-8')
            }
          end
        end
      end
      bag.tagmanifest!
      bag.manifest!
      return bag
    end

    private

    # return all non-fedora tag files
    def bag_tags
      object.metadata_streams
    end

    def mime_extension(ds)
      if ds.kind_of?(ActiveFedora::NtriplesRDFDatastream)
        ext = 'nt'
      else
        if ds.mimeType.blank?
          ext = ''
        else
          ext = MIME::Types[ds.mimeType] || ''
          ext = ext.first.extensions[0] unless ext.blank?

        end
      end
      return '.' + ext
    end

    def bag_fedora_tags
      object.datastreams.select { |label, ds| ds.is_a?(ActiveFedora::RelsExtDatastream) or ds.dsid == "DC"}
    end


  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
hybag-0.2.0 lib/hybag/bag_writer.rb
hybag-0.1.1 lib/hybag/bag_writer.rb
hybag-0.1.0 lib/hybag/bag_writer.rb
hybag-0.0.11 lib/hybag/bag_writer.rb
hybag-0.0.10 lib/hybag/bag_writer.rb
hybag-0.0.9 lib/hybag/bag_writer.rb
hybag-0.0.8 lib/hybag/bag_writer.rb
hybag-0.0.7 lib/hybag/bag_writer.rb
hybag-0.0.6 lib/hybag/bag_writer.rb
hybag-0.0.5 lib/hybag/bag_writer.rb
hybag-0.0.4 lib/hybag/bag_writer.rb
hybag-0.0.3 lib/hybag/bag_writer.rb
hybag-0.0.2 lib/hybag/bag_writer.rb
hybag-0.0.1 lib/hybag/bag_writer.rb