Class: Hybag::BagWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/hybag/bag_writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ BagWriter

Returns a new instance of BagWriter.



5
6
7
8
# File 'lib/hybag/bag_writer.rb', line 5

def initialize(object)
  @object = object
  @bag = object.bag
end

Instance Attribute Details

#bagObject (readonly)

Returns the value of attribute bag.



3
4
5
# File 'lib/hybag/bag_writer.rb', line 3

def bag
  @bag
end

#objectObject (readonly)

Returns the value of attribute object.



4
5
6
# File 'lib/hybag/bag_writer.rb', line 4

def object
  @object
end

Instance Method Details

#write!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hybag/bag_writer.rb', line 9

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