Class: Rubydora::Datastream
- Inherits:
-
Object
- Object
- Rubydora::Datastream
- 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
-
#digital_object ⇒ Object
readonly
Returns the value of attribute digital_object.
-
#dsid ⇒ Object
readonly
Returns the value of attribute dsid.
Instance Method Summary collapse
-
#content ⇒ String
(also: #read)
Retrieve the content of the datastream (and cache it).
-
#content=(new_content) ⇒ String or IO
Set the content of the datastream.
-
#create ⇒ Rubydora::Datastream
Add datastream to Fedora.
-
#delete ⇒ Rubydora::Datastream
Purge the datastream from Fedora.
-
#initialize(digital_object, dsid, options = {}) ⇒ Datastream
constructor
Initialize a Rubydora::Datastream object, which may or may not already exist in the datastore.
-
#new? ⇒ Boolean
Does this datastream already exist?.
-
#pid ⇒ Object
Helper method to get digital object pid.
-
#profile ⇒ Hash
Retrieve the datastream profile as a hash (and cache it).
-
#save ⇒ Rubydora::Datastream
Modify or save the datastream.
-
#url ⇒ String
Get the URL for the datastream content.
Methods included from ExtensionParameters
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
43 44 45 46 47 48 49 50 51 |
# File 'lib/rubydora/datastream.rb', line 43 def initialize digital_object, dsid, = {} _run_initialize_callbacks do @digital_object = digital_object @dsid = dsid .each do |key, value| self.send(:"#{key}=", value) end end end |
Instance Attribute Details
#digital_object ⇒ Object (readonly)
Returns the value of attribute digital_object.
13 14 15 |
# File 'lib/rubydora/datastream.rb', line 13 def digital_object @digital_object end |
#dsid ⇒ Object (readonly)
Returns the value of attribute dsid.
13 14 15 |
# File 'lib/rubydora/datastream.rb', line 13 def dsid @dsid end |
Instance Method Details
#content ⇒ String Also known as: read
Retrieve the content of the datastream (and cache it)
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
86 87 88 89 |
# File 'lib/rubydora/datastream.rb', line 86 def content= new_content content_will_change! @content = new_content end |
#create ⇒ Rubydora::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 |
#delete ⇒ Rubydora::Datastream
Purge the datastream from Fedora
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?
60 61 62 |
# File 'lib/rubydora/datastream.rb', line 60 def new? profile.empty? end |
#pid ⇒ Object
Helper method to get digital object pid
54 55 56 |
# File 'lib/rubydora/datastream.rb', line 54 def pid digital_object.pid end |
#profile ⇒ Hash
Retrieve the datastream profile as a hash (and cache it)
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 |
#save ⇒ Rubydora::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 |
#url ⇒ String
Get the URL for the datastream content
79 80 81 |
# File 'lib/rubydora/datastream.rb', line 79 def url repository.datastream_url(pid, dsid) + "/content" end |