module ActiveFedora::DatastreamCollections::ClassMethods

def create_named_datastream_finders(name, prefix)

thumbnails_ids - Get array of dsid's for thumbnail datastreams
thumbnails - Get array of thumbnail datastreams
==== Examples for "thumbnails" datastream

[datastream_name]_ids - Returns array of named datastream dsids
[datastream_name] - Returns array of named datastreams
Creates the following helper methods for a datastream name

** EXPERIMENTAL **
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

def create_named_datastream_update_methods(name)

TODO: Add [datastream_name]_remove
thumbnails_append - Append a thumbnail datastream
==== Examples for "thumbnails" datastream

[datastream_name]_append - Add a named datastream
Creates the following helper methods for a datastream name

** EXPERIMENTAL **
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 

def has_datastream(args)

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

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

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

: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.
:mimeType - if supplied it will ensure any datastreams added are of this type, if not supplied any mimeType is acceptabl e
:type - defaults to ActiveFedora::Datastream if you would like content specific class to be used supply it here
:prefix - used to create the DSID plus an index ie. THUMB1, THUMB2. If :prefix is not specified, defaults to :name value in all uppercase
Optional Keys in args

:name - name to give this datastream (must be unique)
Required Keys in args

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

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

** EXPERIMENTAL **
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