Module: ActiveFedora::Datastreams

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_fedora/datastreams.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_datastream(datastream, opts = {}) ⇒ String

Adds datastream to the object. Saves the datastream to fedora upon adding. If datastream does not have a DSID, a unique DSID is generated :prefix option will set the prefix on auto-generated DSID

Returns:

  • (String)

    dsid of the added datastream



96
97
98
99
100
101
102
103
# File 'lib/active_fedora/datastreams.rb', line 96

def add_datastream(datastream, opts={})
  if datastream.dsid == nil || datastream.dsid.empty?
    prefix = opts.has_key?(:prefix) ? opts[:prefix] : "DS"
    datastream.instance_variable_set :@dsid, generate_dsid(prefix)
  end
  datastreams[datastream.dsid] = datastream
  return datastream.dsid
end

#add_disseminator_location_to_datastreamsObject

Adds the disseminator location to the datastream after the pid has been established



31
32
33
34
35
36
37
38
39
# File 'lib/active_fedora/datastreams.rb', line 31

def add_disseminator_location_to_datastreams
  self.ds_specs.each do |name,ds_config|
    ds = datastreams[name]
    if ds && ds.controlGroup == 'E' && ds_config[:disseminator].present?
      ds.dsLocation= "#{ActiveFedora.config_for_environment[:url]}/objects/#{pid}/methods/#{ds_config[:disseminator]}"
    end
  end
  true
end

#add_file_datastream(file, opts = {}) ⇒ Object

Add the given file as a datastream in the object

Parameters:

  • file (File)

    the file to add

  • opts (Hash) (defaults to: {})

    options: :dsid, :label, :mimeType, :prefix, :checksumType



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/active_fedora/datastreams.rb', line 151

def add_file_datastream(file, opts={})
  label = opts.has_key?(:label) ? opts[:label] : ""
  attrs = {:dsLabel => label, :controlGroup => 'M', :blob => file, :prefix=>opts[:prefix]}
  if opts.has_key?(:mime_type)
    attrs.merge!({:mimeType=>opts[:mime_type]})
  elsif opts.has_key?(:mimeType)
    attrs.merge!({:mimeType=>opts[:mimeType]})
  elsif opts.has_key?(:content_type)
    attrs.merge!({:mimeType=>opts[:content_type]})
  end
  attrs[:checksumType] = opts[:checksumType] if opts[:checksumType]
  attrs[:versionable] = opts[:versionable] unless opts[:versionable].nil?
  ds = create_datastream(self.class.datastream_class_for_name(opts[:dsid]), opts[:dsid], attrs)
  add_datastream(ds)
end

#configure_datastream(ds, ds_spec = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/active_fedora/datastreams.rb', line 63

def configure_datastream(ds, ds_spec=nil)
  ds_spec ||= self.ds_specs[ds.dsid]
  if ds_spec
    ds.model = self if ds_spec[:type] == RelsExtDatastream
    # If you called has_metadata with a block, pass the block into the Datastream class
    if ds_spec[:block].class == Proc
      ds_spec[:block].call(ds)
    end
  end
end

#corresponding_datastream_name(method_name) ⇒ Object

Given a method name, return the best-guess dsid



42
43
44
45
46
47
48
# File 'lib/active_fedora/datastreams.rb', line 42

def corresponding_datastream_name(method_name)
  dsid = method_name.to_s
  return dsid if datastreams.has_key? dsid
  unescaped_name = method_name.to_s.gsub('_', '-')
  return unescaped_name if datastreams.has_key? unescaped_name
  nil
end

#create_datastream(type, dsid, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/active_fedora/datastreams.rb', line 168

def create_datastream(type, dsid, opts={})
  dsid ||= generate_dsid(opts[:prefix] || "DS")
  klass = type.kind_of?(Class) ? type : type.constantize
  raise ArgumentError, "Argument dsid must be of type string" unless dsid.kind_of?(String)
  ds = klass.new(inner_object, dsid)
  [:mimeType, :controlGroup, :dsLabel, :dsLocation, :checksumType, :versionable].each do |key|
    ds.send("#{key}=".to_sym, opts[key]) unless opts[key].nil?
  end
  blob = opts[:blob] 
  if blob 
    if !ds.mimeType.present? 
      ##TODO, this is all done by rubydora -- remove
      ds.mimeType = blob.respond_to?(:content_type) ? blob.content_type : "application/octet-stream"
    end
    if !ds.dsLabel.present? && blob.respond_to?(:path)
      ds.dsLabel = File.basename(blob.path)
    end
  end

  ds.content = blob || "" 
  ds
end

#datastream_from_spec(ds_spec, name) ⇒ Object



74
75
76
# File 'lib/active_fedora/datastreams.rb', line 74

def datastream_from_spec(ds_spec, name)
  inner_object.datastream_object_for name, {}, ds_spec
end

#datastreamsObject

Returns all known datastreams for the object. If the object has been saved to fedora, the persisted datastreams will be included. Datastreams that have been modified in memory are given preference over the copy in Fedora.



59
60
61
# File 'lib/active_fedora/datastreams.rb', line 59

def datastreams
  @datastreams ||= DatastreamHash.new(self)
end

#dcObject

Return the Dublin Core (DC) Datastream. You can also get at this via the datastreams.



125
126
127
128
# File 'lib/active_fedora/datastreams.rb', line 125

def dc
  Deprecation.warn(Datastreams, 'dc is deprecated. Consider using Base#datastreams["DC"] instead.', caller)
  return datastreams["DC"] 
end

#ds_specsObject



22
23
24
# File 'lib/active_fedora/datastreams.rb', line 22

def ds_specs
  self.class.ds_specs
end

#format_dsid(prefix, suffix) ⇒ Object

Provided so that an application can override how generated pids are formatted (e.g DS01 instead of DS1)



119
120
121
# File 'lib/active_fedora/datastreams.rb', line 119

def format_dsid(prefix, suffix)
  sprintf("%s%i", prefix,suffix)
end

#generate_dsid(prefix = "DS") ⇒ Object

return a valid dsid that is not currently in use. Uses a prefix (default “DS”) and an auto-incrementing integer Example: if there are already datastreams with IDs DS1 and DS2, this method will return DS3. If you specify FOO as the prefix, it will return FOO1.



112
113
114
115
116
# File 'lib/active_fedora/datastreams.rb', line 112

def generate_dsid(prefix="DS")
  matches = datastreams.keys.map {|d| data = /^#{prefix}(\d+)$/.match(d); data && data[1].to_i}.compact
  val = matches.empty? ? 1 : matches.max + 1
  format_dsid(prefix, val)
end

#load_datastreamsObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/active_fedora/datastreams.rb', line 78

def load_datastreams
  local_ds_specs = self.ds_specs.dup
  inner_object.datastreams.each do |dsid, ds|
    self.add_datastream(ds)
    configure_datastream(datastreams[dsid])
    local_ds_specs.delete(dsid)
  end
  local_ds_specs.each do |name,ds_spec|
    ds = datastream_from_spec(ds_spec, name)
    self.add_datastream(ds)
    configure_datastream(ds, ds_spec)
  end
end

#metadata_streamsObject

return all datastreams of type ActiveFedora::RDFDatastream or ActiveFedora::NokogiriDatastream



106
107
108
# File 'lib/active_fedora/datastreams.rb', line 106

def 
  datastreams.select { |k, ds| ds.metadata? }.reject { |k, ds| ds.kind_of?(ActiveFedora::RelsExtDatastream) }.values
end

#rels_extObject

Returns the RELS-EXT Datastream Tries to grab from in-memory datastreams first Failing that, attempts to load from Fedora and addst to in-memory datastreams Failing that, creates a new RelsExtDatastream and adds it to the object



134
135
136
137
138
139
140
141
# File 'lib/active_fedora/datastreams.rb', line 134

def rels_ext
  if !datastreams.has_key?("RELS-EXT") 
    ds = ActiveFedora::RelsExtDatastream.new(@inner_object,'RELS-EXT')
    ds.model = self
    add_datastream(ds)
  end
  return datastreams["RELS-EXT"]
end

#serialize_datastreamsObject



26
27
28
# File 'lib/active_fedora/datastreams.rb', line 26

def serialize_datastreams
  datastreams.each {|k, ds| ds.serialize! }
end