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) ⇒ Object

:nodoc:



110
111
112
113
# File 'lib/active_fedora/datastreams.rb', line 110

def add(datastream) # :nodoc:
  ActiveSupport::Deprecation.warn "Warning: ActiveFedora::Base.add has been deprecatedand will be removed in 5.0.  Use add_datastream"
  add_datastream(datastream)
end

#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



101
102
103
104
105
106
107
108
# File 'lib/active_fedora/datastreams.rb', line 101

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.class.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



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/active_fedora/datastreams.rb', line 184

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

#additional_attributes_for_external_and_redirect_control_groups(ds, ds_config) ⇒ Object

This method provides validation of proper options for control_group ‘E’ and ‘R’ and builds an attribute hash to be merged back into ds.attributes prior to saving

Parameters:

  • ds (Object)

    The datastream

  • ds_config (Object)

    hash of options which may contain :disseminator and :url



228
229
230
231
232
233
234
235
236
# File 'lib/active_fedora/datastreams.rb', line 228

def additional_attributes_for_external_and_redirect_control_groups(ds,ds_config)
  if ds.controlGroup=='E'
    if !ds_config[:disseminator].present? && ds_config[:url].present?
      ds.dsLocation= ds_config[:url]
    end
  elsif ds.controlGroup=='R'
    ds.dsLocation= ds_config[:url]
  end
end

#configure_datastream(ds, ds_spec = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/active_fedora/datastreams.rb', line 68

def configure_datastream(ds, ds_spec=nil)
  ds_spec ||= self.class.ds_specs[ds.instance_variable_get(:@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)


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/active_fedora/datastreams.rb', line 201

def create_datastream(type, dsid, opts={})
  dsid = generate_dsid(opts[:prefix] || "DS") if dsid == nil
  klass = type.kind_of?(Class) ? type : type.constantize
  raise ArgumentError, "Argument dsid must be of type string" unless dsid.kind_of?(String) || dsid.kind_of?(NilClass)
  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



79
80
81
# File 'lib/active_fedora/datastreams.rb', line 79

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

#datastreams_in_memoryObject



63
64
65
66
# File 'lib/active_fedora/datastreams.rb', line 63

def datastreams_in_memory
  ActiveSupport::Deprecation.warn("ActiveFedora::Base.datastreams_in_memory has been deprecated.  Use #datastreams instead")
  datastreams
end

#dcObject

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



158
159
160
161
# File 'lib/active_fedora/datastreams.rb', line 158

def dc
  #dc = REXML::Document.new(datastreams["DC"].content)
  return  datastreams["DC"] 
end

#file_streamsObject

Deprecated.

return all datastreams not of type ActiveFedora::MetadataDatastream (that aren’t Dublin Core or RELS-EXT streams either)



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/active_fedora/datastreams.rb', line 129

def file_streams
  ActiveSupport::Deprecation.warn("ActiveFedora::Base#file_streams has been deprecated and will be removed in 5.0")
  results = []
  datastreams.each_value do |ds|
    if !ds.kind_of?(ActiveFedora::MetadataDatastream) && !ds.kind_of?(ActiveFedora::NokogiriDatastream)
      dsid = ds.dsid
      if dsid != "DC" && dsid != "RELS-EXT"
        results << ds
      end
    end
  end
  return results
end

#format_dsid(prefix, suffix) ⇒ Object

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



152
153
154
# File 'lib/active_fedora/datastreams.rb', line 152

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.



145
146
147
148
149
# File 'lib/active_fedora/datastreams.rb', line 145

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



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/active_fedora/datastreams.rb', line 83

def load_datastreams
  ds_specs = self.class.ds_specs.dup
  inner_object.datastreams.each do |dsid, ds|
    self.add_datastream(ds)
    configure_datastream(datastreams[dsid])
    ds_specs.delete(dsid)
  end
  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::MetadataDatastream



116
117
118
119
120
121
122
123
124
# File 'lib/active_fedora/datastreams.rb', line 116

def 
  results = []
  datastreams.each_value do |ds|
    if ds.kind_of?(ActiveFedora::MetadataDatastream) || ds.kind_of?(ActiveFedora::NokogiriDatastream)
      results << ds
    end
  end
  return results
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



167
168
169
170
171
172
173
174
# File 'lib/active_fedora/datastreams.rb', line 167

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



22
23
24
25
26
27
28
# File 'lib/active_fedora/datastreams.rb', line 22

def serialize_datastreams
  datastreams.each {|k, ds| ds.serialize! }
  self. = datastreams.any? do |k,ds| 
    ds.changed? && (ds.class.included_modules.include?(ActiveFedora::MetadataDatastreamHelper) || ds.instance_of?(ActiveFedora::RelsExtDatastream) || ds.kind_of?(ActiveFedora::RDFDatastream))
  end
 true
end