Skip to content

Commit 86a06ab

Browse files
committed
Merge pull request #5 from ld4l/add_casting_restrictions
Add casting restrictions; Let Annotation.resume receive same parameters as .new
2 parents 8e5048b + a5ccca5 commit 86a06ab

File tree

9 files changed

+75
-41
lines changed

9 files changed

+75
-41
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,43 @@ puts stb.dump :ttl
122122
<http://example.org/term/engineering> a <http://www.w3.org/ns/oa#SemanticTag> .
123123
```
124124

125+
*Resume annotation of unknown type.*
126+
```
127+
## Using RDF::URI
128+
# Create the annotations first using previous examples.
129+
a1 = LD4L::OpenAnnotationRDF::Annotation.resume(RDF::URI('http://localhost/c10'))
130+
# => #<LD4L::OpenAnnotationRDF::CommentAnnotation:0x3fdd8267adc8(default)>
131+
132+
a2 = LD4L::OpenAnnotationRDF::Annotation.resume(RDF::URI('http://localhost/t10'))
133+
# => #<LD4L::OpenAnnotationRDF::TagAnnotation:0x3fdd826073f0(default)>
134+
135+
a3 = LD4L::OpenAnnotationRDF::Annotation.resume(RDF::URI('http://localhost/st10'))
136+
# => #<LD4L::OpenAnnotationRDF::SemanticTagAnnotation:0x3fdd8259c7a8(default)>
137+
138+
139+
## Using string URI
140+
# Create the annotations first using previous examples.
141+
a1 = LD4L::OpenAnnotationRDF::Annotation.resume('http://localhost/c10')
142+
# => #<LD4L::OpenAnnotationRDF::CommentAnnotation:0x3fdd8267adc8(default)>
143+
144+
a2 = LD4L::OpenAnnotationRDF::Annotation.resume('http://localhost/t10')
145+
# => #<LD4L::OpenAnnotationRDF::TagAnnotation:0x3fdd826073f0(default)>
146+
147+
a3 = LD4L::OpenAnnotationRDF::Annotation.resume('http://localhost/st10')
148+
# => #<LD4L::OpenAnnotationRDF::SemanticTagAnnotation:0x3fdd8259c7a8(default)>
149+
150+
151+
## Using localname expanded using configured base_uri
152+
# Create the annotations first using previous examples.
153+
a1 = LD4L::OpenAnnotationRDF::Annotation.resume('c10')
154+
# => #<LD4L::OpenAnnotationRDF::CommentAnnotation:0x3fdd8267adc8(default)>
155+
156+
a2 = LD4L::OpenAnnotationRDF::Annotation.resume('t10')
157+
# => #<LD4L::OpenAnnotationRDF::TagAnnotation:0x3fdd826073f0(default)>
158+
159+
a3 = LD4L::OpenAnnotationRDF::Annotation.resume('st10')
160+
# => #<LD4L::OpenAnnotationRDF::SemanticTagAnnotation:0x3fdd8259c7a8(default)>
161+
```
125162

126163
### Configurations
127164

lib/ld4l/open_annotation_rdf/annotation.rb

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,31 @@ class << self; attr_reader :localname_prefix end
1414
property :hasTarget, :predicate => RDFVocabularies::OA.hasTarget # :type => URI
1515
property :hasBody, :predicate => RDFVocabularies::OA.hasBody
1616
property :annotatedBy, :predicate => RDFVocabularies::OA.annotatedBy, :class_name => LD4L::FoafRDF::Person
17-
property :annotatedAt, :predicate => RDFVocabularies::OA.annotatedAt # :type => xsd:dateTime # the time Annotation was created
18-
property :motivatedBy, :predicate => RDFVocabularies::OA.motivatedBy # comes from RDFVocabularies::OA ontology
17+
property :annotatedAt, :predicate => RDFVocabularies::OA.annotatedAt, :cast => false # :type => xsd:dateTime # the time Annotation was created
18+
property :motivatedBy, :predicate => RDFVocabularies::OA.motivatedBy, :cast => false # comes from RDFVocabularies::OA ontology
1919

20-
def self.resume(*args)
21-
return nil unless args.kind_of?(Array) && args.size > 0 && args.first.kind_of?(RDF::URI)
22-
23-
rdf_subject = args.first
24-
a = new(rdf_subject)
20+
def self.resume(uri_or_str)
21+
# Let ActiveTriples::Resource validate uri_or_str when creating new Annotation
22+
a = new(uri_or_str)
2523

2624
# get motivatedBy
2725
m = a.get_values(:motivatedBy)
28-
return a unless m.kind_of?(Array) && m.size > 0 && m.first.kind_of?(ActiveTriples::Resource)
26+
return a unless m.kind_of?(Array) && m.size > 0 && m.first.kind_of?(RDF::Vocabulary::Term)
2927

3028
# motivatedBy is set
31-
m_uri = m.first.rdf_subject
32-
29+
m_uri = m.first
3330
# currently only support commenting and tagging
34-
return LD4L::OpenAnnotationRDF::CommentAnnotation.new(rdf_subject) if m_uri == RDFVocabularies::OA.commenting
31+
return LD4L::OpenAnnotationRDF::CommentAnnotation.new(uri_or_str) if m_uri == RDFVocabularies::OA.commenting
3532
return a unless m_uri == RDFVocabularies::OA.tagging
3633

3734
# Tagging can be TagAnnotation or SemanticTagAnnotation. Only way to tell is by checking type of body.
38-
sta = LD4L::OpenAnnotationRDF::SemanticTagAnnotation.new(rdf_subject)
35+
sta = LD4L::OpenAnnotationRDF::SemanticTagAnnotation.new(uri_or_str)
3936
stb = sta.getBody
40-
return sta if stb.type.include?(RDFVocabularies::OA.SemanticTag)
37+
return sta if stb.type.include?(RDFVocabularies::OA.SemanticTag)
4138

42-
ta = LD4L::OpenAnnotationRDF::TagAnnotation.new(rdf_subject)
39+
ta = LD4L::OpenAnnotationRDF::TagAnnotation.new(uri_or_str)
4340
tb = ta.getBody
44-
return ta if tb.type.include?(RDFVocabularies::OA.Tag)
41+
return ta if tb.type.include?(RDFVocabularies::OA.Tag)
4542

4643
# can't match to a known annotation type, so return as generic annotation
4744
return a

lib/ld4l/open_annotation_rdf/comment_body.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class << self; attr_reader :localname_prefix end
99
:base_uri => LD4L::OpenAnnotationRDF.configuration.base_uri,
1010
:repository => :default
1111

12-
property :content, :predicate => RDFVocabularies::CNT.chars # :type => XSD.string
13-
property :format, :predicate => RDF::DC.format # :type => XSD.string
12+
property :content, :predicate => RDFVocabularies::CNT.chars, :cast => true # :type => XSD.string
13+
property :format, :predicate => RDF::DC.format, :cast => true # :type => XSD.string
1414

1515
def initialize(*args)
1616
super(*args)

lib/ld4l/open_annotation_rdf/tag_body.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class << self; attr_reader :localname_prefix end
99
:base_uri => LD4L::OpenAnnotationRDF.configuration.base_uri,
1010
:repository => :default
1111

12-
property :tag, :predicate => RDFVocabularies::CNT.chars # :type => XSD.string
12+
property :tag, :predicate => RDFVocabularies::CNT.chars, :cast => false # :type => XSD.string
1313

1414
##
1515
# Get a list of annotations using the tag value.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module LD4L
22
module OpenAnnotationRDF
3-
VERSION = "0.0.7"
3+
VERSION = "0.0.8"
44
end
55
end

spec/ld4l/open_annotation_rdf/annotation_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@
151151

152152
it "should be settable" do
153153
subject.motivatedBy = RDFVocabularies::OA.describing
154-
expect(subject.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.describing
154+
expect(subject.motivatedBy.first).to eq RDFVocabularies::OA.describing
155155
end
156156

157157
it "should be changeable" do
158158
subject.motivatedBy = RDFVocabularies::OA.describing
159159
subject.motivatedBy = RDFVocabularies::OA.classifying
160-
expect(subject.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.classifying
160+
expect(subject.motivatedBy.first).to eq RDFVocabularies::OA.classifying
161161
end
162162
end
163163

@@ -189,7 +189,7 @@
189189
expect(a.hasTarget.first.rdf_subject.to_s).to eq "http://example.org/bibref/br3"
190190
expect(a.annotatedBy.first).to eq a_person
191191
expect(a.annotatedAt.first).to eq a_time
192-
expect(a.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.commenting
192+
expect(a.motivatedBy.first).to eq RDFVocabularies::OA.commenting
193193

194194
b = a.getBody
195195
expect(b).to be_a_kind_of(LD4L::OpenAnnotationRDF::CommentBody)
@@ -215,7 +215,7 @@
215215
expect(a.hasTarget.first.rdf_subject.to_s).to eq "http://example.org/bibref/br3"
216216
expect(a.annotatedBy.first).to eq a_person
217217
expect(a.annotatedAt.first).to eq a_time
218-
expect(a.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.tagging
218+
expect(a.motivatedBy.first).to eq RDFVocabularies::OA.tagging
219219

220220
b = a.getBody
221221
expect(b).to be_a_kind_of(LD4L::OpenAnnotationRDF::TagBody)
@@ -240,7 +240,7 @@
240240
expect(a.hasTarget.first.rdf_subject.to_s).to eq "http://example.org/bibref/br3"
241241
expect(a.annotatedBy.first).to eq a_person
242242
expect(a.annotatedAt.first).to eq a_time
243-
expect(a.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.tagging
243+
expect(a.motivatedBy.first).to eq RDFVocabularies::OA.tagging
244244

245245
b = a.getBody
246246
expect(b).to be_a_kind_of(LD4L::OpenAnnotationRDF::SemanticTagBody)
@@ -293,7 +293,7 @@
293293
end
294294

295295
it "should reset the motivatedBy" do
296-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
296+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
297297
end
298298

299299
it "should be persisted" do
@@ -325,7 +325,7 @@
325325

326326
it "should delete from the repository" do
327327
subject.reload
328-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
328+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
329329
subject.motivatedBy = []
330330
expect(subject.motivatedBy).to eq []
331331
subject.persist!

spec/ld4l/open_annotation_rdf/comment_annotation_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,18 @@
154154

155155
describe 'motivatedBy' do
156156
it "should be OA.commenting if we haven't set it" do
157-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting
157+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting
158158
end
159159

160160
it "should be settable" do
161161
subject.motivatedBy = RDFVocabularies::OA.describing
162-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.describing
162+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.describing
163163
end
164164

165165
it "should be changeable" do
166166
subject.motivatedBy = RDFVocabularies::OA.describing
167167
subject.motivatedBy = RDFVocabularies::OA.classifying
168-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.classifying
168+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.classifying
169169
end
170170
end
171171

@@ -220,7 +220,7 @@
220220
end
221221

222222
it "should reset the motivatedBy" do
223-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
223+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
224224
end
225225

226226
it "should be persisted" do
@@ -252,7 +252,7 @@
252252

253253
it "should delete from the repository" do
254254
subject.reload
255-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
255+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
256256
subject.motivatedBy = []
257257
expect(subject.motivatedBy).to eq []
258258
subject.persist!

spec/ld4l/open_annotation_rdf/semantic_tag_annotation_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,18 @@
212212

213213
describe 'motivatedBy' do
214214
it "should be OA.tagging if we haven't set it" do
215-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.tagging
215+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.tagging
216216
end
217217

218218
it "should be settable" do
219219
subject.motivatedBy = RDFVocabularies::OA.describing
220-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.describing
220+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.describing
221221
end
222222

223223
it "should be changeable" do
224224
subject.motivatedBy = RDFVocabularies::OA.describing
225225
subject.motivatedBy = RDFVocabularies::OA.classifying
226-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.classifying
226+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.classifying
227227
end
228228
end
229229

@@ -278,7 +278,7 @@
278278
end
279279

280280
it "should reset the motivatedBy" do
281-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
281+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
282282
end
283283

284284
it "should be persisted" do
@@ -310,7 +310,7 @@
310310

311311
it "should delete from the repository" do
312312
subject.reload
313-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
313+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
314314
subject.motivatedBy = []
315315
expect(subject.motivatedBy).to eq []
316316
subject.persist!

spec/ld4l/open_annotation_rdf/tag_annotation_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,18 +264,18 @@
264264

265265
describe 'motivatedBy' do
266266
it "should be OA.tagging if we haven't set it" do
267-
expect(subject.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.tagging
267+
expect(subject.motivatedBy.first).to eq RDFVocabularies::OA.tagging
268268
end
269269

270270
it "should be settable" do
271271
subject.motivatedBy = RDFVocabularies::OA.describing
272-
expect(subject.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.describing
272+
expect(subject.motivatedBy.first).to eq RDFVocabularies::OA.describing
273273
end
274274

275275
it "should be changeable" do
276276
subject.motivatedBy = RDFVocabularies::OA.describing
277277
subject.motivatedBy = RDFVocabularies::OA.classifying
278-
expect(subject.motivatedBy.first.rdf_subject).to eq RDFVocabularies::OA.classifying
278+
expect(subject.motivatedBy.first).to eq RDFVocabularies::OA.classifying
279279
end
280280
end
281281

@@ -330,7 +330,7 @@
330330
end
331331

332332
it "should reset the motivatedBy" do
333-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
333+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
334334
end
335335

336336
it "should be persisted" do
@@ -362,7 +362,7 @@
362362

363363
it "should delete from the repository" do
364364
subject.reload
365-
expect(subject.motivatedBy.first.rdf_subject.to_s).to eq RDFVocabularies::OA.commenting.to_s
365+
expect(subject.motivatedBy.first.to_s).to eq RDFVocabularies::OA.commenting.to_s
366366
subject.motivatedBy = []
367367
expect(subject.motivatedBy).to eq []
368368
subject.persist!

0 commit comments

Comments
 (0)