active-triples 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 954d837104d6d548331e0626a0aef47ad302d815
4
- data.tar.gz: 85c28d7bcfaee8ea84d3f6210106f8544efacab1
3
+ metadata.gz: 0d6cf7f26813ed44293a6b7203b38086e3b575d5
4
+ data.tar.gz: 0810ad91e32b13f6130abc1e31e56284157db424
5
5
  SHA512:
6
- metadata.gz: dac57c51c274344dac11cdab92679f78e9547cd12ac579c9c4849164f95fda0ecbeec3a83c4648a4328cd63a2a6f36d0c5f4bc386d534882f1cbd894fcd7abf3
7
- data.tar.gz: cd941623441fef977ac8e334266fd3a641d6465ce4c5d48ad2d49f083d66e57ab6168a87fe038f2b0ae774821b46c526ff7f442758a3128bb69f9d61007d81f3
6
+ metadata.gz: 40d43a979b77e9c591e584d7d5a61717990ce62c4f5dce67b8ebeee2806b12a027e7569eb88e2a3d7f9b19c793c8518dc9fe1e98d9c0f973055be3085d231f27
7
+ data.tar.gz: 3511436cbfac29ef7b52021786eff8bf3ba157f5b822e70939e23dfff72026a3951d39abc8e0ceb102aa5b00a826861f4d6f6ac6189955c847f8ea9d6c8bf766
@@ -10,7 +10,7 @@ module ActiveTriples
10
10
  extend Configurable
11
11
  extend Properties
12
12
 
13
- delegate :rdf_subject, :mark_for_destruction, :marked_for_destruction?, :set_value, :get_values, :parent, :dump, :attributes=, to: :resource
13
+ delegate :rdf_subject, :mark_for_destruction, :marked_for_destruction?, :set_value, :get_values, :parent, :type, :dump, :attributes=, to: :resource
14
14
  alias_method :to_ary, :to_a
15
15
 
16
16
  class << self
@@ -34,10 +34,21 @@ module ActiveTriples
34
34
  graph.singleton_class.properties.keys.each do |property|
35
35
  graph.singleton_class.send(:register_property, property)
36
36
  end
37
- graph.insert RDF::Statement.new(subject, RDF.type, RDF.List)
38
37
  graph.reload
39
38
  end
40
39
 
40
+ def clear
41
+ graph.send :erase_old_resource
42
+ parent = graph.parent
43
+ old_subject = subject
44
+ super
45
+ @subject = old_subject
46
+ @graph = ListResource.new(subject)
47
+ graph << parent if parent
48
+ graph.parent = parent
49
+ graph.list = self
50
+ end
51
+
41
52
  def []=(idx, value)
42
53
  raise IndexError "index #{idx} too small for array: minimum 0" if idx < 0
43
54
 
@@ -148,8 +159,15 @@ module ActiveTriples
148
159
  when Array then RDF::List.new(nil, graph, value)
149
160
  else value
150
161
  end
162
+
163
+ if subject == RDF.nil
164
+ @subject = RDF::Node.new
165
+ @graph = ListResource.new(subject)
166
+ @graph.type = RDF.List
167
+ end
151
168
 
152
169
  if empty?
170
+ @graph.type = RDF.List
153
171
  resource.set_value(RDF.first, value)
154
172
  resource.insert([subject, RDF.rest, RDF.nil])
155
173
  resource << value if value.kind_of? Resource
@@ -1,3 +1,3 @@
1
1
  module ActiveTriples
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -3,118 +3,186 @@ require 'nokogiri'
3
3
  require 'linkeddata'
4
4
 
5
5
  describe ActiveTriples::List do
6
- before :each do
7
- class MADS < RDF::Vocabulary("http://www.loc.gov/mads/rdf/v1#")
8
- property :complexSubject
9
- property :authoritativeLabel
10
- property :elementList
11
- property :elementValue
12
- property :TopicElement
13
- property :TemporalElement
6
+
7
+ subject { ActiveTriples::List.new }
8
+
9
+ context 'when empty' do
10
+ it 'has subject of RDF.nil' do
11
+ expect(subject.subject).to eq RDF.nil
14
12
  end
15
- class DemoList < ActiveTriples::Resource
16
- property :elementList, :predicate => MADS.elementList, :class_name => 'DemoList::List'
17
- class List < ActiveTriples::List
18
- property :topicElement, :predicate => MADS.TopicElement, :class_name => 'DemoList::List::TopicElement'
19
- property :temporalElement, :predicate => MADS.TemporalElement, :class_name => 'DemoList::List::TemporalElement'
20
-
21
- class TopicElement < ActiveTriples::Resource
22
- configure :type => MADS.TopicElement
23
- property :elementValue, :predicate => MADS.elementValue
24
- end
25
- class TemporalElement < ActiveTriples::Resource
26
- configure :type => MADS.TemporalElement
27
- property :elementValue, :predicate => MADS.elementValue
28
- end
29
- end
13
+
14
+ it 'has no statements' do
15
+ expect(subject.statements.size).to eq 0
16
+ end
17
+
18
+ it 'knows it is empty' do
19
+ expect(subject.size).to eq 0
20
+ expect(subject).to be_empty
30
21
  end
31
- end
32
- after(:each) do
33
- Object.send(:remove_const, :DemoList)
34
- Object.send(:remove_const, :MADS)
35
22
  end
36
23
 
37
- describe "a new list" do
38
- let (:ds) { DemoList.new('http://example.org/foo') }
39
- subject { ds.elementList.build}
24
+ context 'with elements' do
25
+ before do
26
+ subject << 1
27
+ subject << 2
28
+ subject << 3
29
+ end
40
30
 
41
- it "should insert at the end" do
42
- expect(subject).to be_kind_of DemoList::List
43
- expect(subject.size).to eq 0
44
- subject[1] = DemoList::List::TopicElement.new
45
- expect(subject.size).to eq 2
31
+ it 'has a non-nil subject' do
32
+ expect(subject.subject).not_to eq RDF.nil
46
33
  end
47
34
 
48
- it "should insert at the head" do
49
- expect(subject).to be_kind_of DemoList::List
50
- expect(subject.size).to eq 0
51
- subject[0] = DemoList::List::TopicElement.new
52
- expect(subject.size).to eq 1
35
+ it 'has a non-nil subject' do
36
+ expect(subject.subject).not_to eq RDF.nil
53
37
  end
54
38
 
55
- describe "that has 4 elements" do
39
+ it 'has type of rdf:List' do
40
+ expect(subject.type.first).to eq RDF.List
41
+ end
42
+
43
+ it 'has correct number of elements' do
44
+ expect(subject.length).to eq 3
45
+ end
46
+
47
+ context 'after clear' do
56
48
  before do
57
- subject[3] = DemoList::List::TopicElement.new
58
- expect(subject.size).to eq 4
49
+ subject.clear
50
+ subject << 1
59
51
  end
60
- it "should insert in the middle" do
61
- subject[1] = DemoList::List::TopicElement.new
62
- expect(subject.size).to eq 4
52
+
53
+ it 'has a type of rdf:List' do
54
+ expect(subject.type.first).to eq RDF.List
63
55
  end
64
- end
65
56
 
66
- describe "return updated xml" do
67
- it "should be built" do
68
- subject[0] = RDF::URI.new "http://library.ucsd.edu/ark:/20775/bbXXXXXXX6"
69
- subject[1] = DemoList::List::TopicElement.new
70
- subject[1].elementValue = "Relations with Mexican Americans"
71
- subject[2] = RDF::URI.new "http://library.ucsd.edu/ark:/20775/bbXXXXXXX4"
72
- subject[3] = DemoList::List::TemporalElement.new
73
- subject[3].elementValue = "20th century"
74
- ds.elementList = subject
75
- doc = Nokogiri::XML(ds.dump(:rdfxml))
76
- ns = {rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#", mads: "http://www.loc.gov/mads/rdf/v1#"}
77
- expect(doc.xpath('/rdf:RDF/rdf:Description/@rdf:about', ns).map(&:value)).to eq ["http://example.org/foo"]
78
- expect(doc.xpath('//rdf:Description/mads:elementList/@rdf:parseType', ns).map(&:value)).to eq ["Collection"]
79
- expect(doc.xpath('//rdf:Description/mads:elementList/*[position() = 1]/@rdf:about', ns).map(&:value)).to eq ["http://library.ucsd.edu/ark:/20775/bbXXXXXXX6"]
80
- expect(doc.xpath('//rdf:Description/mads:elementList/*[position() = 2]/mads:elementValue', ns).map(&:text)).to eq ["Relations with Mexican Americans"]
81
- expect(doc.xpath('//rdf:Description/mads:elementList/*[position() = 3]/@rdf:about', ns).map(&:value)).to eq ["http://library.ucsd.edu/ark:/20775/bbXXXXXXX4"]
82
- expect(doc.xpath('//rdf:Description/mads:elementList/*[position() = 4]/mads:elementValue', ns).map(&:text)).to eq ["20th century"]
57
+ it 'has correct number of elements' do
58
+ expect(subject.length).to eq 1
83
59
  end
84
60
  end
85
61
  end
86
62
 
87
- describe "an empty list" do
88
- subject { DemoList.new.elementList.build }
89
- it "should have to_ary" do
90
- expect(subject.to_ary).to eq []
63
+ context 'with properties' do
64
+ before :each do
65
+ class MADS < RDF::Vocabulary("http://www.loc.gov/mads/rdf/v1#")
66
+ property :complexSubject
67
+ property :authoritativeLabel
68
+ property :elementList
69
+ property :elementValue
70
+ property :TopicElement
71
+ property :TemporalElement
72
+ end
73
+ class DemoList < ActiveTriples::Resource
74
+ property :elementList, :predicate => MADS.elementList, :class_name => 'DemoList::List'
75
+ class List < ActiveTriples::List
76
+ property :topicElement, :predicate => MADS.TopicElement, :class_name => 'DemoList::List::TopicElement'
77
+ property :temporalElement, :predicate => MADS.TemporalElement, :class_name => 'DemoList::List::TemporalElement'
78
+
79
+ class TopicElement < ActiveTriples::Resource
80
+ configure :type => MADS.TopicElement
81
+ property :elementValue, :predicate => MADS.elementValue
82
+ end
83
+ class TemporalElement < ActiveTriples::Resource
84
+ configure :type => MADS.TemporalElement
85
+ property :elementValue, :predicate => MADS.elementValue
86
+ end
87
+ end
88
+ end
89
+ end
90
+ after(:each) do
91
+ Object.send(:remove_const, :DemoList)
92
+ Object.send(:remove_const, :MADS)
91
93
  end
92
- end
93
94
 
94
- describe "a list that has a constructed element" do
95
- let(:ds) { DemoList.new('http://example.org/foo') }
96
- let(:list) { ds.elementList.build }
97
- let!(:topic) { list.topicElement.build }
95
+ describe "a new list" do
96
+ let (:ds) { DemoList.new('http://example.org/foo') }
97
+ subject { ds.elementList.build}
98
98
 
99
- it "should have to_ary" do
100
- expect(list.to_ary.size).to eq 1
101
- expect(list.to_ary.first.class).to eq DemoList::List::TopicElement
99
+ it "should insert at the end" do
100
+ expect(subject).to be_kind_of DemoList::List
101
+ expect(subject.size).to eq 0
102
+ subject[1] = DemoList::List::TopicElement.new
103
+ expect(subject.size).to eq 2
104
+ end
105
+
106
+ it "should insert at the head" do
107
+ expect(subject).to be_kind_of DemoList::List
108
+ expect(subject.size).to eq 0
109
+ subject[0] = DemoList::List::TopicElement.new
110
+ expect(subject.size).to eq 1
111
+ end
112
+
113
+ describe "that has 4 elements" do
114
+ before do
115
+ subject[3] = DemoList::List::TopicElement.new
116
+ expect(subject.size).to eq 4
117
+ end
118
+ it "should insert in the middle" do
119
+ subject[1] = DemoList::List::TopicElement.new
120
+ expect(subject.size).to eq 4
121
+ end
122
+ end
123
+
124
+ describe "return updated xml" do
125
+ it "should be built" do
126
+ subject[0] = RDF::URI.new "http://library.ucsd.edu/ark:/20775/bbXXXXXXX6"
127
+ subject[1] = DemoList::List::TopicElement.new
128
+ subject[1].elementValue = "Relations with Mexican Americans"
129
+ subject[2] = RDF::URI.new "http://library.ucsd.edu/ark:/20775/bbXXXXXXX4"
130
+ subject[3] = DemoList::List::TemporalElement.new
131
+ subject[3].elementValue = "20th century"
132
+ ds.elementList = subject
133
+ doc = Nokogiri::XML(ds.dump(:rdfxml))
134
+ ns = {rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#", mads: "http://www.loc.gov/mads/rdf/v1#"}
135
+ expect(doc.xpath('/rdf:RDF/rdf:Description/@rdf:about', ns).map(&:value)).to eq ["http://example.org/foo"]
136
+ expect(doc.xpath('//rdf:Description/mads:elementList/@rdf:parseType', ns).map(&:value)).to eq ["Collection"]
137
+ expect(doc.xpath('//rdf:Description/mads:elementList/*[position() = 1]/@rdf:about', ns).map(&:value)).to eq ["http://library.ucsd.edu/ark:/20775/bbXXXXXXX6"]
138
+ expect(doc.xpath('//rdf:Description/mads:elementList/*[position() = 2]/mads:elementValue', ns).map(&:text)).to eq ["Relations with Mexican Americans"]
139
+ expect(doc.xpath('//rdf:Description/mads:elementList/*[position() = 3]/@rdf:about', ns).map(&:value)).to eq ["http://library.ucsd.edu/ark:/20775/bbXXXXXXX4"]
140
+ expect(doc.xpath('//rdf:Description/mads:elementList/*[position() = 4]/mads:elementValue', ns).map(&:text)).to eq ["20th century"]
141
+ end
142
+ end
102
143
  end
103
144
 
104
- it "should be able to be cleared" do
105
- list.topicElement.build
106
- list.topicElement.build
107
- list.topicElement.build
108
- expect(list.size).to eq 4
109
- list.clear
110
- expect(list.size).to eq 0
145
+ describe "an empty list" do
146
+ subject { DemoList.new.elementList.build }
147
+ it "should have to_ary" do
148
+ expect(subject.to_ary).to eq []
149
+ end
111
150
  end
112
- end
113
151
 
114
- describe "a list with content" do
115
- subject do
116
- subject = DemoList.new('http://example.org/foo')
117
- subject << RDF::RDFXML::Reader.for(:rdfxml).new(<<END
152
+ describe "a list that has a constructed element" do
153
+ let(:ds) { DemoList.new('http://example.org/foo') }
154
+ let(:list) { ds.elementList.build }
155
+ let!(:topic) { list.topicElement.build }
156
+
157
+ it "should have to_ary" do
158
+ expect(list.to_ary.size).to eq 1
159
+ expect(list.to_ary.first.class).to eq DemoList::List::TopicElement
160
+ end
161
+
162
+ describe 'clearing a list' do
163
+ it "should be able to be cleared" do
164
+ list.topicElement.build
165
+ list.topicElement.build
166
+ list.topicElement.build
167
+ expect(list.size).to eq 4
168
+ list.clear
169
+ expect(list.size).to eq 0
170
+ end
171
+
172
+ it 'should allow elements to be added after clearing' do
173
+ list.clear
174
+ list.topicElement.build
175
+ list.topicElement.build
176
+ list.topicElement.build
177
+ expect(list.size).to eq 3
178
+ end
179
+ end
180
+ end
181
+
182
+ describe "a list with content" do
183
+ subject do
184
+ subject = DemoList.new('http://example.org/foo')
185
+ subject << RDF::RDFXML::Reader.for(:rdfxml).new(<<END
118
186
  <rdf:RDF
119
187
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:mads="http://www.loc.gov/mads/rdf/v1#">
120
188
 
@@ -132,64 +200,64 @@ describe ActiveTriples::List do
132
200
  </mads:ComplexSubject>
133
201
  </rdf:RDF>
134
202
  END
135
- )
203
+ )
136
204
 
137
- subject
138
- end
139
- it "should have a subject" do
140
- expect(subject.rdf_subject.to_s).to eq "http://example.org/foo"
141
- end
205
+ subject
206
+ end
207
+ it "should have a subject" do
208
+ expect(subject.rdf_subject.to_s).to eq "http://example.org/foo"
209
+ end
142
210
 
143
- let (:list) { subject.elementList.first }
211
+ let (:list) { subject.elementList.first }
144
212
 
145
- it "should have fields" do
146
- expect(list.first.rdf_subject).to eq "http://library.ucsd.edu/ark:/20775/bbXXXXXXX6"
147
- expect(list[1]).to be_kind_of DemoList::List::TopicElement
148
- expect(list[1].elementValue).to eq ["Relations with Mexican Americans"]
149
- expect(list[2].rdf_subject).to eq "http://library.ucsd.edu/ark:/20775/bbXXXXXXX4"
150
- expect(list[3]).to be_kind_of DemoList::List::TemporalElement
151
- expect(list[3].elementValue).to eq ["20th century"]
152
- end
213
+ it "should have fields" do
214
+ expect(list.first.rdf_subject).to eq "http://library.ucsd.edu/ark:/20775/bbXXXXXXX6"
215
+ expect(list[1]).to be_kind_of DemoList::List::TopicElement
216
+ expect(list[1].elementValue).to eq ["Relations with Mexican Americans"]
217
+ expect(list[2].rdf_subject).to eq "http://library.ucsd.edu/ark:/20775/bbXXXXXXX4"
218
+ expect(list[3]).to be_kind_of DemoList::List::TemporalElement
219
+ expect(list[3].elementValue).to eq ["20th century"]
220
+ end
153
221
 
154
- it "should have each" do
155
- foo = []
156
- list.each { |n| foo << n.class }
157
- expect(foo).to eq [ActiveTriples::Resource, DemoList::List::TopicElement, ActiveTriples::Resource, DemoList::List::TemporalElement]
158
- end
222
+ it "should have each" do
223
+ foo = []
224
+ list.each { |n| foo << n.class }
225
+ expect(foo).to eq [ActiveTriples::Resource, DemoList::List::TopicElement, ActiveTriples::Resource, DemoList::List::TemporalElement]
226
+ end
159
227
 
160
- it "should have to_ary" do
161
- ary = list.to_ary
162
- expect(ary.size).to eq 4
163
- expect(ary[1].elementValue).to eq ['Relations with Mexican Americans']
164
- end
228
+ it "should have to_ary" do
229
+ ary = list.to_ary
230
+ expect(ary.size).to eq 4
231
+ expect(ary[1].elementValue).to eq ['Relations with Mexican Americans']
232
+ end
165
233
 
166
- it "should have size" do
167
- expect(list.size).to eq 4
168
- end
234
+ it "should have size" do
235
+ expect(list.size).to eq 4
236
+ end
169
237
 
170
238
 
171
- describe "updating fields" do
172
- it "stores the values in a containing node" do
173
- list[3].elementValue = ["1900s"]
174
- doc = Nokogiri::XML(subject.dump :rdfxml)
175
- ns = {rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#", mads: "http://www.loc.gov/mads/rdf/v1#"}
176
- expect(doc.xpath('/rdf:RDF/mads:ComplexSubject/@rdf:about', ns).map(&:value)).to eq ["http://example.org/foo"]
177
- expect(doc.xpath('//mads:ComplexSubject/mads:elementList/@rdf:parseType', ns).map(&:value)).to eq ["Collection"]
178
- expect(doc.xpath('//mads:ComplexSubject/mads:elementList/*[position() = 1]/@rdf:about', ns).map(&:value)).to eq ["http://library.ucsd.edu/ark:/20775/bbXXXXXXX6"]
179
- expect(doc.xpath('//mads:ComplexSubject/mads:elementList/*[position() = 2]/mads:elementValue', ns).map(&:text)).to eq ["Relations with Mexican Americans"]
180
- expect(doc.xpath('//mads:ComplexSubject/mads:elementList/*[position() = 3]/@rdf:about', ns).map(&:value)).to eq ["http://library.ucsd.edu/ark:/20775/bbXXXXXXX4"]
181
- expect(doc.xpath('//mads:ComplexSubject/mads:elementList/*[position() = 4]/mads:elementValue', ns).map(&:text)).to eq ["1900s"]
182
- expect(RDF::List.new(list.rdf_subject, subject.graph)).to be_valid
183
- end
184
-
185
- it "should be a valid list" do
186
- list << "Val"
187
- # TODO this is a workaround for https://github.com/projecthydra/active_fedora/issues/444
188
- # remove the following line when #444 is closed.
189
- list.resource.persist!
190
- expect(RDF::List.new(list.rdf_subject, subject.graph)).to be_valid
191
- end
192
- end
239
+ describe "updating fields" do
240
+ it "stores the values in a containing node" do
241
+ list[3].elementValue = ["1900s"]
242
+ doc = Nokogiri::XML(subject.dump :rdfxml)
243
+ ns = {rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#", mads: "http://www.loc.gov/mads/rdf/v1#"}
244
+ expect(doc.xpath('/rdf:RDF/mads:ComplexSubject/@rdf:about', ns).map(&:value)).to eq ["http://example.org/foo"]
245
+ expect(doc.xpath('//mads:ComplexSubject/mads:elementList/@rdf:parseType', ns).map(&:value)).to eq ["Collection"]
246
+ expect(doc.xpath('//mads:ComplexSubject/mads:elementList/*[position() = 1]/@rdf:about', ns).map(&:value)).to eq ["http://library.ucsd.edu/ark:/20775/bbXXXXXXX6"]
247
+ expect(doc.xpath('//mads:ComplexSubject/mads:elementList/*[position() = 2]/mads:elementValue', ns).map(&:text)).to eq ["Relations with Mexican Americans"]
248
+ expect(doc.xpath('//mads:ComplexSubject/mads:elementList/*[position() = 3]/@rdf:about', ns).map(&:value)).to eq ["http://library.ucsd.edu/ark:/20775/bbXXXXXXX4"]
249
+ expect(doc.xpath('//mads:ComplexSubject/mads:elementList/*[position() = 4]/mads:elementValue', ns).map(&:text)).to eq ["1900s"]
250
+ expect(RDF::List.new(list.rdf_subject, subject.graph)).to be_valid
251
+ end
252
+
253
+ it "should be a valid list" do
254
+ list << "Val"
255
+ # TODO this is a workaround for https://github.com/projecthydra/active_fedora/issues/444
256
+ # remove the following line when #444 is closed.
257
+ list.resource.persist!
258
+ expect(RDF::List.new(list.rdf_subject, subject.graph)).to be_valid
259
+ end
260
+ end
261
+ end
193
262
  end
194
263
  end
195
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active-triples
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Johnson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-01 00:00:00.000000000 Z
12
+ date: 2014-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdf