Class: Rubydora::DigitalObject
- Inherits:
-
Object
- Object
- Rubydora::DigitalObject
- Extended by:
- ActiveModel::Callbacks
- Includes:
- ActiveModel::Dirty, ExtensionParameters, ModelsMixin, RelationshipsMixin
- Defined in:
- lib/rubydora/digital_object.rb
Overview
This class represents a Fedora object and provides helpers for managing attributes, datastreams, and relationships.
Using the extension framework, implementors may provide additional functionality to this base implementation.
Constant Summary collapse
- OBJ_ATTRIBUTES =
mapping object parameters to profile elements
{:state => :objState, :ownerId => :objOwnerId, :label => :objLabel, :logMessage => nil, :lastModifiedDate => :objLastModDate }
Constants included from RelationshipsMixin
Instance Attribute Summary collapse
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
Class Method Summary collapse
-
.create(pid, options = {}, repository = nil) ⇒ Object
create a new fedora object (see also DigitalObject#save).
-
.find(pid, repository = nil) ⇒ Object
find an existing fedora object TODO: raise an error if the object does not yet exist.
Instance Method Summary collapse
-
#datastreams ⇒ Array<Rubydora::Datastream>
(also: #datastream)
List of datastreams.
-
#delete ⇒ Rubydora::DigitalObject
Purge the object from Fedora.
-
#fetch(dsid) ⇒ Object
(also: #[])
provide an hash-like way to access datastreams.
-
#initialize(pid, repository = nil, options = {}) ⇒ DigitalObject
constructor
Initialize a Rubydora::DigitalObject, which may or may not already exist in the data store.
-
#new? ⇒ Boolean
Does this object already exist?.
-
#profile ⇒ Hash
Retrieve the object profile as a hash (and cache it).
-
#repository ⇒ Rubydora::Repository
repository reference from the digital object.
-
#save ⇒ Rubydora::DigitalObject
persist the object to Fedora, either as a new object by modifing the existing object.
-
#uri ⇒ Object
(also: #fqpid)
Return a full uri pid (for use in relations, etc.
Methods included from RelationshipsMixin
#add_relationship, included, #purge_relationship, #relationship, #relationship_changed, #relationships
Methods included from ModelsMixin
Methods included from ExtensionParameters
Constructor Details
#initialize(pid, repository = nil, options = {}) ⇒ DigitalObject
Initialize a Rubydora::DigitalObject, which may or may not already exist in the data store.
Provides ‘after_initialize` callback for extensions
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rubydora/digital_object.rb', line 66 def initialize pid, repository = nil, = {} _run_initialize_callbacks do self.pid = pid @repository = repository .each do |key, value| self.send(:"#{key}=", value) end end end |
Instance Attribute Details
#pid ⇒ Object
Returns the value of attribute pid.
19 20 21 |
# File 'lib/rubydora/digital_object.rb', line 19 def pid @pid end |
Class Method Details
.create(pid, options = {}, repository = nil) ⇒ Object
create a new fedora object (see also DigitalObject#save)
51 52 53 54 55 |
# File 'lib/rubydora/digital_object.rb', line 51 def self.create pid, = {}, repository = nil repository ||= Rubydora.repository assigned_pid = repository.ingest(.merge(:pid => pid)) self.new assigned_pid, repository end |
.find(pid, repository = nil) ⇒ Object
find an existing fedora object TODO: raise an error if the object does not yet exist
43 44 45 |
# File 'lib/rubydora/digital_object.rb', line 43 def self.find pid, repository = nil self.new pid, repository end |
Instance Method Details
#datastreams ⇒ Array<Rubydora::Datastream> Also known as: datastream
List of datastreams
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rubydora/digital_object.rb', line 121 def datastreams @datastreams ||= begin h = Hash.new { |h,k| h[k] = Datastream.new self, k } begin datastreams_xml = repository.datastreams(:pid => pid) datastreams_xml.gsub! '<objectDatastreams', '<objectDatastreams xmlns="http://www.fedora.info/definitions/1/0/access/"' unless datastreams_xml =~ /xmlns=/ doc = Nokogiri::XML(datastreams_xml) doc.xpath('//access:datastream', {'access' => "http://www.fedora.info/definitions/1/0/access/"}).each do |ds| h[ds['dsid']] = Datastream.new self, ds['dsid'] end rescue RestClient::ResourceNotFound end h end end |
#delete ⇒ Rubydora::DigitalObject
Purge the object from Fedora
168 169 170 171 172 |
# File 'lib/rubydora/digital_object.rb', line 168 def delete repository.purge_object(:pid => pid) reset self end |
#fetch(dsid) ⇒ Object Also known as: []
provide an hash-like way to access datastreams
141 142 143 |
# File 'lib/rubydora/digital_object.rb', line 141 def fetch dsid datastreams[dsid] end |
#new? ⇒ Boolean
Does this object already exist?
87 88 89 |
# File 'lib/rubydora/digital_object.rb', line 87 def new? self.profile.empty? end |
#profile ⇒ Hash
Retrieve the object 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 112 113 114 115 116 117 |
# File 'lib/rubydora/digital_object.rb', line 93 def profile @profile ||= begin profile_xml = repository.object(:pid => pid) profile_xml.gsub! '<objectProfile', '<objectProfile xmlns="http://www.fedora.info/definitions/1/0/access/"' unless profile_xml =~ /xmlns=/ doc = Nokogiri::XML(profile_xml) h = doc.xpath('/access:objectProfile/*', {'access' => "http://www.fedora.info/definitions/1/0/access/"} ).inject({}) do |sum, node| sum[node.name] ||= [] sum[node.name] << node.text if node.name == "objModels" sum[node.name] = node.xpath('access:model', {'access' => "http://www.fedora.info/definitions/1/0/access/"}).map { |x| x.text } end sum end h.select { |key, value| value.length == 1 }.each do |key, value| next if key == "objModels" h[key] = value.first end h rescue {} end end |
#repository ⇒ Rubydora::Repository
repository reference from the digital object
176 177 178 |
# File 'lib/rubydora/digital_object.rb', line 176 def repository @repository ||= Rubydora.repository end |
#save ⇒ Rubydora::DigitalObject
persist the object to Fedora, either as a new object by modifing the existing object
also will save all ‘:dirty?` datastreams that already exist new datastreams must be directly saved
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/rubydora/digital_object.rb', line 153 def save if self.new? self.pid = repository.ingest to_api_params.merge(:pid => pid) else p = to_api_params repository.modify_object p.merge(:pid => pid) unless p.empty? end self.datastreams.select { |dsid, ds| ds.changed? }.reject {|dsid, ds| ds.new? }.each { |dsid, ds| ds.save } reset self end |
#uri ⇒ Object Also known as: fqpid
Return a full uri pid (for use in relations, etc
79 80 81 82 |
# File 'lib/rubydora/digital_object.rb', line 79 def uri return pid if pid =~ /.+\/.+/ "info:fedora/#{pid}" end |