Class: ActiveFedora::Datastream

Inherits:
Fedora::Datastream show all
Defined in:
lib/active_fedora/datastream.rb

Overview

This class represents a Fedora datastream

Instance Attribute Summary collapse

Attributes inherited from Fedora::Datastream

#mime_type

Attributes inherited from Fedora::BaseObject

#attributes, #blob, #errors, #new_object, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Fedora::Datastream

#control_group, #control_group=, #dsid, #label, #label=, #uri, #url

Methods inherited from Fedora::BaseObject

#[], #new_object?

Constructor Details

#initialize(attrs = {}) ⇒ Datastream

Returns a new instance of Datastream.



9
10
11
12
13
# File 'lib/active_fedora/datastream.rb', line 9

def initialize(attrs = {})
  @fields={}
  @dirty = false
  super
end

Instance Attribute Details

#dirtyObject

Returns the value of attribute dirty.



7
8
9
# File 'lib/active_fedora/datastream.rb', line 7

def dirty
  @dirty
end

#fieldsObject

Returns the value of attribute fields.



7
8
9
# File 'lib/active_fedora/datastream.rb', line 7

def fields
  @fields
end

#last_modifiedObject

Returns the value of attribute last_modified.



7
8
9
# File 'lib/active_fedora/datastream.rb', line 7

def last_modified
  @last_modified
end

Class Method Details

.delete(parent_pid, dsid) ⇒ Object



26
27
28
# File 'lib/active_fedora/datastream.rb', line 26

def self.delete(parent_pid, dsid)
  Fedora::Repository.instance.delete("%s/datastreams/%s"%[parent_pid, dsid])
end

.from_xml(tmpl, node) ⇒ Object

Populate a Datastream object based on the “datastream” node from a FOXML file

Parameters:

  • tmpl (ActiveFedora::Datastream)

    the Datastream object that you are building

  • node (Nokogiri::XML::Node)

    the “foxml:datastream” node from a FOXML file



79
80
81
82
83
# File 'lib/active_fedora/datastream.rb', line 79

def self.from_xml(tmpl, node)
  tmpl.instance_variable_set(:@dirty, false)
  tmpl.control_group= node['CONTROL_GROUP']
  tmpl
end

Instance Method Details

#after_saveObject

Callback. Override this to insert behaviors after the save method. By default, sets self.dirty = false



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

def after_save
  self.dirty = false
end

#before_saveObject

Callback. Does nothing by default. Override this to insert behaviors before the save method.



72
73
74
# File 'lib/active_fedora/datastream.rb', line 72

def before_save 
  #check_concurrency
end

#check_concurrencyObject

:nodoc:



113
114
115
# File 'lib/active_fedora/datastream.rb', line 113

def check_concurrency # :nodoc:
  return true
end

#contentObject

Return the xml content representing this Datastream from Fedora



16
17
18
19
# File 'lib/active_fedora/datastream.rb', line 16

def content
  result = Fedora::Repository.instance.fetch_custom(self.attributes[:pid], "datastreams/#{self.dsid}/content")
  return result
end

#content=(content) ⇒ Object

set this Datastream’s content



22
23
24
# File 'lib/active_fedora/datastream.rb', line 22

def content=(content)
  self.blob = content
end

#deleteObject



30
31
32
# File 'lib/active_fedora/datastream.rb', line 30

def delete
  self.class.delete(self.pid, self.dsid)
end

#dirty?Boolean

Test whether this datastream been modified since it was last saved?

Returns:

  • (Boolean)


58
59
60
# File 'lib/active_fedora/datastream.rb', line 58

def dirty?
  @dirty
end

#dsid=(dsid) ⇒ Object

set this datastreams identifier (note: sets both dsID and dsid)



45
46
47
48
# File 'lib/active_fedora/datastream.rb', line 45

def dsid=(dsid)
  self.attributes[:dsID] = dsid
  self.attributes[:dsid] = dsid
end

#last_modified_in_repositoryObject

returns a datetime in the standard W3C DateTime Format.

ie 2008-10-17T00:17:18.194Z



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

def last_modified_in_repository
  # A hack to get around the fact that you can't call getDatastreamHistory 
  # or API-M getDatasreams on Fedora 3.0 REST API  
  # grabs the CREATED attribute off of the last foxml:datastreamVersion 
  # within the appropriate datastream node in the objectXML
  if self.pid != nil
    object_xml = Fedora::FedoraObject.object_xml(self.pid).gsub("\n     ","")
    datastream_xml = REXML::Document.new(object_xml).root.elements["foxml:datastream[@ID='#{self.dsid}']"]
    
    if datastream_xml.length > 3
      datastream_xml.elements.each do |el|
        logger.debug el.inspect
      end
    end
    
    datastream_xml.elements[datastream_xml.length - 2].attributes["CREATED"]
  else
    return nil
  end
end

#pidObject

get this datastreams identifier



35
36
37
# File 'lib/active_fedora/datastream.rb', line 35

def pid
  self.attributes[:pid]
end

#pid=(pid) ⇒ Object

set this datastreams parent identifier



40
41
42
# File 'lib/active_fedora/datastream.rb', line 40

def pid=(pid)
  self.attributes[:pid] = pid
end

#saveObject

Save the datastream into fedora. Also triggers #before_save and #after_save callbacks



64
65
66
67
68
69
# File 'lib/active_fedora/datastream.rb', line 64

def save
  before_save
  result = Fedora::Repository.instance.save(self)
  after_save
  result
end

#to_paramObject

compatibility method for rails’ url generators. This method will urlescape escape dots, which are apparently invalid characters in a dsid.



53
54
55
# File 'lib/active_fedora/datastream.rb', line 53

def to_param
  dsid.gsub(/\./, '%2e')
end