Module: ActiveFedora::DatastreamCollections::ClassMethods

Defined in:
lib/active_fedora/datastream_collections.rb

Instance Method Summary collapse

Instance Method Details

#create_named_datastream_finders(name, prefix) ⇒ Object

Creates the following helper methods for a datastream name

[datastream_name]  - Returns array of named datastreams
[datastream_name]_ids - Returns array of named datastream dsids

Examples for “thumbnails” datastream

thumbnails        -  Get array of thumbnail datastreams
thumbnails_ids    -  Get array of dsid's for thumbnail datastreams


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/active_fedora/datastream_collections.rb', line 97

def create_named_datastream_finders(name, prefix)
  class_eval <<-END,  __FILE__, __LINE__
  def #{name}(opts={})
    id_array = []
    keys = datastreams.keys

    id_array = keys.select {|v| v =~ /^#{prefix}\\d+$/}

    if opts[:response_format] == :id_array
      return id_array
    else
      named_ds = []
      id_array.each do |name|
        if datastreams.has_key?(name)
          named_ds.push(datastreams[name])
        end
      end
      return named_ds
    end
  end
  def #{name}_ids
    #{name}(:response_format => :id_array)
  end
  END
end

#create_named_datastream_update_methods(name) ⇒ Object

Creates the following helper methods for a datastream name

[datastream_name]_append  - Add a named datastream

Examples for “thumbnails” datastream

thumbnails_append -  Append a thumbnail datastream

TODO: Add [datastream_name]_remove



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/active_fedora/datastream_collections.rb', line 74

def create_named_datastream_update_methods(name)
  append_file_method_name = "#{name.to_s.downcase}_file_append"
  append_method_name = "#{name.to_s.downcase}_append"
  #remove_method_name = "#{name.to_s.downcase}_remove"
  self.send(:define_method,:"#{append_file_method_name}") do |*args| 
    file,opts = *args
    opts ||= {}
    add_named_file_datastream(name,file,opts)
  end
  
  self.send(:define_method,:"#{append_method_name}") do |*args| 
    #call add_named_datastream instead of add_file_named_datastream in case not managed datastream
    add_named_datastream(name,*args)
  end
end

#has_datastream(args) ⇒ Object

Allows for a datastream to be treated like any other attribute of a model class while enforcing mimeType and/or datastream type (ie. external, managed, etc.) if defined.

Examples

has_datastream :name=>"thumbnails",:prefix => "THUMB",:type=>ActiveFedora::Datastream, :mimeType=>"image/jpeg", :controlGroup=>'M'                     
has_datastream :name=>"EADs", :type=>ActiveFedora::Datastream, :mimeType=>"application/xml", :controlGroup=>'M' 
has_datastream :name=>"external_images", :type=>ActiveFedora::Datastream, :controlGroup=>'E'

Required Keys in args

:name  -  name to give this datastream (must be unique)

Optional Keys in args

:prefix - used to create the DSID plus an index ie. THUMB1, THUMB2.  If :prefix is not specified, defaults to :name value in all uppercase 
:type - defaults to ActiveFedora::Datastream if you would like content specific class to be used supply it here
:mimeType - if supplied it will ensure any datastreams added are of this type, if not supplied any mimeType is acceptabl e
:controlGroup -  possible values "X", "M", "R", or "E" (InlineXML, Managed Content, Redirect, or External Referenced) If controlGroup is 'E' or 'R' it expects a dsLocation be defined when adding the datastream.

You use the datastream attribute using helper methods created for each datastream name:

Helper Method Examples

thumbnails_append -  Append a thumbnail datastream
thumbnails        -  Get array of thumbnail datastreams
thumbnails_ids    -  Get array of dsid's for thumbnail datastreams

When loading the list of datastreams for a name from Fedora it uses the DSID prefix to find them in Fedora



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/active_fedora/datastream_collections.rb', line 46

def has_datastream(args)
  unless args.has_key?(:name)
    return false
  end
  unless args.has_key?(:prefix)
    args.merge!({:prefix=>args[:name].to_s.upcase})
  end
  unless class_named_datastreams_desc.has_key?(args[:name]) 
    class_named_datastreams_desc[args[:name]] = {} 
  end
      
  args.merge!({:mimeType=>args[:mime_type]}) if args.has_key?(:mime_type)
  
  unless class_named_datastreams_desc[args[:name]].has_key?(:type) 
    #default to type ActiveFedora::Datastream
    args.merge!({:type => "ActiveFedora::Datastream"})
  end
  class_named_datastreams_desc[args[:name]]= args   
  create_named_datastream_finders(args[:name],args[:prefix])
  create_named_datastream_update_methods(args[:name])
end