Class: Rubydora::Datastream

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Dirty, ExtensionParameters
Defined in:
lib/rubydora/datastream.rb

Overview

This class represents a Fedora datastream object and provides helper methods for creating and manipulating them.

Constant Summary collapse

DS_ATTRIBUTES =

mapping datastream attributes (and api parameters) to datastream profile names

{:controlGroup => :dsControlGroup, :dsLocation => :dsLocation, :altIDs => nil, :dsLabel => :dsLabel, :versionable => :dsVersionable, :dsState => :dsState, :formatURI => :dsFormatURI, :checksumType => :dsChecksumType, :checksum => :dsChecksum, :mimeType => :dsMIME, :logMessage => nil, :ignoreContent => nil, :lastModifiedDate => nil, :content => nil}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ExtensionParameters

#apply_extensions, included

Constructor Details

#initialize(digital_object, dsid, options = {}) ⇒ Datastream

Initialize a Rubydora::Datastream object, which may or may not already exist in the datastore.

Provides ‘after_initialize` callback for extensions

Parameters:

  • (Rubydora::DigitalObject)
  • Datastream (String)

    ID

  • default (Hash)

    attribute values (used esp. for creating new datastreams)



43
44
45
46
47
48
49
50
51
# File 'lib/rubydora/datastream.rb', line 43

def initialize digital_object, dsid, options = {}
  _run_initialize_callbacks do
  @digital_object = digital_object
  @dsid = dsid
  options.each do |key, value|
    self.send(:"#{key}=", value)
  end
  end
end

Instance Attribute Details

#digital_objectObject (readonly)

Returns the value of attribute digital_object.



13
14
15
# File 'lib/rubydora/datastream.rb', line 13

def digital_object
  @digital_object
end

#dsidObject (readonly)

Returns the value of attribute dsid.



13
14
15
# File 'lib/rubydora/datastream.rb', line 13

def dsid
  @dsid
end

Instance Method Details

#contentString Also known as: read

Retrieve the content of the datastream (and cache it)

Returns:

  • (String)


66
67
68
69
70
71
72
73
74
# File 'lib/rubydora/datastream.rb', line 66

def content
  begin
    @content ||= repository.datastream_dissemination :pid => pid, :dsid => dsid
  rescue RestClient::ResourceNotFound
  end

  content = @content.read and @content.rewind if @content.kind_of? IO
  content ||= @content
end

#content=(new_content) ⇒ String or IO

Set the content of the datastream

Parameters:

  • (String or IO)

Returns:

  • (String or IO)


86
87
88
89
# File 'lib/rubydora/datastream.rb', line 86

def content= new_content
   content_will_change!
   @content = new_content
end

#createRubydora::Datastream

Add datastream to Fedora



115
116
117
118
119
# File 'lib/rubydora/datastream.rb', line 115

def create
  repository.add_datastream to_api_params.merge({ :pid => pid, :dsid => dsid })
  reset_profile_attributes
  Datastream.new(digital_object, dsid)
end

#deleteRubydora::Datastream

Purge the datastream from Fedora

Returns:



132
133
134
135
136
137
# File 'lib/rubydora/datastream.rb', line 132

def delete
  repository.purge_datastream(:pid => pid, :dsid => dsid) unless self.new?
  digital_object.datastreams.delete(dsid)
  reset_profile_attributes
  self
end

#new?Boolean

Does this datastream already exist?

Returns:

  • (Boolean)


60
61
62
# File 'lib/rubydora/datastream.rb', line 60

def new?
  profile.empty?
end

#pidObject

Helper method to get digital object pid



54
55
56
# File 'lib/rubydora/datastream.rb', line 54

def pid
  digital_object.pid
end

#profileHash

Retrieve the datastream profile as a hash (and cache it)

Returns:

  • (Hash)

    see Fedora #getDatastream documentation for keys



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rubydora/datastream.rb', line 93

def profile
  @profile ||= begin
    profile_xml = repository.datastream(:pid => pid, :dsid => dsid)
    profile_xml.gsub! '<datastreamProfile', '<datastreamProfile xmlns="http://www.fedora.info/definitions/1/0/management/"' unless profile_xml =~ /xmlns=/
    doc = Nokogiri::XML(profile_xml)
    h = doc.xpath('/management:datastreamProfile/*', {'management' => "http://www.fedora.info/definitions/1/0/management/"} ).inject({}) do |sum, node|
                 sum[node.name] ||= []
                 sum[node.name] << node.text
                 sum
               end
    h.select { |key, value| value.length == 1 }.each do |key, value|
      h[key] = value.first
    end

    h
  rescue
    {}
  end
end

#saveRubydora::Datastream

Modify or save the datastream



123
124
125
126
127
128
# File 'lib/rubydora/datastream.rb', line 123

def save
  return create if new?
  repository.modify_datastream to_api_params.merge({ :pid => pid, :dsid => dsid })
  reset_profile_attributes
  Datastream.new(digital_object, dsid)
end

#urlString

Get the URL for the datastream content

Returns:

  • (String)


79
80
81
# File 'lib/rubydora/datastream.rb', line 79

def url
  repository.datastream_url(pid, dsid) + "/content"
end