1
1
class Record
2
2
attr_accessor :id , :titles , :creator , :tags , :sets , :updated_at , :deleted
3
-
4
- def initialize ( id ,
5
- titles = 'title' ,
6
- creator = 'creator' ,
7
- tags = 'tag' ,
8
- sets = nil ,
3
+
4
+ def initialize ( id ,
5
+ titles = 'title' ,
6
+ creator = 'creator' ,
7
+ tags = 'tag' ,
8
+ sets = nil ,
9
9
deleted = false ,
10
10
updated_at = Time . now . utc . xmlschema )
11
-
11
+
12
12
@id = id
13
13
@titles = titles
14
14
@creator = creator
@@ -17,12 +17,12 @@ def initialize(id,
17
17
@deleted = deleted
18
18
@updated_at = updated_at
19
19
end
20
-
20
+
21
21
# Override Object.id
22
22
def id
23
23
@id
24
24
end
25
-
25
+
26
26
def in_set ( spec )
27
27
if @sets . respond_to? ( :each )
28
28
@sets . each { |set | return true if set . spec == spec }
@@ -35,26 +35,26 @@ def in_set(spec)
35
35
36
36
class TestModel < OAI ::Provider ::Model
37
37
include OAI ::Provider
38
-
38
+
39
39
def initialize ( limit = nil )
40
40
super ( limit )
41
41
@records = [ ]
42
42
@sets = [ ]
43
43
@earliest = Time . now . utc . xmlschema
44
44
end
45
-
45
+
46
46
def earliest
47
47
( @records . min { |a , b | a . updated_at <=> b . updated_at } ) . updated_at . utc . xmlschema
48
48
end
49
-
49
+
50
50
def latest
51
51
@records . max { |a , b | a . updated_at <=> b . updated_at } . updated_at . utc . xmlschema
52
52
end
53
53
54
54
def sets
55
55
@sets
56
56
end
57
-
57
+
58
58
def find ( selector , opts = { } )
59
59
return nil unless selector
60
60
@@ -79,15 +79,15 @@ def find(selector, opts={})
79
79
( opts [ :from ] . nil? || rec . updated_at >= opts [ :from ] ) &&
80
80
( opts [ :until ] . nil? || rec . updated_at <= opts [ :until ] ) )
81
81
#else
82
- # ((opts[:set].nil? || rec.in_set(opts[:set])) &&
82
+ # ((opts[:set].nil? || rec.in_set(opts[:set])) &&
83
83
# (opts[:from].nil? || rec.updated_at >= opts[:from]) &&
84
84
# (opts[:until].nil? || rec.updated_at <= opts[:until]))
85
85
#end
86
86
end
87
87
88
88
if @limit && records . size > @limit
89
89
@groups = generate_chunks ( records , @limit )
90
- return PartialResult . new ( @groups [ 0 ] ,
90
+ return PartialResult . new ( @groups [ 0 ] ,
91
91
ResumptionToken . new ( opts . merge ( { :last => 1 } ) ) )
92
92
end
93
93
return records
@@ -102,25 +102,25 @@ def find(selector, opts={})
102
102
nil
103
103
end
104
104
end
105
-
105
+
106
106
def generate_chunks ( records , limit )
107
107
groups = [ ]
108
108
records . each_slice ( limit ) do |group |
109
109
groups << group
110
110
end
111
111
groups
112
112
end
113
-
113
+
114
114
def generate_records ( number , timestamp = Time . now . utc . xmlschema , sets = [ ] , deleted = false )
115
115
@earliest = timestamp . dup if @earliest . nil? || timestamp . to_s < @earliest . to_s
116
116
@earliest = timestamp . dup if @earliest . nil?
117
-
117
+
118
118
# Add any sets we don't already have
119
119
sets = [ sets ] unless sets . respond_to? ( :each )
120
120
sets . each do |set |
121
121
@sets << set unless @sets . include? ( set )
122
- end
123
-
122
+ end
123
+
124
124
# Generate some records
125
125
number . times do |id |
126
126
rec = Record . new ( @records . size , "title_#{ id } " , "creator_#{ id } " , "tag_#{ id } " )
@@ -130,11 +130,11 @@ def generate_records(number, timestamp = Time.now.utc.xmlschema, sets = [], dele
130
130
@records << rec
131
131
end
132
132
end
133
-
133
+
134
134
end
135
135
136
136
class SimpleModel < TestModel
137
-
137
+
138
138
def initialize
139
139
super
140
140
# Create a couple of sets
@@ -156,7 +156,7 @@ def initialize
156
156
end
157
157
158
158
class BigModel < TestModel
159
-
159
+
160
160
def initialize ( limit = nil )
161
161
super ( limit )
162
162
generate_records ( 100 , Time . parse ( "October 2 2000" ) )
@@ -165,7 +165,7 @@ def initialize(limit = nil)
165
165
generate_records ( 100 , Time . parse ( "January 2 2001" ) )
166
166
generate_records ( 100 , Time . parse ( "February 2 2001" ) )
167
167
end
168
-
168
+
169
169
end
170
170
171
171
class MappedModel < TestModel
@@ -179,15 +179,15 @@ def initialize
179
179
180
180
generate_records ( 5 , Time . parse ( "dec 1 2006" ) , set_one )
181
181
end
182
-
182
+
183
183
def map_oai_dc
184
184
{ :title => :creator , :creator => :titles , :subject => :tags }
185
185
end
186
186
187
187
end
188
188
189
189
class ComplexModel < TestModel
190
-
190
+
191
191
def initialize ( limit = nil )
192
192
super ( limit )
193
193
# Create a couple of sets
@@ -225,20 +225,22 @@ def initialize(limit = nil)
225
225
generate_records ( 50 , Time . parse ( "June 2 1998" ) , [ set_one , set_one_two ] , true )
226
226
generate_records ( 50 , Time . parse ( "October 10 1998" ) , [ set_three , set_three_four ] , true )
227
227
generate_records ( 250 , Time . parse ( "July 2 2002" ) , [ set_two , set_one_two ] )
228
-
228
+
229
229
generate_records ( 250 , Time . parse ( "September 15 2004" ) , [ set_three , set_three_four ] )
230
230
generate_records ( 50 , Time . parse ( "October 10 2004" ) , [ set_three , set_three_four ] , true )
231
231
generate_records ( 250 , Time . parse ( "December 25 2005" ) , [ set_four , set_three_four ] )
232
232
end
233
-
233
+
234
234
def about record
235
- <<-eos
236
- <oai_dc:dc
235
+ xml = <<-eos
236
+ <oai_dc:dc
237
237
xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
238
238
xmlns:dc="http://purl.org/dc/elements/1.1/">
239
239
<dc:publisher>Ruby OAI test data</dc:publisher>
240
240
</oai_dc:dc>
241
241
eos
242
+ # Removes new-lines and formatting, which is a problem with Ruby 1.8.x
243
+ xml . gsub ( /\s +/ , ' ' )
242
244
end
243
245
end
244
246
0 commit comments