Module: ActiveFedora::Datastreams

Extended by:
ActiveSupport::Concern, Deprecation
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



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

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



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

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

#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



195
196
197
198
199
200
201
202
203
# File 'lib/active_fedora/datastreams.rb', line 195

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



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

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



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

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



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

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.



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

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

#dcObject

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



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

def dc
  return datastreams["DC"] 
end

#ds_specsObject



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

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)



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

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.



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

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



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

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



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

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



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

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