ladder 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -63,7 +63,7 @@ module Ladder::Searchable
63
63
  ##
64
64
  # Specify type of serialization to use for indexing
65
65
  #
66
- def index(opts = {})
66
+ def index_for_search(opts = {})
67
67
  case opts[:as]
68
68
  when :jsonld
69
69
  if opts[:related]
@@ -1,3 +1,3 @@
1
1
  module Ladder
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ladder::Resource::Dynamic do
4
+ before do
5
+ Mongoid.load!('mongoid.yml', :development)
6
+ Mongoid.logger.level = Moped.logger.level = Logger::DEBUG
7
+ Mongoid.purge!
8
+
9
+ LADDER_BASE_URI = 'http://example.org'
10
+
11
+ class Thing
12
+ include Ladder::Resource::Dynamic
13
+ end
14
+
15
+ class Person
16
+ include Ladder::Resource::Dynamic
17
+ end
18
+ end
19
+
20
+ it_behaves_like 'a Resource'
21
+
22
+ # TODO: Add specs
23
+
24
+ after do
25
+ Object.send(:remove_const, :LADDER_BASE_URI) if Object
26
+ Object.send(:remove_const, "Thing") if Object
27
+ Object.send(:remove_const, "Person") if Object
28
+ end
29
+ end
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'pry'
3
2
 
4
3
  describe Ladder::Resource do
5
4
  before do
@@ -17,346 +16,12 @@ describe Ladder::Resource do
17
16
  include Ladder::Resource
18
17
  end
19
18
  end
20
-
19
+
20
+ it_behaves_like 'a Resource'
21
+
21
22
  after do
22
23
  Object.send(:remove_const, :LADDER_BASE_URI) if Object
23
24
  Object.send(:remove_const, "Thing") if Object
24
25
  Object.send(:remove_const, "Person") if Object
25
26
  end
26
-
27
- subject { Thing.new }
28
- let(:person) { Person.new }
29
-
30
- shared_context 'with data' do
31
- let(:concept) { Concept.new }
32
- let(:part) { Part.new }
33
-
34
- before do
35
- class Concept
36
- include Ladder::Resource
37
- end
38
-
39
- class Part
40
- include Ladder::Resource
41
- end
42
-
43
- # localized literal
44
- subject.class.property :title, :predicate => RDF::DC.title
45
- subject.title = 'Comet in Moominland'
46
-
47
- # many-to-many
48
- person.class.property :things, :predicate => RDF::DC.relation, :class_name => 'Thing'
49
- subject.class.property :people, :predicate => RDF::DC.creator, :class_name => 'Person'
50
- subject.people << person
51
-
52
- # one-sided has-many
53
- subject.class.has_and_belongs_to_many :concepts, inverse_of: nil
54
- subject.class.property :concepts, :predicate => RDF::DC.subject, :class_name => 'Concept'
55
- subject.concepts << concept
56
-
57
- # embedded one
58
- part.class.embedded_in :thing
59
- part.class.property :thing, :predicate => RDF::DC.relation, :class_name => 'Thing'
60
- subject.class.embeds_one :part, cascade_callbacks: true
61
- subject.class.property :part, :predicate => RDF::DC.hasPart, :class_name => 'Part'
62
- subject.part = part
63
- subject.save
64
- end
65
-
66
- after do
67
- Object.send(:remove_const, 'Concept')
68
- Object.send(:remove_const, 'Part')
69
- end
70
-
71
- it 'should have relations' do
72
- expect(subject.title).to eq 'Comet in Moominland'
73
- expect(subject.people.to_a).to include person
74
- expect(subject.concepts.to_a).to include concept
75
- expect(subject.part).to eq part
76
- end
77
-
78
- it 'should have reverse relations' do
79
- expect(person.things.to_a).to include subject
80
- expect(concept.relations).to be_empty
81
- expect(part.thing).to eq subject
82
- end
83
- end
84
-
85
- describe 'LADDER_BASE_URI' do
86
- it 'should automatically have a base URI' do
87
- expect(subject.rdf_subject.parent).to eq RDF::URI('http://example.org/things/')
88
- end
89
- end
90
-
91
- describe '#property' do
92
- context 'with localized literal' do
93
- before do
94
- subject.class.property :title, :predicate => RDF::DC.title
95
- subject.title = 'Comet in Moominland'
96
- end
97
-
98
- it 'should return localized value' do
99
- expect(subject.title).to eq 'Comet in Moominland'
100
- end
101
-
102
- it 'should return all locales' do
103
- expect(subject.attributes['title']).to eq Hash({'en' => 'Comet in Moominland'})
104
- end
105
-
106
- it 'should have a valid predicate' do
107
- expect(subject.class.properties).to include 'title'
108
- expect(t = subject.class.properties['title']).to be_a ActiveTriples::NodeConfig
109
- expect(t.predicate).to eq RDF::DC.title
110
- end
111
- end
112
-
113
- context 'with many-to-many' do
114
- before do
115
- subject.class.property :people, :predicate => RDF::DC.creator, :class_name => 'Person'
116
- person.class.property :things, :predicate => RDF::DC.relation, :class_name => 'Thing'
117
- subject.people << person
118
- subject.save
119
- end
120
-
121
- it 'should have a relation' do
122
- expect(subject.relations).to include 'people'
123
- expect(subject.relations['people'].relation).to eq (Mongoid::Relations::Referenced::ManyToMany)
124
- expect(subject.people.to_a).to include person
125
- end
126
-
127
- it 'should have an inverse relation' do
128
- expect(person.relations).to include 'things'
129
- expect(person.relations['things'].relation).to eq (Mongoid::Relations::Referenced::ManyToMany)
130
- expect(person.things.to_a).to include subject
131
- end
132
-
133
- it 'should have a valid predicate' do
134
- expect(subject.class.properties).to include 'people'
135
- expect(t = subject.class.properties['people']).to be_a ActiveTriples::NodeConfig
136
- expect(t.predicate).to eq RDF::DC.creator
137
- end
138
-
139
- it 'should have a valid inverse predicate' do
140
- expect(person.class.properties).to include 'things'
141
- expect(t = person.class.properties['things']).to be_a ActiveTriples::NodeConfig
142
- expect(t.predicate).to eq RDF::DC.relation
143
- end
144
- end
145
-
146
- context 'with one-sided has-many' do
147
- before do
148
- subject.class.has_and_belongs_to_many :people, inverse_of: nil
149
- subject.class.property :people, :predicate => RDF::DC.creator, :class_name => 'Person'
150
- subject.people << person
151
- end
152
-
153
- it 'should have a relation' do
154
- expect(subject.relations).to include 'people'
155
- expect(subject.relations['people'].relation).to eq (Mongoid::Relations::Referenced::ManyToMany)
156
- expect(subject.people.to_a).to include person
157
- end
158
-
159
- it 'should not have an inverse relation' do
160
- expect(subject.relations['people'].inverse_of).to be nil
161
- expect(person.relations).to be_empty
162
- end
163
-
164
- it 'should have a valid predicate' do
165
- expect(subject.class.properties).to include 'people'
166
- expect(t = subject.class.properties['people']).to be_a ActiveTriples::NodeConfig
167
- expect(t.predicate).to eq RDF::DC.creator
168
- end
169
-
170
- it 'should not have an inverse predicate' do
171
- expect(person.class.properties).to be_empty
172
- end
173
- end
174
-
175
- context 'with embeds-many' do
176
- before do
177
- subject.class.embeds_many :people
178
- subject.class.property :people, :predicate => RDF::DC.creator, :class_name => 'Person'
179
-
180
- person.class.embedded_in :thing
181
- person.class.property :thing, :predicate => RDF::DC.relation, :class_name => 'Thing'
182
-
183
- subject.people << person
184
- end
185
-
186
- it 'should have a relation' do
187
- expect(subject.relations).to include 'people'
188
- expect(subject.relations['people'].relation).to eq (Mongoid::Relations::Embedded::Many)
189
- expect(subject.people.to_a).to include person
190
- end
191
-
192
- it 'should have an inverse relation' do
193
- expect(person.relations).to include 'thing'
194
- expect(person.relations['thing'].relation).to eq (Mongoid::Relations::Embedded::In)
195
- expect(person.thing).to eq subject
196
- end
197
-
198
- it 'should have a valid predicate' do
199
- expect(subject.class.properties).to include 'people'
200
- expect(t = subject.class.properties['people']).to be_a ActiveTriples::NodeConfig
201
- expect(t.predicate).to eq RDF::DC.creator
202
- end
203
-
204
- it 'should have a valid inverse predicate' do
205
- expect(person.class.properties).to include 'thing'
206
- expect(t = person.class.properties['thing']).to be_a ActiveTriples::NodeConfig
207
- expect(t.predicate).to eq RDF::DC.relation
208
- end
209
- end
210
- end
211
-
212
- describe '#update_resource' do
213
-
214
- context 'without related: true' do
215
- include_context 'with data'
216
-
217
- before do
218
- subject.update_resource
219
- end
220
-
221
- it 'should have a literal object' do
222
- subject.resource.query(:subject => subject.rdf_subject, :predicate => RDF::DC.title).each_statement do |s|
223
- expect(s.object.to_s).to eq 'Comet in Moominland'
224
- end
225
- end
226
-
227
- it 'should have an embedded object' do
228
- query = subject.resource.query(:subject => subject.rdf_subject, :predicate => RDF::DC.hasPart)
229
- expect(query.count).to eq 1
230
-
231
- query.each_statement do |s|
232
- expect(s.object).to eq part.rdf_subject
233
- end
234
- end
235
-
236
- it 'should have an embedded object relation' do
237
- query = subject.resource.query(:subject => part.rdf_subject, :predicate => RDF::DC.relation)
238
- expect(query.count).to eq 1
239
- expect(query.to_hash).to eq part.resource.statements.to_hash
240
-
241
- query.each_statement do |s|
242
- expect(s.object).to eq subject.rdf_subject
243
- end
244
- end
245
-
246
- it 'should not have related objects' do
247
- expect(subject.resource.query(:subject => person.rdf_subject)).to be_empty
248
- expect(subject.resource.query(:subject => concept.rdf_subject)).to be_empty
249
- end
250
-
251
- it 'should not have related object relations' do
252
- expect(person.resource.statements).to be_empty
253
- expect(concept.resource.statements).to be_empty
254
- end
255
- end
256
-
257
- context 'with related: true' do
258
- include_context 'with data'
259
-
260
- before do
261
- subject.update_resource(:related => true)
262
- end
263
-
264
- it 'should have a literal object' do
265
- subject.resource.query(:subject => subject.rdf_subject, :predicate => RDF::DC.title).each_statement do |s|
266
- expect(s.object.to_s).to eq 'Comet in Moominland'
267
- end
268
- end
269
-
270
- it 'should have an embedded object' do
271
- query = subject.resource.query(:subject => subject.rdf_subject, :predicate => RDF::DC.hasPart)
272
- expect(query.count).to eq 1
273
-
274
- query.each_statement do |s|
275
- expect(s.object).to eq part.rdf_subject
276
- end
277
- end
278
-
279
- it 'should have an embedded object relation' do
280
- query = subject.resource.query(:subject => part.rdf_subject, :predicate => RDF::DC.relation)
281
- expect(query.count).to eq 1
282
- expect(query.to_hash).to eq part.resource.statements.to_hash
283
-
284
- query.each_statement do |s|
285
- expect(s.object).to eq subject.rdf_subject
286
- end
287
- end
288
-
289
- it 'should have related objects' do
290
- # many-to-many
291
- query_creator = subject.resource.query(:subject => subject.rdf_subject, :predicate => RDF::DC.creator)
292
- expect(query_creator.count).to eq 1
293
-
294
- query_creator.each_statement do |s|
295
- expect(s.object).to eq person.rdf_subject
296
- end
297
-
298
- # one-sided has-many
299
- query_subject = subject.resource.query(:subject => subject.rdf_subject, :predicate => RDF::DC.subject)
300
- expect(query_subject.count).to eq 1
301
-
302
- query_subject.each_statement do |s|
303
- expect(s.object).to eq concept.rdf_subject
304
- end
305
- end
306
-
307
- it 'should have related object relations' do
308
- # many-to-many
309
- query = person.resource.query(:subject => person.rdf_subject, :predicate => RDF::DC.relation)
310
- expect(query.count).to eq 1
311
- expect(query.to_hash).to eq person.resource.statements.to_hash
312
-
313
- query.each_statement do |s|
314
- expect(s.object).to eq subject.rdf_subject
315
- end
316
-
317
- # one-sided has-many
318
- expect(subject.resource.query(:subject => concept.rdf_subject)).to be_empty
319
- expect(concept.resource.statements).to be_empty
320
- end
321
- end
322
-
323
- context 'with related and then without related' do
324
- include_context 'with data'
325
-
326
- before do
327
- subject.update_resource(:related => true)
328
- subject.update_resource
329
- end
330
-
331
- it 'should not have related objects' do
332
- expect(subject.resource.query(:subject => person.rdf_subject)).to be_empty
333
- expect(subject.resource.query(:subject => concept.rdf_subject)).to be_empty
334
- end
335
-
336
- it 'should have related object relations' do
337
- # many-to-many
338
- query = person.resource.query(:subject => person.rdf_subject, :predicate => RDF::DC.relation)
339
- expect(query.count).to eq 1
340
- expect(query.to_hash).to eq person.resource.statements.to_hash
341
-
342
- query.each_statement do |s|
343
- expect(s.object).to eq subject.rdf_subject
344
- end
345
-
346
- # one-sided has-many
347
- expect(subject.resource.query(:subject => concept.rdf_subject)).to be_empty
348
- expect(concept.resource.statements).to be_empty
349
- end
350
- end
351
- end
352
-
353
- describe '#as_jsonld' do
354
- include_context 'with data'
355
-
356
- it 'should output a valid jsonld representation of itself' do
357
- g = RDF::Graph.new << JSON::LD::API.toRdf(subject.as_jsonld)
358
- expect(subject.resource.to_hash == g.to_hash).to be true
359
- end
360
- end
361
-
362
27
  end
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'pry'
3
2
 
4
3
  describe Ladder::Searchable do
5
4
  before do
@@ -23,199 +22,11 @@ describe Ladder::Searchable do
23
22
  end
24
23
  end
25
24
 
25
+ it_behaves_like 'a Resource'
26
+ it_behaves_like 'a Searchable'
27
+
26
28
  after do
27
29
  Object.send(:remove_const, "Thing") if Object
28
30
  Object.send(:remove_const, "Person") if Object
29
31
  end
30
-
31
- subject { Thing.new }
32
- let(:person) { Person.new }
33
-
34
- shared_context 'with data' do
35
- before do
36
- subject.class.configure type: RDF::DC.BibliographicResource
37
- subject.class.property :title, :predicate => RDF::DC.title
38
- subject.title = 'Comet in Moominland'
39
- end
40
- end
41
-
42
- describe '#index' do
43
- include_context 'with data'
44
-
45
- context 'with default' do
46
- before do
47
- subject.class.index
48
- subject.save
49
- Elasticsearch::Model.client.indices.flush
50
- end
51
-
52
- it 'should exist in the index' do
53
- results = subject.class.search('title:moomin*')
54
- expect(results.count).to eq 1
55
- expect(results.first._source.to_hash).to eq JSON.parse(subject.as_indexed_json.to_json)
56
- end
57
- end
58
-
59
- context 'with as qname' do
60
- before do
61
- subject.class.index as: :qname
62
- subject.save
63
- Elasticsearch::Model.client.indices.flush
64
- end
65
-
66
- it 'should exist in the index' do
67
- results = subject.class.search('dc.title.en:moomin*')
68
- expect(results.count).to eq 1
69
- expect(results.first._source.to_hash).to eq JSON.parse(subject.as_qname.to_json)
70
- end
71
- end
72
-
73
- context 'with as jsonld' do
74
- before do
75
- subject.class.index as: :jsonld
76
- subject.save
77
- Elasticsearch::Model.client.indices.flush
78
- end
79
-
80
- it 'should exist in the index' do
81
- results = subject.class.search('dc\:title.@value:moomin*')
82
- expect(results.count).to eq 1
83
- expect(results.first._source.to_hash).to eq subject.as_jsonld
84
- end
85
- end
86
- end
87
-
88
- describe '#index related' do
89
- include_context 'with data'
90
-
91
- before do
92
- # related object
93
- person.class.configure type: RDF::FOAF.Person
94
- person.class.property :name, :predicate => RDF::FOAF.name
95
- person.name = 'Tove Jansson'
96
-
97
- # many-to-many relation
98
- person.class.property :things, :predicate => RDF::DC.relation, :class_name => 'Thing'
99
- subject.class.property :people, :predicate => RDF::DC.creator, :class_name => 'Person'
100
- subject.people << person
101
- end
102
-
103
- context 'with default' do
104
- before do
105
- person.class.index
106
- subject.class.index
107
- subject.save
108
- Elasticsearch::Model.client.indices.flush
109
- end
110
-
111
- it 'should contain an ID for the related object' do
112
- results = subject.class.search('person_ids.$oid:' + person.id)
113
- expect(results.count).to eq 1
114
- end
115
-
116
- it 'should include the related object in the index' do
117
- results = person.class.search('name:tove')
118
- expect(results.count).to eq 1
119
- expect(results.first._source.to_hash).to eq JSON.parse(person.as_indexed_json.to_json)
120
- end
121
-
122
- it 'should contain an ID for the subject' do
123
- results = person.class.search('thing_ids.$oid:' + subject.id)
124
- expect(results.count).to eq 1
125
- end
126
- end
127
-
128
- context 'with as qname' do
129
- before do
130
- person.class.index as: :qname
131
- subject.class.index as: :qname
132
- subject.save
133
- Elasticsearch::Model.client.indices.flush
134
- end
135
-
136
- it 'should contain an ID for the related object' do
137
- results = subject.class.search('dc.creator:' + person.id)
138
- expect(results.count).to eq 1
139
- end
140
-
141
- it 'should include the related object in the index' do
142
- results = person.class.search('foaf.name.en:tove')
143
- expect(results.count).to eq 1
144
- expect(results.first._source.to_hash).to eq JSON.parse(person.as_qname.to_json)
145
- end
146
-
147
- it 'should contain an ID for the subject' do
148
- results = person.class.search('dc.relation:' + subject.id)
149
- expect(results.count).to eq 1
150
- end
151
- end
152
-
153
- context 'with as_qname related' do
154
- before do
155
- person.class.index as: :qname, related: true
156
- subject.class.index as: :qname, related: true
157
- subject.save
158
- Elasticsearch::Model.client.indices.flush
159
- end
160
-
161
- it 'should contain a embedded related object' do
162
- results = subject.class.search('dc.creator.foaf.name.en:tove')
163
- expect(results.count).to eq 1
164
- expect(results.first._source['dc']['creator'].first).to eq Hashie::Mash.new person.as_qname
165
- end
166
-
167
- it 'should contain an embedded subject in the related object' do
168
- results = person.class.search('dc.relation.dc.title.en:moomin*')
169
- expect(results.count).to eq 1
170
- expect(results.first._source['dc']['relation'].first).to eq Hashie::Mash.new subject.as_qname
171
- end
172
- end
173
-
174
- context 'with as_jsonld' do
175
- before do
176
- person.class.index as: :jsonld
177
- subject.class.index as: :jsonld
178
- subject.save
179
- Elasticsearch::Model.client.indices.flush
180
- end
181
-
182
- it 'should contain an ID for the related object' do
183
- results = subject.class.search('dc\:creator.@id:' + person.id)
184
- expect(results.count).to eq 1
185
- end
186
-
187
- it 'should include the related object in the index' do
188
- results = person.class.search('foaf\:name.@value:tove')
189
- expect(results.count).to eq 1
190
- expect(results.first._source.to_hash).to eq person.as_jsonld
191
- end
192
-
193
- it 'should contain an ID for the subject' do
194
- results = person.class.search('dc\:relation.@id:' + subject.id)
195
- expect(results.count).to eq 1
196
- end
197
- end
198
-
199
- context 'with as_jsonld related' do
200
- before do
201
- person.class.index as: :jsonld, related: true
202
- subject.class.index as: :jsonld, related: true
203
- subject.save
204
- Elasticsearch::Model.client.indices.flush
205
- end
206
-
207
- it 'should contain a embedded related object' do
208
- results = subject.class.search('dc\:creator.foaf\:name.@value:tove')
209
- expect(results.count).to eq 1
210
- expect(results.first._source.to_hash['dc:creator']).to eq person.as_jsonld.except '@context'
211
- end
212
-
213
- it 'should contain an embedded subject in the related object' do
214
- results = person.class.search('dc\:relation.dc\:title.@value:moomin*')
215
- expect(results.count).to eq 1
216
- expect(results.first._source.to_hash['dc:relation']).to eq subject.as_jsonld.except '@context'
217
- end
218
- end
219
- end
220
-
221
32
  end