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 +4 -4
- data/lib/active_triples/list.rb +20 -2
- data/lib/active_triples/version.rb +1 -1
- data/spec/active_triples/list_spec.rb +207 -139
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d6cf7f26813ed44293a6b7203b38086e3b575d5
|
4
|
+
data.tar.gz: 0810ad91e32b13f6130abc1e31e56284157db424
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40d43a979b77e9c591e584d7d5a61717990ce62c4f5dce67b8ebeee2806b12a027e7569eb88e2a3d7f9b19c793c8518dc9fe1e98d9c0f973055be3085d231f27
|
7
|
+
data.tar.gz: 3511436cbfac29ef7b52021786eff8bf3ba157f5b822e70939e23dfff72026a3951d39abc8e0ceb102aa5b00a826861f4d6f6ac6189955c847f8ea9d6c8bf766
|
data/lib/active_triples/list.rb
CHANGED
@@ -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
|
@@ -3,118 +3,186 @@ require 'nokogiri'
|
|
3
3
|
require 'linkeddata'
|
4
4
|
|
5
5
|
describe ActiveTriples::List do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
24
|
+
context 'with elements' do
|
25
|
+
before do
|
26
|
+
subject << 1
|
27
|
+
subject << 2
|
28
|
+
subject << 3
|
29
|
+
end
|
40
30
|
|
41
|
-
it
|
42
|
-
expect(subject).
|
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
|
49
|
-
expect(subject).
|
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
|
-
|
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
|
58
|
-
|
49
|
+
subject.clear
|
50
|
+
subject << 1
|
59
51
|
end
|
60
|
-
|
61
|
-
|
62
|
-
expect(subject.
|
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
|
-
|
67
|
-
|
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
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
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
|
-
|
95
|
-
|
96
|
-
|
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
|
-
|
100
|
-
|
101
|
-
|
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
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
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
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
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
|
-
|
211
|
+
let (:list) { subject.elementList.first }
|
144
212
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
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
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
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
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
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
|
-
|
167
|
-
|
168
|
-
|
234
|
+
it "should have size" do
|
235
|
+
expect(list.size).to eq 4
|
236
|
+
end
|
169
237
|
|
170
238
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
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.
|
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-
|
12
|
+
date: 2014-07-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rdf
|