rubydora 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
@@ -50,7 +50,7 @@ module Rubydora
50
50
  # Does this datastream already exist?
51
51
  # @return [Boolean]
52
52
  def new?
53
- profile.nil?
53
+ profile.empty?
54
54
  end
55
55
 
56
56
  # Retrieve the content of the datastream (and cache it)
@@ -81,9 +81,9 @@ module Rubydora
81
81
  def profile
82
82
  @profile ||= begin
83
83
  profile_xml = repository.datastream(:pid => pid, :dsid => dsid)
84
- profile_xml.gsub! '<datastreamProfile', '<datastreamProfile xmlns="http://www.fedora.info/definitions/1/0/access/"' unless profile_xml =~ /xmlns=/
84
+ profile_xml.gsub! '<datastreamProfile', '<datastreamProfile xmlns="http://www.fedora.info/definitions/1/0/management/"' unless profile_xml =~ /xmlns=/
85
85
  doc = Nokogiri::XML(profile_xml)
86
- h = doc.xpath('/access:datastreamProfile/*', {'access' => "http://www.fedora.info/definitions/1/0/access/"} ).inject({}) do |sum, node|
86
+ h = doc.xpath('/management:datastreamProfile/*', {'management' => "http://www.fedora.info/definitions/1/0/management/"} ).inject({}) do |sum, node|
87
87
  sum[node.name] ||= []
88
88
  sum[node.name] << node.text
89
89
  sum
@@ -94,7 +94,7 @@ module Rubydora
94
94
 
95
95
  h
96
96
  rescue
97
- nil
97
+ {}
98
98
  end
99
99
  end
100
100
 
@@ -45,8 +45,8 @@ module Rubydora
45
45
  # @param [Rubydora::Repository] context
46
46
  def self.create pid, options = {}, repository = nil
47
47
  repository ||= Rubydora.repository
48
- repository.ingest(options.merge(:pid => pid))
49
- DigitalObject.new pid, repository
48
+ assigned_pid = repository.ingest(options.merge(:pid => pid))
49
+ DigitalObject.new assigned_pid, repository
50
50
  end
51
51
 
52
52
  ##
@@ -59,7 +59,7 @@ module Rubydora
59
59
  # @param [Rubydora::Repository] repository context
60
60
  # @param [Hash] options default attribute values (used esp. for creating new datastreams
61
61
  def initialize pid, repository = nil, options = {}
62
- @pid = pid.gsub('info:fedora/', '')
62
+ self.pid = pid
63
63
  @repository = repository
64
64
 
65
65
  options.each do |key, value|
@@ -80,7 +80,7 @@ module Rubydora
80
80
  # Does this object already exist?
81
81
  # @return [Boolean]
82
82
  def new?
83
- self.profile.nil?
83
+ self.profile.empty?
84
84
  end
85
85
 
86
86
  # Retrieve the object profile as a hash (and cache it)
@@ -107,7 +107,7 @@ module Rubydora
107
107
 
108
108
  h
109
109
  rescue
110
- nil
110
+ {}
111
111
  end
112
112
  end
113
113
 
@@ -126,7 +126,12 @@ module Rubydora
126
126
  end
127
127
  end
128
128
  alias_method :datastream, :datastreams
129
- alias_method :[], :datastreams
129
+
130
+ # provide an hash-like way to access datastreams
131
+ def fetch dsid
132
+ datastreams[dsid]
133
+ end
134
+ alias_method :[], :fetch
130
135
 
131
136
  # persist the object to Fedora, either as a new object
132
137
  # by modifing the existing object
@@ -137,21 +142,22 @@ module Rubydora
137
142
  # @return [Rubydora::DigitalObject] a new copy of this object
138
143
  def save
139
144
  if self.new?
140
- repository.ingest to_api_params.merge(:pid => pid)
145
+ self.pid = repository.ingest to_api_params.merge(:pid => pid)
141
146
  else
142
147
  p = to_api_params
143
148
  repository.modify_object p.merge(:pid => pid) unless p.empty?
144
149
  end
145
150
 
146
151
  self.datastreams.select { |dsid, ds| ds.dirty? }.reject {|dsid, ds| ds.new? }.each { |dsid, ds| ds.save }
147
- DigitalObject.new(pid, repository)
152
+ reset
153
+ self
148
154
  end
149
155
 
150
156
  # Purge the object from Fedora
151
157
  # @return [Rubydora::DigitalObject] `self`
152
158
  def delete
153
159
  repository.purge_object(:pid => pid)
154
- reset_profile_attributes
160
+ reset
155
161
  self
156
162
  end
157
163
 
@@ -162,6 +168,13 @@ module Rubydora
162
168
  end
163
169
 
164
170
  protected
171
+ # set the pid of the object
172
+ # @param [String] pid
173
+ # @return [String] the base pid
174
+ def pid= pid
175
+ @pid = pid.gsub('info:fedora/', '')
176
+ end
177
+
165
178
  # datastream parameters
166
179
  # @return [Hash]
167
180
  def to_api_params
@@ -188,5 +201,17 @@ module Rubydora
188
201
  end
189
202
  end
190
203
 
204
+ # reset the datastreams cache
205
+ def reset_datastreams
206
+ @datastreams = nil
207
+ end
208
+
209
+ # reset local data so that it is requested from Fedora
210
+ def reset
211
+ reset_profile_attributes
212
+ reset_datastreams
213
+ self
214
+ end
215
+
191
216
  end
192
217
  end
@@ -31,6 +31,9 @@ module Rubydora::Ext
31
31
  end
32
32
  end
33
33
 
34
+ # convert a model string to a Ruby class (see ActiveSupport::Inflector#constantize)
35
+ # @param [String] lower_case_and_underscored_word
36
+ # @return [Module]
34
37
  def self.string_to_constant lower_case_and_underscored_word
35
38
  camel_cased_word = lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
36
39
 
@@ -82,16 +82,33 @@ module Rubydora
82
82
  diff[:-] ||= []
83
83
 
84
84
  diff[:+].each do |o|
85
- obj_uri = (( o.fqpid if o.respond_to? :fqpid ) || ( o.uri if o.respond_to? :uri ) || (o.to_s if o.respond_to? :to_s?) || o )
86
- repository.add_relationship :subject => fqpid, :predicate => predicate, :object => obj_uri
85
+ add_relationship(predicate, o)
87
86
  end
88
87
 
89
88
  diff[:-].each do |o|
90
- obj_uri = (( o.fqpid if o.respond_to? :fqpid ) || ( o.uri if o.respond_to? :uri ) || (o.to_s if o.respond_to? :to_s?) || o )
91
- repository.purge_relationship :subject => fqpid, :predicate => predicate, :object => obj_uri
89
+ purge_relationship(predicate, o)
92
90
  end
93
91
  end
94
92
 
93
+ # Add a relationship for this object
94
+ # @param [String] predicate
95
+ # @param [String, Rubydora::DigitalObject] object
96
+ # @return self
97
+ def add_relationship predicate, object
98
+ obj_uri = (( object.fqpid if object.respond_to? :fqpid ) || ( object.uri if object.respond_to? :uri ) || (object.to_s if object.respond_to? :to_s?) || object )
99
+ repository.add_relationship :subject => fqpid, :predicate => predicate, :object => obj_uri
100
+ self
101
+ end
102
+
103
+ # Purge a relationship from this object
104
+ # @param [String] predicate
105
+ # @param [String, Rubydora::DigitalObject] object
106
+ # @return self
107
+ def purge_relationship predicate, object
108
+ obj_uri = (( object.fqpid if object.respond_to? :fqpid ) || ( object.uri if object.respond_to? :uri ) || (object.to_s if object.respond_to? :to_s?) || object )
109
+ repository.purge_relationship :subject => fqpid, :predicate => predicate, :object => obj_uri
110
+ end
111
+
95
112
  # accessor to all retrieved relationships
96
113
  # @return [Hash]
97
114
  def relationships
@@ -21,6 +21,11 @@ module Rubydora
21
21
  DigitalObject.find(pid, self)
22
22
  end
23
23
 
24
+ # {include:DigitalObject.create}
25
+ def create pid, options = {}
26
+ DigitalObject.create(pid, options = {}, self)
27
+ end
28
+
24
29
  # repository profile (from API-A-LITE data)
25
30
  # @return [Hash]
26
31
  def profile
data/rubydora.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rubydora}
8
- s.version = "0.0.5"
8
+ s.version = "0.0.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Chris Beer"]
12
- s.date = %q{2011-04-21}
12
+ s.date = %q{2011-04-22}
13
13
  s.description = %q{Fedora Commons REST API ruby library : REQUIRES FCREPO 3.4+}
14
14
  s.email = %q{chris@cbeer.info}
15
15
  s.extra_rdoc_files = [
@@ -40,7 +40,7 @@ describe Rubydora::DigitalObject do
40
40
 
41
41
  it "should call ingest on save" do
42
42
  @object.should_receive(:datastreams).and_return({})
43
- @mock_repository.should_receive(:ingest).with(hash_including(:pid => 'pid'))
43
+ @mock_repository.should_receive(:ingest).with(hash_including(:pid => 'pid')).and_return('pid')
44
44
  @object.save
45
45
  end
46
46
  end
@@ -48,10 +48,18 @@ describe Rubydora::DigitalObject do
48
48
  describe "create" do
49
49
  it "should call the Fedora REST API to create a new object" do
50
50
  @mock_repository = mock(Rubydora::Repository)
51
- @mock_repository.should_receive(:ingest).with(instance_of(Hash)).and_return(nil)
51
+ @mock_repository.should_receive(:ingest).with(instance_of(Hash)).and_return("pid")
52
52
  obj = Rubydora::DigitalObject.create "pid", { :a => 1, :b => 2}, @mock_repository
53
53
  obj.should be_a_kind_of(Rubydora::DigitalObject)
54
54
  end
55
+
56
+ it "should return a new object with the Fedora response pid when no pid is provided" do
57
+ @mock_repository = mock(Rubydora::Repository)
58
+ @mock_repository.should_receive(:ingest).with(instance_of(Hash)).and_return("pid")
59
+ obj = Rubydora::DigitalObject.create "new", { :a => 1, :b => 2}, @mock_repository
60
+ obj.should be_a_kind_of(Rubydora::DigitalObject)
61
+ obj.pid.should == "pid"
62
+ end
55
63
  end
56
64
 
57
65
  describe "retreive" do
@@ -71,7 +79,7 @@ describe Rubydora::DigitalObject do
71
79
 
72
80
  it "should allow other datastreams to be added" do
73
81
  @mock_repository.should_receive(:datastreams).with(:pid => 'pid').and_return("<objectDatastreams><datastream dsid='a'></datastream>><datastream dsid='b'></datastream>><datastream dsid='c'></datastream></objectDatastreams>")
74
- @mock_repository.should_receive(:datastream).with(:pid => 'pid', :dsid => 'z').and_raise("")
82
+ @mock_repository.should_receive(:datastream).with(:pid => 'pid', :dsid => 'z').and_raise(RestClient::ResourceNotFound)
75
83
 
76
84
  @object.datastreams.length.should == 3
77
85
 
@@ -81,6 +89,13 @@ describe Rubydora::DigitalObject do
81
89
 
82
90
  @object.datastreams.length.should == 4
83
91
  end
92
+
93
+ it "should let datastreams be accessed via hash notation" do
94
+ @mock_repository.should_receive(:datastreams).with(:pid => 'pid').and_return("<objectDatastreams><datastream dsid='a'></datastream>><datastream dsid='b'></datastream>><datastream dsid='c'></datastream></objectDatastreams>")
95
+
96
+ @object['a'].should be_a_kind_of(Rubydora::Datastream)
97
+ @object['a'].should == @object.datastreams['a']
98
+ end
84
99
 
85
100
  end
86
101
 
@@ -95,8 +110,10 @@ describe Rubydora::DigitalObject do
95
110
  @mock_repository = mock(Rubydora::Repository)
96
111
  @mock_repository.should_receive(:object).any_number_of_times.with({:pid => 'pid'}).and_return <<-XML
97
112
  <objectProfile>
113
+ <not>empty</not>
98
114
  </objectProfile>
99
115
  XML
116
+
100
117
  @object = Rubydora::DigitalObject.new 'pid', @mock_repository
101
118
  end
102
119
 
@@ -124,6 +141,14 @@ describe Rubydora::DigitalObject do
124
141
  @mock_repository.should_receive(:modify_object).with(hash_including(:pid => 'pid'))
125
142
  @object.save
126
143
  end
144
+
145
+ it "should reset the object state on save" do
146
+ @object.label = "asdf"
147
+ @object.should_receive(:datastreams).and_return({})
148
+ @mock_repository.should_receive(:modify_object).with(hash_including(:pid => 'pid'))
149
+ @object.profile.should_not be_nil
150
+ expect { @object.save }.to change { @object.instance_variable_get('@profile') }.to nil
151
+ end
127
152
  end
128
153
 
129
154
  describe "delete" do
@@ -138,7 +163,6 @@ describe Rubydora::DigitalObject do
138
163
  end
139
164
  end
140
165
 
141
-
142
166
  describe "models" do
143
167
  before(:each) do
144
168
  @mock_repository = mock(Rubydora::Repository)
@@ -15,7 +15,7 @@ describe "Integration testing against a live Fedora repository" do
15
15
  it "should create an object" do
16
16
  obj = @repository.find('test:1')
17
17
  obj.new?.should == true
18
- obj = obj.save
18
+ obj.save
19
19
  obj.new?.should == false
20
20
  end
21
21
 
@@ -52,7 +52,7 @@ describe "Integration testing against a live Fedora repository" do
52
52
 
53
53
  it "should create a managed datastream" do
54
54
  obj = @repository.find('test:1')
55
- obj = obj.save
55
+ obj.save
56
56
  ds = obj.datastreams["Test"]
57
57
 
58
58
  ds.content = open(__FILE__).read
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubydora
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Chris Beer
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-21 00:00:00 -04:00
18
+ date: 2011-04-22 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency