rubydora 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +25 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +77 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/lib/rubydora/array_with_callback.rb +34 -0
- data/lib/rubydora/callbacks.rb +49 -0
- data/lib/rubydora/datastream.rb +152 -0
- data/lib/rubydora/digital_object.rb +190 -0
- data/lib/rubydora/ext/solr.rb +103 -0
- data/lib/rubydora/ext.rb +5 -0
- data/lib/rubydora/extension_parameters.rb +40 -0
- data/lib/rubydora/models_mixin.rb +28 -0
- data/lib/rubydora/relationships_mixin.rb +101 -0
- data/lib/rubydora/repository.rb +82 -0
- data/lib/rubydora/resource_index.rb +43 -0
- data/lib/rubydora/rest_api_client/v33.rb +36 -0
- data/lib/rubydora/rest_api_client.rb +267 -0
- data/lib/rubydora.rb +46 -0
- data/rubydora.gemspec +104 -0
- data/spec/datastream_spec.rb +100 -0
- data/spec/digital_object_spec.rb +260 -0
- data/spec/ext_solr_spec.rb +70 -0
- data/spec/integration_test_spec.rb +93 -0
- data/spec/repository_spec.rb +85 -0
- data/spec/resource_index_spec.rb +28 -0
- data/spec/rest_api_client_spec.rb +146 -0
- data/spec/spec_helper.rb +4 -0
- metadata +245 -0
data/Gemfile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gem "fastercsv"
|
4
|
+
gem "rest-client"
|
5
|
+
gem "nokogiri"
|
6
|
+
gem "mime-types"
|
7
|
+
|
8
|
+
group :soap do
|
9
|
+
gem "soap4r"
|
10
|
+
end
|
11
|
+
|
12
|
+
# Add dependencies required to use your gem here.
|
13
|
+
# Example:
|
14
|
+
# gem "activesupport", ">= 2.3.5"
|
15
|
+
|
16
|
+
# Add dependencies to develop your gem here.
|
17
|
+
# Include everything needed to run rake, tests, features, etc.
|
18
|
+
group :development do
|
19
|
+
gem "shoulda", ">= 0"
|
20
|
+
gem "bundler", "~> 1.0.0"
|
21
|
+
gem "jeweler", "~> 1.5.1"
|
22
|
+
gem "rcov", ">= 0"
|
23
|
+
gem "rspec"
|
24
|
+
gem "yard"
|
25
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Chris Beer
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
= rubydora
|
2
|
+
|
3
|
+
Rubydora is a low-level Fedora Commons REST API consumer, providing direct access to REST API methods, as well as a primitive ruby abstraction.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install rubydora
|
8
|
+
|
9
|
+
== Examples
|
10
|
+
|
11
|
+
> repo = Rubydora.connect :url => 'http://localhost:8983/fedora', :user => 'fedoraAdmin', :password => 'fedoraAdmin'
|
12
|
+
=> #<Rubydora::Repository:0x101859538 @config={:url=>"http://localhost:8983/fedora", :user=>"fedoraAdmin", :password=>"fedoraAdmin"}>
|
13
|
+
|
14
|
+
> obj = repo.find('test:1')
|
15
|
+
=> #<Rubydora::DigitalObject:0x101977230 @pid="test:1", @repository=#<Rubydora::Repository:0x1019beef0 @config={:user=>"fedoraAdmin", :url=>"http://localhost:8983/fedora", :password=>"fedora"}>>
|
16
|
+
|
17
|
+
> obj.new?
|
18
|
+
=> true
|
19
|
+
|
20
|
+
> obj = obj.save
|
21
|
+
=> #<Rubydora::DigitalObject:0x1017601b8 @pid="test:1", @repository=#<Rubydora::Repository:0x1018e3058 @config={:url=>"http://localhost:8983/fedora", :user=>"fedoraAdmin", :password=>"fedoraAdmin"}, @client=#<RestClient::Resource:0x101882910 @options={:user=>"fedoraAdmin", :password=>"fedoraAdmin"}, @block=nil, @url="http://localhost:8983/fedora">>>
|
22
|
+
|
23
|
+
> obj.profile
|
24
|
+
=> {"objDissIndexViewURL"=>"http://localhost:8983/fedora/get/test:1/fedora-system:3/viewMethodIndex", "objLabel"=>"", "objModels"=>"info:fedora/fedora-system:FedoraObject-3.0", "objCreateDate"=>"2011-04-18T13:34:11.285Z", "objOwnerId"=>"fedoraAdmin", "objState"=>"A", "objItemIndexViewURL"=>"http://localhost:8983/fedora/get/test:1/fedora-system:3/viewItemIndex", "objLastModDate"=>"2011-04-18T13:47:30.110Z"}
|
25
|
+
|
26
|
+
> obj.models
|
27
|
+
=> ["info:fedora/fedora-system:FedoraObject-3.0"]
|
28
|
+
|
29
|
+
> obj.models << 'info:fedora/test:cmodel'
|
30
|
+
=> ["info:fedora/fedora-system:FedoraObject-3.0", "info:fedora/test:cmodel"]
|
31
|
+
|
32
|
+
> obj2 = repo.find('test:2')
|
33
|
+
=> [...]
|
34
|
+
|
35
|
+
> obj1.parts << obj2
|
36
|
+
=> [...]
|
37
|
+
|
38
|
+
> obj.datastreams
|
39
|
+
=> {"DC"=>#<Rubydora::Datastream:0x101860180 @dsid="DC" ...> }
|
40
|
+
|
41
|
+
> ds = obj.datastreams['File']
|
42
|
+
=> #<Rubydora::Datastream:0x1017f26a8 @dsid="File" ...>
|
43
|
+
> ds.controlGroup = 'R'
|
44
|
+
=> "R"
|
45
|
+
> ds.dsLocation = 'http://example.org/index.html'
|
46
|
+
=> "http://example.org/index.html"
|
47
|
+
> ds.dsLabel = 'Example redirect datastream'
|
48
|
+
=> "Example redirect datastream"
|
49
|
+
> ds.mimeType = 'text/html'
|
50
|
+
=> "text/html"
|
51
|
+
> ds.save
|
52
|
+
=> #<Rubydora::Datastream:0x10177a568 @dsid="File" ...>
|
53
|
+
|
54
|
+
> obj.datastreams
|
55
|
+
=> {"DC"=>#<Rubydora::Datastream:0x101860180 @dsid="DC" ..., "File"=>#<Rubydora::Datastream:0x10177a568 @dsid="File" ...>}
|
56
|
+
|
57
|
+
> obj.datastreams["File"].delete
|
58
|
+
=> true
|
59
|
+
> obj.datastreams["File"].new?
|
60
|
+
=> true
|
61
|
+
|
62
|
+
|
63
|
+
== Contributing to rubydora
|
64
|
+
|
65
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
66
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
67
|
+
* Fork the project
|
68
|
+
* Start a feature/bugfix branch
|
69
|
+
* Commit and push until you are happy with your contribution
|
70
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
71
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
72
|
+
|
73
|
+
== Copyright
|
74
|
+
|
75
|
+
Copyright (c) 2011 Chris Beer. See LICENSE.txt for
|
76
|
+
further details.
|
77
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "rubydora"
|
16
|
+
gem.homepage = "http://github.com/cbeer/rubydora"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Fedora Commons REST API ruby library}
|
19
|
+
gem.description = %Q{Fedora Commons REST API ruby library : REQUIRES FCREPO 3.4+}
|
20
|
+
gem.email = "chris@cbeer.info"
|
21
|
+
gem.authors = ["Chris Beer"]
|
22
|
+
end
|
23
|
+
Jeweler::RubygemsDotOrgTasks.new
|
24
|
+
|
25
|
+
# Get your spec rake tasks working in RSpec 2.0
|
26
|
+
|
27
|
+
require 'rspec/core/rake_task'
|
28
|
+
|
29
|
+
desc 'Default: run specs.'
|
30
|
+
task :default => :spec
|
31
|
+
|
32
|
+
desc "Run specs"
|
33
|
+
RSpec::Core::RakeTask.new do |t|
|
34
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
35
|
+
# Put spec opts in a file named .rspec in root
|
36
|
+
end
|
37
|
+
|
38
|
+
desc "Generate code coverage"
|
39
|
+
require "rcov/rcovtask"
|
40
|
+
RSpec::Core::RakeTask.new(:coverage) do |t|
|
41
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
42
|
+
t.rcov = true
|
43
|
+
t.rcov_opts = ['--exclude', 'spec', '--exclude', 'gems/*']
|
44
|
+
end
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "rubydora #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.2
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Rubydora
|
2
|
+
##
|
3
|
+
# This is an attempt to implement an Array-like
|
4
|
+
# object that calls a method after data is modified
|
5
|
+
class ArrayWithCallback < Array
|
6
|
+
##
|
7
|
+
# FIXME: It would be nice to use Rubydora::Callbacks here,
|
8
|
+
# however, this method requires instance-level callbacks
|
9
|
+
|
10
|
+
[:<<, :collect!, :map!, :compact!, :concat, :delete, :delete_at, :delete_if, :pop, :push, :reject!, :replace, :select!, :[]=, :slice!, :uniq! ].each do |method|
|
11
|
+
class_eval <<-RUBY
|
12
|
+
def #{method.to_s} *args, &blk
|
13
|
+
old = self.dup
|
14
|
+
res = super(*args, &blk)
|
15
|
+
call_on_change({:+ => self - old, :- => old - self})
|
16
|
+
res
|
17
|
+
end
|
18
|
+
RUBY
|
19
|
+
end
|
20
|
+
|
21
|
+
# duck-typing Rubydora::Callbacks @hooks and accessor
|
22
|
+
def on_change
|
23
|
+
@hooks ||= {}
|
24
|
+
@hooks[:on_change] ||= []
|
25
|
+
end
|
26
|
+
|
27
|
+
# duck-typing Rubydora::Callbacks call_* methods
|
28
|
+
def call_on_change changes = {}
|
29
|
+
self.on_change.each do |h|
|
30
|
+
h.call(self, changes)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Rubydora
|
2
|
+
|
3
|
+
# Provides class level methods for handling
|
4
|
+
# callback methods that alter object instances
|
5
|
+
module Callbacks
|
6
|
+
# add callback framework to base class
|
7
|
+
# @param [Class] base
|
8
|
+
def self.included(base)
|
9
|
+
base.extend ExtendableClassMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
# hooks support
|
13
|
+
module ExtendableClassMethods
|
14
|
+
# creates the @hooks container ("hooks" are blocks or procs).
|
15
|
+
# returns an array
|
16
|
+
def hooks
|
17
|
+
@hooks ||= {}
|
18
|
+
end
|
19
|
+
|
20
|
+
# register callback procs
|
21
|
+
# @param [Array<Symbol>] hook name
|
22
|
+
def register_callback *attrs
|
23
|
+
attrs.each do |method_name|
|
24
|
+
next if methods.include? method_name.to_s
|
25
|
+
instance_eval %Q{
|
26
|
+
def #{method_name}(&blk)
|
27
|
+
self.hooks[:#{method_name}] ||= []
|
28
|
+
self.hooks[:#{method_name}] << blk
|
29
|
+
end
|
30
|
+
|
31
|
+
def clear_#{method_name}_blocks!
|
32
|
+
self.hooks[:#{method_name}] = []
|
33
|
+
end
|
34
|
+
}
|
35
|
+
|
36
|
+
class_eval %Q{
|
37
|
+
def call_#{method_name}
|
38
|
+
self.class.hooks[:#{method_name}] ||= []
|
39
|
+
self.class.hooks[:#{method_name}].each do |h|
|
40
|
+
instance_eval &h
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
module Rubydora
|
2
|
+
# This class represents a Fedora datastream object
|
3
|
+
# and provides helper methods for creating and manipulating
|
4
|
+
# them.
|
5
|
+
class Datastream
|
6
|
+
include Rubydora::Callbacks
|
7
|
+
register_callback :after_initialize
|
8
|
+
include Rubydora::ExtensionParameters
|
9
|
+
|
10
|
+
attr_reader :digital_object, :dsid
|
11
|
+
|
12
|
+
# mapping datastream attributes (and api parameters) to datastream profile names
|
13
|
+
DS_ATTRIBUTES = {: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, :file => nil}
|
14
|
+
|
15
|
+
# accessors for datastream attributes
|
16
|
+
DS_ATTRIBUTES.each do |attribute, profile_name|
|
17
|
+
class_eval %Q{
|
18
|
+
def #{attribute.to_s}
|
19
|
+
@#{attribute.to_s} || profile['#{profile_name.to_s}']
|
20
|
+
end
|
21
|
+
|
22
|
+
attr_writer :#{attribute.to_s}
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Initialize a Rubydora::Datastream object, which may or
|
28
|
+
# may not already exist in the datastore.
|
29
|
+
#
|
30
|
+
# Provides `after_initialize` callback for extensions
|
31
|
+
#
|
32
|
+
# @param [Rubydora::DigitalObject]
|
33
|
+
# @param [String] Datastream ID
|
34
|
+
# @param [Hash] default attribute values (used esp. for creating new datastreams
|
35
|
+
def initialize digital_object, dsid, options = {}
|
36
|
+
@digital_object = digital_object
|
37
|
+
@dsid = dsid
|
38
|
+
options.each do |key, value|
|
39
|
+
self.send(:"#{key}=", value)
|
40
|
+
end
|
41
|
+
|
42
|
+
call_after_initialize
|
43
|
+
end
|
44
|
+
|
45
|
+
# Does this datastream already exist?
|
46
|
+
# @return [Boolean]
|
47
|
+
def new?
|
48
|
+
profile.nil?
|
49
|
+
end
|
50
|
+
|
51
|
+
# Retrieve the content of the datastream (and cache it)
|
52
|
+
# @return [String]
|
53
|
+
def content
|
54
|
+
@content ||= repository.datastream_dissemination :pid => digital_object.pid, :dsid => dsid
|
55
|
+
end
|
56
|
+
alias_method :read, :content
|
57
|
+
|
58
|
+
def content= content
|
59
|
+
@file = content
|
60
|
+
@content = content.dup
|
61
|
+
@content &&= @content.read if @content.respond_to? :read
|
62
|
+
@content &&= @content.to_s if @content.respond_to? :read
|
63
|
+
end
|
64
|
+
|
65
|
+
# Retrieve the datastream profile as a hash (and cache it)
|
66
|
+
# @return [Hash] see Fedora #getDatastream documentation for keys
|
67
|
+
def profile
|
68
|
+
@profile ||= begin
|
69
|
+
profile_xml = repository.datastream(:pid => digital_object.pid, :dsid => dsid)
|
70
|
+
profile_xml.gsub! '<datastreamProfile', '<datastreamProfile xmlns="http://www.fedora.info/definitions/1/0/access/"' unless profile_xml =~ /xmlns=/
|
71
|
+
doc = Nokogiri::XML(profile_xml)
|
72
|
+
h = doc.xpath('/access:datastreamProfile/*', {'access' => "http://www.fedora.info/definitions/1/0/access/"} ).inject({}) do |sum, node|
|
73
|
+
sum[node.name] ||= []
|
74
|
+
sum[node.name] << node.text
|
75
|
+
sum
|
76
|
+
end
|
77
|
+
h.select { |key, value| value.length == 1 }.each do |key, value|
|
78
|
+
h[key] = value.first
|
79
|
+
end
|
80
|
+
|
81
|
+
h
|
82
|
+
rescue
|
83
|
+
nil
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# Has this datastream been modified, but not yet saved?
|
88
|
+
# @return [Boolean]
|
89
|
+
def dirty?
|
90
|
+
DS_ATTRIBUTES.any? { |attribute, profile_name| instance_variable_defined?("@#{attribute.to_s}") } || new?
|
91
|
+
end
|
92
|
+
|
93
|
+
# Add datastream to Fedora
|
94
|
+
# @return [Rubydora::Datastream]
|
95
|
+
def create
|
96
|
+
repository.add_datastream to_api_params.merge({ :pid => digital_object.pid, :dsid => dsid })
|
97
|
+
reset_profile_attributes
|
98
|
+
Datastream.new(digital_object, dsid)
|
99
|
+
end
|
100
|
+
|
101
|
+
# Modify or save the datastream
|
102
|
+
# @return [Rubydora::Datastream]
|
103
|
+
def save
|
104
|
+
return create if new?
|
105
|
+
repository.modify_datastream to_api_params.merge({ :pid => digital_object.pid, :dsid => dsid })
|
106
|
+
reset_profile_attributes
|
107
|
+
Datastream.new(digital_object, dsid)
|
108
|
+
end
|
109
|
+
|
110
|
+
# Purge the datastream from Fedora
|
111
|
+
# @return [Rubydora::Datastream] `self`
|
112
|
+
def delete
|
113
|
+
repository.purge_datastream(:pid => digital_object.pid, :dsid => dsid) unless self.new?
|
114
|
+
digital_object.datastreams.delete(dsid)
|
115
|
+
reset_profile_attributes
|
116
|
+
self
|
117
|
+
end
|
118
|
+
|
119
|
+
protected
|
120
|
+
# datastream parameters
|
121
|
+
# @return [Hash]
|
122
|
+
def to_api_params
|
123
|
+
h = default_api_params
|
124
|
+
DS_ATTRIBUTES.each do |attribute, profile_name|
|
125
|
+
h[attribute] = instance_variable_get("@#{attribute.to_s}") if instance_variable_defined?("@#{attribute.to_s}")
|
126
|
+
end
|
127
|
+
|
128
|
+
h
|
129
|
+
end
|
130
|
+
|
131
|
+
# default datastream parameters
|
132
|
+
# @return [Hash]
|
133
|
+
def default_api_params
|
134
|
+
{ :controlGroup => 'M', :dsState => 'A', :checksumType => 'DISABLED', :versionable => true}
|
135
|
+
end
|
136
|
+
|
137
|
+
# reset all profile attributes
|
138
|
+
# @return [Hash]
|
139
|
+
def reset_profile_attributes
|
140
|
+
@profile = nil
|
141
|
+
DS_ATTRIBUTES.each do |attribute, profile_name|
|
142
|
+
instance_variable_set("@#{attribute.to_s}", nil) if instance_variable_defined?("@#{attribute.to_s}")
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
# repository reference from the digital object
|
147
|
+
# @return [Rubydora::Repository]
|
148
|
+
def repository
|
149
|
+
digital_object.repository
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
module Rubydora
|
2
|
+
|
3
|
+
# This class represents a Fedora object and provides
|
4
|
+
# helpers for managing attributes, datastreams, and
|
5
|
+
# relationships.
|
6
|
+
#
|
7
|
+
# Using the extension framework, implementors may
|
8
|
+
# provide additional functionality to this base
|
9
|
+
# implementation.
|
10
|
+
class DigitalObject
|
11
|
+
include Rubydora::Callbacks
|
12
|
+
register_callback :after_initialize
|
13
|
+
include Rubydora::ExtensionParameters
|
14
|
+
include Rubydora::ModelsMixin
|
15
|
+
include Rubydora::RelationshipsMixin
|
16
|
+
|
17
|
+
|
18
|
+
attr_reader :pid
|
19
|
+
|
20
|
+
# mapping object parameters to profile elements
|
21
|
+
OBJ_ATTRIBUTES = {:state => :objState, :ownerId => :objOwnerId, :label => :objLabel, :logMessage => nil, :lastModifiedDate => :objLastModDate }
|
22
|
+
|
23
|
+
OBJ_ATTRIBUTES.each do |attribute, profile_name|
|
24
|
+
class_eval <<-RUBY
|
25
|
+
def #{attribute.to_s}
|
26
|
+
@#{attribute.to_s} || profile['#{profile_name.to_s}']
|
27
|
+
end
|
28
|
+
|
29
|
+
attr_writer :#{attribute.to_s}
|
30
|
+
|
31
|
+
RUBY
|
32
|
+
end
|
33
|
+
|
34
|
+
# find an existing fedora object
|
35
|
+
# TODO: raise an error if the object does not yet exist
|
36
|
+
# @param [String] pid
|
37
|
+
# @param [Rubydora::Repository] context
|
38
|
+
def self.find pid, repository = nil
|
39
|
+
DigitalObject.new pid, repository
|
40
|
+
end
|
41
|
+
|
42
|
+
# create a new fedora object (see also DigitalObject#save)
|
43
|
+
# @param [String] pid
|
44
|
+
# @param [Hash] options
|
45
|
+
# @param [Rubydora::Repository] context
|
46
|
+
def self.create pid, options = {}, repository = nil
|
47
|
+
repository ||= Rubydora.repository
|
48
|
+
repository.ingest(options.merge(:pid => pid))
|
49
|
+
DigitalObject.new pid, repository
|
50
|
+
end
|
51
|
+
|
52
|
+
##
|
53
|
+
# Initialize a Rubydora::DigitalObject, which may or
|
54
|
+
# may not already exist in the data store.
|
55
|
+
#
|
56
|
+
# Provides `after_initialize` callback for extensions
|
57
|
+
#
|
58
|
+
# @param [String] pid
|
59
|
+
# @param [Rubydora::Repository] repository context
|
60
|
+
# @param [Hash] options default attribute values (used esp. for creating new datastreams
|
61
|
+
def initialize pid, repository = nil, options = {}
|
62
|
+
@pid = pid
|
63
|
+
@repository = repository
|
64
|
+
|
65
|
+
options.each do |key, value|
|
66
|
+
self.send(:"#{key}=", value)
|
67
|
+
end
|
68
|
+
|
69
|
+
call_after_initialize
|
70
|
+
end
|
71
|
+
|
72
|
+
##
|
73
|
+
# Return a full uri pid (for use in relations, etc
|
74
|
+
def uri
|
75
|
+
return pid if pid =~ /.+\/.+/
|
76
|
+
"info:fedora/#{pid}"
|
77
|
+
end
|
78
|
+
alias_method :fqpid, :uri
|
79
|
+
|
80
|
+
# Does this object already exist?
|
81
|
+
# @return [Boolean]
|
82
|
+
def new?
|
83
|
+
self.profile.nil?
|
84
|
+
end
|
85
|
+
|
86
|
+
# Retrieve the object profile as a hash (and cache it)
|
87
|
+
# @return [Hash] see Fedora #getObject documentation for keys
|
88
|
+
def profile
|
89
|
+
@profile ||= begin
|
90
|
+
profile_xml = repository.object(:pid => pid)
|
91
|
+
profile_xml.gsub! '<objectProfile', '<objectProfile xmlns="http://www.fedora.info/definitions/1/0/access/"' unless profile_xml =~ /xmlns=/
|
92
|
+
doc = Nokogiri::XML(profile_xml)
|
93
|
+
h = doc.xpath('/access:objectProfile/*', {'access' => "http://www.fedora.info/definitions/1/0/access/"} ).inject({}) do |sum, node|
|
94
|
+
sum[node.name] ||= []
|
95
|
+
sum[node.name] << node.text
|
96
|
+
|
97
|
+
if node.name == "objModels"
|
98
|
+
sum[node.name] = node.xpath('access:model', {'access' => "http://www.fedora.info/definitions/1/0/access/"}).map { |x| x.text }
|
99
|
+
end
|
100
|
+
|
101
|
+
sum
|
102
|
+
end
|
103
|
+
h.select { |key, value| value.length == 1 }.each do |key, value|
|
104
|
+
next if key == "objModels"
|
105
|
+
h[key] = value.first
|
106
|
+
end
|
107
|
+
|
108
|
+
h
|
109
|
+
rescue
|
110
|
+
nil
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# List of datastreams
|
115
|
+
# @return [Array<Rubydora::Datastream>]
|
116
|
+
def datastreams
|
117
|
+
@datastreams ||= begin
|
118
|
+
h = Hash.new { |h,k| h[k] = Datastream.new self, k }
|
119
|
+
datastreams_xml = repository.datastreams(:pid => pid)
|
120
|
+
datastreams_xml.gsub! '<objectDatastreams', '<objectDatastreams xmlns="http://www.fedora.info/definitions/1/0/access/"' unless datastreams_xml =~ /xmlns=/
|
121
|
+
doc = Nokogiri::XML(datastreams_xml)
|
122
|
+
doc.xpath('//access:datastream', {'access' => "http://www.fedora.info/definitions/1/0/access/"}).each { |ds| h[ds['dsid']] = Datastream.new self, ds['dsid'] }
|
123
|
+
h
|
124
|
+
rescue RestClient::ResourceNotFound
|
125
|
+
h = Hash.new { |h,k| h[k] = Datastream.new self, k }
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# persist the object to Fedora, either as a new object
|
130
|
+
# by modifing the existing object
|
131
|
+
#
|
132
|
+
# also will save all `:dirty?` datastreams that already exist
|
133
|
+
# new datastreams must be directly saved
|
134
|
+
#
|
135
|
+
# @return [Rubydora::DigitalObject] a new copy of this object
|
136
|
+
def save
|
137
|
+
if self.new?
|
138
|
+
repository.ingest to_api_params.merge(:pid => pid)
|
139
|
+
else
|
140
|
+
p = to_api_params
|
141
|
+
repository.modify_object p.merge(:pid => pid) unless p.empty?
|
142
|
+
end
|
143
|
+
|
144
|
+
self.datastreams.select { |dsid, ds| ds.dirty? }.reject {|dsid, ds| ds.new? }.each { |dsid, ds| ds.save }
|
145
|
+
DigitalObject.new(pid, repository)
|
146
|
+
end
|
147
|
+
|
148
|
+
# Purge the object from Fedora
|
149
|
+
# @return [Rubydora::DigitalObject] `self`
|
150
|
+
def delete
|
151
|
+
repository.purge_object(:pid => pid)
|
152
|
+
reset_profile_attributes
|
153
|
+
self
|
154
|
+
end
|
155
|
+
|
156
|
+
# repository reference from the digital object
|
157
|
+
# @return [Rubydora::Repository]
|
158
|
+
def repository
|
159
|
+
@repository ||= Rubydora.repository
|
160
|
+
end
|
161
|
+
|
162
|
+
protected
|
163
|
+
# datastream parameters
|
164
|
+
# @return [Hash]
|
165
|
+
def to_api_params
|
166
|
+
h = default_api_params
|
167
|
+
OBJ_ATTRIBUTES.each do |attribute, profile_name|
|
168
|
+
h[attribute] = instance_variable_get("@#{attribute.to_s}") if instance_variable_defined?("@#{attribute.to_s}")
|
169
|
+
end
|
170
|
+
|
171
|
+
h
|
172
|
+
end
|
173
|
+
|
174
|
+
# default datastream parameters
|
175
|
+
# @return [Hash]
|
176
|
+
def default_api_params
|
177
|
+
{ }
|
178
|
+
end
|
179
|
+
|
180
|
+
# reset all profile attributes
|
181
|
+
# @return [Hash]
|
182
|
+
def reset_profile_attributes
|
183
|
+
@profile = nil
|
184
|
+
OBJ_ATTRIBUTES.each do |attribute, profile_name|
|
185
|
+
instance_variable_set("@#{attribute.to_s}", nil) if instance_variable_defined?("@#{attribute.to_s}")
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
190
|
+
end
|