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::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=, #mime_type, #mime_type=, #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



91
92
93
94
95
# File 'lib/active_fedora/datastream.rb', line 91

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



98
99
100
# File 'lib/active_fedora/datastream.rb', line 98

def after_save
  self.dirty = false
end

#before_saveObject

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



84
85
86
# File 'lib/active_fedora/datastream.rb', line 84

def before_save 
  #check_concurrency
end

#check_concurrencyObject

:nodoc:



125
126
127
# File 'lib/active_fedora/datastream.rb', line 125

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)


70
71
72
# File 'lib/active_fedora/datastream.rb', line 70

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/active_fedora/datastream.rb', line 104

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



76
77
78
79
80
81
# File 'lib/active_fedora/datastream.rb', line 76

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

#sizeObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_fedora/datastream.rb', line 50

def size
  if !self.attributes.fetch(:dsSize,nil)
    if self.new_object?
      self.attributes[:dsSize]=nil
    else
      attrs = XmlSimple.xml_in(Fedora::Repository.instance.fetch_custom(self.pid,"datastreams/#{self.dsid}"))
      self.attributes[:dsSize]=attrs["dsSize"].first
    end
  end
  self.attributes[:dsSize]
end

#to_paramObject

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



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

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