Module: ActiveFedora::DatastreamCollections
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/active_fedora/datastream_collections.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#add_named_datastream(name, opts = {}) ⇒ Object
** EXPERIMENTAL **.
-
#add_named_file_datastream(name, file, opts = {}) ⇒ Object
** EXPERIMENTAL **.
-
#assert_kind_of(n, o, t) ⇒ Object
** EXPERIMENTAL **.
-
#datastream_names ⇒ Object
** EXPERIMENTAL **.
-
#is_named_datastream?(name) ⇒ Boolean
** EXPERIMENTAL **.
-
#named_datastreams ⇒ Object
** EXPERIMENTAL **.
-
#named_datastreams_desc ⇒ Object
** EXPERIMENTAL **.
-
#named_datastreams_desc_from_class ⇒ Object
** EXPERIMENTAL **.
-
#named_datastreams_ids ⇒ Object
** EXPERIMENTAL **.
-
#update_named_datastream(name, opts = {}) ⇒ Object
** EXPERIMENTAL **.
Instance Method Details
#add_named_datastream(name, opts = {}) ⇒ Object
** EXPERIMENTAL **
This object is used by [datastream_name]_append helper to add a named datastream but can also be called directly.
Parameters
name: name of datastream to add
opts: hash defining datastream attributes
The following are expected keys in opts hash:
:label => Defaults to the file name
:blob or :file => Required to point to the datastream file being added if managed content
:controlGroup => defaults to 'M' for managed, can also be 'E' external and 'R' for redirected
:content_type => required if the file does not respond to 'content_type' and should match :mimeType in has_datastream definition if defined
:dsLocation => holds uri location of datastream. Required only if :controlGroup is type 'E' or 'R'.
:dsid or :dsId => Optional, and used to update an existing datastream with dsid supplied. Will throw an error if dsid does not exist and does not match prefix pattern for datastream name
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/active_fedora/datastream_collections.rb', line 181 def add_named_datastream(name,opts={}) unless named_datastreams_desc.has_key?(name) && named_datastreams_desc[name].has_key?(:type) raise "Failed to add datastream. Named datastream #{name} not defined for object #{pid}." end if opts.has_key?(:mime_type) opts.merge!({:content_type=>opts[:mime_type]}) elsif opts.has_key?(:mimeType) opts.merge!({:content_type=>opts[:mimeType]}) end opts.merge!(named_datastreams_desc[name]) label = opts.has_key?(:label) ? opts[:label] : "" #only do these steps for managed datastreams unless (opts.has_key?(:controlGroup)&&opts[:controlGroup]!="M") if opts.has_key?(:file) opts.merge!({:blob => opts[:file]}) opts.delete(:file) end raise "You must define parameter blob for this managed datastream to load for #{pid}" unless opts.has_key?(:blob) #if no explicit label and is a file use original file name for label if !opts.has_key?(:label)&&opts[:blob].respond_to?(:original_filename) label = opts[:blob].original_filename end if opts[:blob].respond_to?(:content_type)&&!opts[:blob].content_type.nil? && !opts.has_key?(:content_type) opts.merge!({:content_type=>opts[:blob].content_type}) end raise "The blob must respond to content_type or the hash must have :content_type or :mime_type property set" unless opts.has_key?(:content_type) #throw error for mimeType mismatch if named_datastreams_desc[name].has_key?(:mimeType) && !named_datastreams_desc[name][:mimeType].eql?(opts[:content_type]) raise "Content type mismatch for add datastream #{name} to #{pid}. Expected: #{named_datastreams_desc[name][:mimeType]}, Actual: #{opts[:content_type]}" end else label = opts[:dsLocation] if (opts.has_key?(:dsLocation)) end opts.merge!(:dsLabel => label) ds = create_datastream(named_datastreams_desc[name][:type], opts[:dsid], opts) #Must be of type datastream assert_kind_of 'datastream', ds, ActiveFedora::Datastream #make sure dsid is nil so that it uses the prefix for mapping purposes #check dsid works for the prefix if it is set if !ds.dsid.nil? && opts.has_key?(:prefix) raise "dsid supplied does not conform to pattern #{opts[:prefix]}[number]" unless ds.dsid =~ /#{opts[:prefix]}[0-9]/ end add_datastream(ds,opts) end |
#add_named_file_datastream(name, file, opts = {}) ⇒ Object
** EXPERIMENTAL **
Calls add_named_datastream while assuming it will be managed content and sets :blob and :controlGroup values accordingly
Parameters
name: Datastream name
file: The file to add for this datastream
opts: Options hash. See +add_named_datastream+ for expected keys and values
162 163 164 165 |
# File 'lib/active_fedora/datastream_collections.rb', line 162 def add_named_file_datastream(name, file, opts={}) opts.merge!({:blob=>file,:controlGroup=>'M'}) add_named_datastream(name,opts) end |
#assert_kind_of(n, o, t) ⇒ Object
** EXPERIMENTAL **
Throws an assertion failure unless the object ‘o’ is kind_of? class ‘t’
Parameters
n: Name of object
o: The object to test
t: The class type to check is kind_of?
259 260 261 |
# File 'lib/active_fedora/datastream_collections.rb', line 259 def assert_kind_of(n, o,t) raise "Assertion failure: #{n}: #{o} is not of type #{t}" unless o.kind_of?(t) end |
#datastream_names ⇒ Object
** EXPERIMENTAL **
Returns array of datastream names defined for this object
151 152 153 |
# File 'lib/active_fedora/datastream_collections.rb', line 151 def datastream_names named_datastreams_desc.keys end |
#is_named_datastream?(name) ⇒ Boolean
** EXPERIMENTAL **
Returns true if the name is a defined named datastream
266 267 268 |
# File 'lib/active_fedora/datastream_collections.rb', line 266 def is_named_datastream?(name) named_datastreams_desc.has_key?(name) end |
#named_datastreams ⇒ Object
** EXPERIMENTAL **
Returns hash of datastream names defined by has_datastream calls mapped to array of datastream objects that have been added
Example
For the following has_datastream entries and a datastream defined for minivan only would be
has_datastream :name=>"minivan", :prefix => "VAN", :type=>ActiveFedora::Datastream,:mimeType=>"image/jpeg", :controlGroup=>'M'
has_datastream :name=>"external_images", :prefix=>"EXTIMG", :type=>ActiveFedora::Datastream,:mimeType=>"image/jpeg", :controlGroup=>'E'
Returns
{"external_images"=>[],"thumbnails"=>{#<ActiveFedora::Datastream:0x7ffd6512daf8 ...}}
281 282 283 284 285 286 287 |
# File 'lib/active_fedora/datastream_collections.rb', line 281 def named_datastreams ds_values = {} self.class.class_named_datastreams_desc.keys.each do |name| ds_values.merge!({name=>self.send("#{name}")}) end return ds_values end |
#named_datastreams_desc ⇒ Object
** EXPERIMENTAL **
Returns the hash that stores arguments passed to has_datastream calls within an ActiveFedora::Base child class.
has_datastream :name=>"audio_file", :prefix=>"AUDIO", :type=>ActiveFedora::Datastream, :mimeType=>"audio/x-wav"
has_datastream :name=>"external_images", :prefix=>"EXTIMG", :type=>ActiveFedora::Datastream,:mimeType=>"image/jpeg", :controlGroup=>'E'
The above examples result in the following hash
{"audio_file"=>{:prefix=>"AUDIO",:type=>ActiveFedora::Datastream, :mimeType=>"audio/x-wav", :controlGroup=>'M'},
"external_images=>{:prefix=>"EXTIMG", :type=>ActiveFedora::Datastream,:mimeType=>"image/jpeg", :controlGroup=>'E'}}
This hash is later used when adding a named datastream such as an “audio_file” as defined above.
144 145 146 |
# File 'lib/active_fedora/datastream_collections.rb', line 144 def named_datastreams_desc self.class_named_datastreams_desc ||= {} end |
#named_datastreams_desc_from_class ⇒ Object
** EXPERIMENTAL **
Get class variable hash that stores has_datastream arguments. It is used to initialize the value returned by public named_datastreams_desc method Deprecated
331 332 333 334 |
# File 'lib/active_fedora/datastream_collections.rb', line 331 def named_datastreams_desc_from_class logger.warn "named_datastreams_desc_from_class is deprecated and will be removed in the next version" self.class.class_named_datastreams_desc end |
#named_datastreams_ids ⇒ Object
** EXPERIMENTAL **
Returns hash of datastream names mapped to an array of dsid’s for named datastream objects
Example
For the following has_datastream call, assume we have added two datastreams.
has_datastream :name=>"thumbnails",:prefix => "THUMB",:type=>ActiveFedora::Datastream, :mimeType=>"image/jpeg", :controlGroup=>'M'
It would then return
{"thumbnails=>["THUMB1", "THUMB2"]}
300 301 302 303 304 305 306 307 |
# File 'lib/active_fedora/datastream_collections.rb', line 300 def named_datastreams_ids dsids = {} self.class.class_named_datastreams_desc.keys.each do |name| dsid_array = self.send("#{name}_ids") dsids[name] = dsid_array end return dsids end |
#update_named_datastream(name, opts = {}) ⇒ Object
** EXPERIMENTAL **
Update an existing named datastream. It has same parameters as add_named_datastream except the :dsid key is now required.
TODO
Currently requires you to update file if a managed datastream but could change to allow metadata only updates as well
245 246 247 248 249 250 |
# File 'lib/active_fedora/datastream_collections.rb', line 245 def update_named_datastream(name, opts={}) #check that dsid provided matches existing datastream with that name raise "You must define parameter dsid for datastream to update for #{pid}" unless opts.include?(:dsid) raise "Datastream with name #{name} and dsid #{opts[:dsid]} does not exist for #{pid}" unless self.send("#{name}_ids").include?(opts[:dsid]) add_named_datastream(name,opts) end |