mongoid-grid_fs 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,52 +1,56 @@
1
1
  NAME
2
2
  ----
3
- mongoid_grid_fs
3
+ mongoid-grid_fs
4
+
5
+ INSTALL
6
+ -------
7
+ gem install mongoid-grid_fs
4
8
 
5
9
  SYNOPSIS
6
10
  --------
7
11
 
8
- ````ruby
12
+ ````ruby
9
13
 
10
- require 'mongoid-grid_fs'
14
+ require 'mongoid-grid_fs'
11
15
 
12
- g = GridFs.put anthing_that_respons_to_read
16
+ g = GridFs.put readable
13
17
 
14
- GridFS.get id
18
+ GridFS.get id
15
19
 
16
- GridFS.delete id
20
+ GridFS.delete id
17
21
 
18
22
 
19
- ````
23
+ ````
20
24
 
21
25
  DESCRIPTION
22
26
  -----------
23
- mongoid_grid_fs is pure mongoid 3 / moped implementation of the mongodb
24
- grid_fs specification
27
+ mongoid_grid_fs is a pure mongoid 3 / moped implementation of the mongodb
28
+ grid_fs specification
25
29
 
26
- ref: http://www.mongodb.org/display/DOCS/GridFS+Specification
30
+ ref: http://www.mongodb.org/display/DOCS/GridFS+Specification
27
31
 
28
- it has the following features:
32
+ it has the following features:
29
33
 
30
- - implementation is on top of mongoid for portability. moped (the drive)
31
- is barely used
34
+ - implementation is on top of mongoid for portability. moped (the driver) is
35
+ barely used
32
36
 
33
- - simple, REST-like api
37
+ - simple, REST-like api
34
38
 
35
- - support for custom namespaces (fs.files vs. image.files)
39
+ - support for custom namespaces (fs.files vs. image.files)
36
40
 
37
- - pathnames and io-like objects can be written to the grid
41
+ - pathnames and io-like objects can be written to the grid
38
42
 
39
- - auto-unique pathnames are generated (by default) to avoid collisions using #put
43
+ - auto-unique pathnames are generated (by default) to avoid collisions using #put
40
44
 
41
- 'path/info/a.rb' -> '$object_id/a.rb'
45
+ 'path/info/a.rb' -> '$object_id/a.rb'
42
46
 
43
- - #[] and #[]= methods which allow the grid to be used like a giant file
44
- hash in the sky
47
+ - [] and []= methods which allow the grid to be used like a giant file
48
+ hash in the sky
45
49
 
46
- - supprt for data_uris
50
+ - supprt for data_uris
47
51
 
48
- ````eruby
52
+ ````erb
49
53
 
50
- <%= image_tag :src => file.data_url %>
54
+ <%= image_tag :src => file.data_uri %>
51
55
 
52
- ````
56
+ ````
@@ -1,419 +1,466 @@
1
- require "mongoid"
2
- require "mime/types"
3
- require "digest/md5"
4
- require "cgi"
5
-
6
- class GridFS
7
1
  ##
8
2
  #
9
- class << GridFS
10
- def version
11
- "1.0.0"
12
- end
3
+ class GridFS
4
+ const_set :Version, '1.1.0'
5
+
6
+ class << GridFS
7
+ def version
8
+ const_get :Version
9
+ end
10
+
11
+ def dependencies
12
+ {
13
+ 'mongoid' => [ 'mongoid' , ' >= 3.0.1' ] ,
14
+ 'mime/types' => [ 'mime-types' , ' >= 1.19' ] ,
15
+ }
16
+ end
13
17
 
14
- attr_accessor :namespace
15
- attr_accessor :file_model
16
- attr_accessor :chunk_model
17
-
18
- def init!
19
- GridFS.build_namespace_for(:Fs)
20
-
21
- GridFS.namespace = Fs
22
- GridFS.file_model = Fs.file_model
23
- GridFS.chunk_model = Fs.chunk_model
24
-
25
- const_set(:File, Fs.file_model)
26
- const_set(:Chunk, Fs.chunk_model)
27
-
28
- to_delegate = %w(
29
- put
30
- get
31
- delete
32
- find
33
- []
34
- []=
35
- )
36
-
37
- to_delegate.each do |method|
38
- class_eval <<-__
39
- def GridFS.#{ method }(*args, &block)
40
- ::GridFS::Fs::#{ method }(*args, &block)
18
+ def libdir(*args, &block)
19
+ @libdir ||= File.expand_path(__FILE__).sub(/\.rb$/,'')
20
+ args.empty? ? @libdir : File.join(@libdir, *args)
21
+ ensure
22
+ if block
23
+ begin
24
+ $LOAD_PATH.unshift(@libdir)
25
+ block.call()
26
+ ensure
27
+ $LOAD_PATH.shift()
41
28
  end
42
- __
29
+ end
30
+ end
31
+
32
+ def load(*libs)
33
+ libs = libs.join(' ').scan(/[^\s+]+/)
34
+ libdir{ libs.each{|lib| Kernel.load(lib) } }
43
35
  end
44
36
  end
45
- end
46
37
 
47
- ##
48
- #
49
- def GridFS.namespace_for(prefix)
50
- prefix = prefix.to_s.downcase
51
- const = "::GridFS::#{ prefix.to_s.camelize }"
52
- namespace = const.split(/::/).last
53
- const_defined?(namespace) ? const_get(namespace) : build_namespace_for(namespace)
38
+ begin
39
+ require 'rubygems'
40
+ rescue LoadError
41
+ nil
42
+ end
43
+
44
+ if defined?(gem)
45
+ dependencies.each do |lib, dependency|
46
+ gem(*dependency)
47
+ require(lib)
48
+ end
49
+ end
50
+
51
+ require "digest/md5"
52
+ require "cgi"
54
53
  end
55
54
 
56
55
  ##
57
56
  #
58
- def GridFS.build_namespace_for(prefix)
59
- prefix = prefix.to_s.downcase
60
- const = prefix.camelize
61
-
62
- namespace =
63
- Module.new do
64
- module_eval(&NamespaceMixin)
65
- self
57
+ class GridFS
58
+ class << GridFS
59
+ attr_accessor :namespace
60
+ attr_accessor :file_model
61
+ attr_accessor :chunk_model
62
+
63
+ def init!
64
+ GridFS.build_namespace_for(:Fs)
65
+
66
+ GridFS.namespace = Fs
67
+ GridFS.file_model = Fs.file_model
68
+ GridFS.chunk_model = Fs.chunk_model
69
+
70
+ const_set(:File, Fs.file_model)
71
+ const_set(:Chunk, Fs.chunk_model)
72
+
73
+ to_delegate = %w(
74
+ put
75
+ get
76
+ delete
77
+ find
78
+ []
79
+ []=
80
+ )
81
+
82
+ to_delegate.each do |method|
83
+ class_eval <<-__
84
+ def GridFS.#{ method }(*args, &block)
85
+ ::GridFS::Fs::#{ method }(*args, &block)
86
+ end
87
+ __
88
+ end
66
89
  end
90
+ end
67
91
 
68
- const_set(const, namespace)
92
+ ##
93
+ #
94
+ def GridFS.namespace_for(prefix)
95
+ prefix = prefix.to_s.downcase
96
+ const = "::GridFS::#{ prefix.to_s.camelize }"
97
+ namespace = const.split(/::/).last
98
+ const_defined?(namespace) ? const_get(namespace) : build_namespace_for(namespace)
99
+ end
69
100
 
70
- file_model = build_file_model_for(namespace)
71
- chunk_model = build_chunk_model_for(namespace)
101
+ ##
102
+ #
103
+ def GridFS.build_namespace_for(prefix)
104
+ prefix = prefix.to_s.downcase
105
+ const = prefix.camelize
72
106
 
73
- file_model.namespace = namespace
74
- chunk_model.namespace = namespace
107
+ namespace =
108
+ Module.new do
109
+ module_eval(&NamespaceMixin)
110
+ self
111
+ end
75
112
 
76
- file_model.chunk_model = chunk_model
77
- chunk_model.file_model = file_model
113
+ const_set(const, namespace)
78
114
 
79
- namespace.prefix = prefix
80
- namespace.file_model = file_model
81
- namespace.chunk_model = chunk_model
115
+ file_model = build_file_model_for(namespace)
116
+ chunk_model = build_chunk_model_for(namespace)
82
117
 
83
- namespace.send(:const_set, :File, file_model)
84
- namespace.send(:const_set, :Chunk, chunk_model)
118
+ file_model.namespace = namespace
119
+ chunk_model.namespace = namespace
85
120
 
86
- #at_exit{ file_model.create_indexes rescue nil }
87
- #at_exit{ chunk_model.create_indexes rescue nil }
121
+ file_model.chunk_model = chunk_model
122
+ chunk_model.file_model = file_model
88
123
 
89
- const_get(const)
90
- end
124
+ namespace.prefix = prefix
125
+ namespace.file_model = file_model
126
+ namespace.chunk_model = chunk_model
91
127
 
92
- NamespaceMixin = proc do
93
- class << self
94
- attr_accessor :prefix
95
- attr_accessor :file_model
96
- attr_accessor :chunk_model
128
+ namespace.send(:const_set, :File, file_model)
129
+ namespace.send(:const_set, :Chunk, chunk_model)
97
130
 
98
- def to_s
99
- prefix
100
- end
131
+ #at_exit{ file_model.create_indexes rescue nil }
132
+ #at_exit{ chunk_model.create_indexes rescue nil }
101
133
 
102
- def namespace
103
- prefix
104
- end
134
+ const_get(const)
135
+ end
105
136
 
106
- def put(readable, attributes = {})
107
- chunks = []
108
- file = file_model.new
109
- attributes.to_options!
137
+ NamespaceMixin = proc do
138
+ class << self
139
+ attr_accessor :prefix
140
+ attr_accessor :file_model
141
+ attr_accessor :chunk_model
110
142
 
111
- if attributes.has_key?(:id)
112
- file.id = attributes.delete(:id)
143
+ def to_s
144
+ prefix
113
145
  end
114
146
 
115
- if attributes.has_key?(:_id)
116
- file.id = attributes.delete(:_id)
147
+ def namespace
148
+ prefix
117
149
  end
118
150
 
119
- if attributes.has_key?(:content_type)
120
- attributes[:contentType] = attributes.delete(:content_type)
121
- end
151
+ def put(readable, attributes = {})
152
+ chunks = []
153
+ file = file_model.new
154
+ attributes.to_options!
122
155
 
123
- if attributes.has_key?(:upload_date)
124
- attributes[:uploadDate] = attributes.delete(:upload_date)
125
- end
156
+ if attributes.has_key?(:id)
157
+ file.id = attributes.delete(:id)
158
+ end
126
159
 
127
- md5 = Digest::MD5.new
128
- length = 0
129
- chunkSize = file.chunkSize
130
- n = 0
160
+ if attributes.has_key?(:_id)
161
+ file.id = attributes.delete(:_id)
162
+ end
131
163
 
132
- GridFS.reading(readable) do |io|
164
+ if attributes.has_key?(:content_type)
165
+ attributes[:contentType] = attributes.delete(:content_type)
166
+ end
133
167
 
134
- filename =
135
- attributes[:filename] ||=
136
- [file.id.to_s, GridFS.extract_basename(io)].join('/').squeeze('/')
168
+ if attributes.has_key?(:upload_date)
169
+ attributes[:uploadDate] = attributes.delete(:upload_date)
170
+ end
137
171
 
138
- content_type =
139
- attributes[:contentType] ||=
140
- GridFS.extract_content_type(filename) || file.contentType
172
+ md5 = Digest::MD5.new
173
+ length = 0
174
+ chunkSize = file.chunkSize
175
+ n = 0
141
176
 
142
- while((buf = io.read(chunkSize)))
143
- md5 << buf
144
- length += buf.size
145
- chunk = file.chunks.build
146
- chunk.data = binary_for(buf)
147
- chunk.n = n
148
- n += 1
149
- chunk.save!
150
- chunks.push(chunk)
151
- end
177
+ GridFS.reading(readable) do |io|
152
178
 
153
- end
179
+ filename =
180
+ attributes[:filename] ||=
181
+ [file.id.to_s, GridFS.extract_basename(io)].join('/').squeeze('/')
154
182
 
155
- attributes[:length] ||= length
156
- attributes[:uploadDate] ||= Time.now.utc
157
- attributes[:md5] ||= md5.hexdigest
183
+ content_type =
184
+ attributes[:contentType] ||=
185
+ GridFS.extract_content_type(filename) || file.contentType
158
186
 
159
- file.update_attributes(attributes)
187
+ while((buf = io.read(chunkSize)))
188
+ md5 << buf
189
+ length += buf.size
190
+ chunk = file.chunks.build
191
+ chunk.data = binary_for(buf)
192
+ chunk.n = n
193
+ n += 1
194
+ chunk.save!
195
+ chunks.push(chunk)
196
+ end
160
197
 
161
- file.save!
162
- file
163
- ensure
164
- chunks.each{|chunk| chunk.destroy rescue nil} if $!
165
- end
198
+ end
166
199
 
167
- if defined?(Moped)
168
- def binary_for(*buf)
169
- Moped::BSON::Binary.new(:generic, buf.join)
200
+ attributes[:length] ||= length
201
+ attributes[:uploadDate] ||= Time.now.utc
202
+ attributes[:md5] ||= md5.hexdigest
203
+
204
+ file.update_attributes(attributes)
205
+
206
+ file.save!
207
+ file
208
+ ensure
209
+ chunks.each{|chunk| chunk.destroy rescue nil} if $!
170
210
  end
171
- else
172
- def binary_for(buf)
173
- BSON::Binary.new(buf.bytes.to_a)
211
+
212
+ if defined?(Moped)
213
+ def binary_for(*buf)
214
+ Moped::BSON::Binary.new(:generic, buf.join)
215
+ end
216
+ else
217
+ def binary_for(buf)
218
+ BSON::Binary.new(buf.bytes.to_a)
219
+ end
174
220
  end
175
- end
176
221
 
177
- def get(id)
178
- file_model.find(id)
179
- end
222
+ def get(id)
223
+ file_model.find(id)
224
+ end
180
225
 
181
- def delete(id)
182
- file_model.find(id).destroy
183
- rescue
184
- nil
185
- end
226
+ def delete(id)
227
+ file_model.find(id).destroy
228
+ rescue
229
+ nil
230
+ end
186
231
 
187
- def where(conditions = {})
188
- case conditions
189
- when String
190
- file_model.where(:filename => conditions)
191
- else
192
- file_model.where(conditions)
232
+ def where(conditions = {})
233
+ case conditions
234
+ when String
235
+ file_model.where(:filename => conditions)
236
+ else
237
+ file_model.where(conditions)
238
+ end
193
239
  end
194
- end
195
240
 
196
- def find(*args)
197
- where(*args).first
198
- end
241
+ def find(*args)
242
+ where(*args).first
243
+ end
199
244
 
200
- def [](filename)
201
- file_model.where(:filename => filename.to_s).first
202
- end
245
+ def [](filename)
246
+ file_model.where(:filename => filename.to_s).first
247
+ end
203
248
 
204
- def []=(filename, readable)
205
- file = self[filename]
206
- file.destroy if file
207
- put(readable, :filename => filename.to_s)
208
- end
249
+ def []=(filename, readable)
250
+ file = self[filename]
251
+ file.destroy if file
252
+ put(readable, :filename => filename.to_s)
253
+ end
209
254
 
210
- # TODO - opening with a mode = 'w' should return a GridIO::IOProxy
211
- # implementing a StringIO-like interface
212
- #
213
- def open(filename, mode = 'r', &block)
214
- raise NotImplementedError
255
+ # TODO - opening with a mode = 'w' should return a GridIO::IOProxy
256
+ # implementing a StringIO-like interface
257
+ #
258
+ def open(filename, mode = 'r', &block)
259
+ raise NotImplementedError
260
+ end
215
261
  end
216
262
  end
217
- end
218
263
 
219
- ##
220
- #
221
- def GridFS.build_file_model_for(namespace)
222
- prefix = namespace.name.split(/::/).last.downcase
223
- file_model_name = "#{ namespace.name }::File"
224
- chunk_model_name = "#{ namespace.name }::Chunk"
264
+ ##
265
+ #
266
+ def GridFS.build_file_model_for(namespace)
267
+ prefix = namespace.name.split(/::/).last.downcase
268
+ file_model_name = "#{ namespace.name }::File"
269
+ chunk_model_name = "#{ namespace.name }::Chunk"
225
270
 
226
- Class.new do
227
- include Mongoid::Document
271
+ Class.new do
272
+ include Mongoid::Document
228
273
 
229
- singleton_class = class << self; self; end
274
+ singleton_class = class << self; self; end
230
275
 
231
- singleton_class.instance_eval do
232
- define_method(:name){ file_model_name }
233
- attr_accessor :chunk_model
234
- attr_accessor :namespace
235
- end
276
+ singleton_class.instance_eval do
277
+ define_method(:name){ file_model_name }
278
+ attr_accessor :chunk_model
279
+ attr_accessor :namespace
280
+ end
236
281
 
237
- self.default_collection_name = "#{ prefix }.files"
282
+ self.default_collection_name = "#{ prefix }.files"
238
283
 
239
- field(:filename, :type => String)
240
- field(:contentType, :type => String, :default => 'application/octet-stream')
284
+ field(:filename, :type => String)
285
+ field(:contentType, :type => String, :default => 'application/octet-stream')
241
286
 
242
- field(:length, :type => Integer, :default => 0)
243
- field(:chunkSize, :type => Integer, :default => (256 * (2 ** 20)))
244
- field(:uploadDate, :type => Date, :default => Time.now.utc)
245
- field(:md5, :type => String, :default => Digest::MD5.hexdigest(''))
287
+ field(:length, :type => Integer, :default => 0)
288
+ field(:chunkSize, :type => Integer, :default => (256 * (2 ** 20)))
289
+ field(:uploadDate, :type => Date, :default => Time.now.utc)
290
+ field(:md5, :type => String, :default => Digest::MD5.hexdigest(''))
246
291
 
247
- %w( filename contentType length chunkSize uploadDate md5 ).each do |f|
248
- validates_presence_of(f)
249
- end
250
- validates_uniqueness_of(:filename)
292
+ %w( filename contentType length chunkSize uploadDate md5 ).each do |f|
293
+ validates_presence_of(f)
294
+ end
295
+ validates_uniqueness_of(:filename)
251
296
 
252
- has_many(:chunks, :class_name => chunk_model_name, :inverse_of => :files, :dependent => :destroy, :order => [:n, :asc])
297
+ has_many(:chunks, :class_name => chunk_model_name, :inverse_of => :files, :dependent => :destroy, :order => [:n, :asc])
253
298
 
254
- index({:filename => 1}, :unique => true)
299
+ index({:filename => 1}, :unique => true)
255
300
 
256
- def path
257
- filename
258
- end
301
+ def path
302
+ filename
303
+ end
259
304
 
260
- def basename
261
- ::File.basename(filename)
262
- end
305
+ def basename
306
+ ::File.basename(filename)
307
+ end
263
308
 
264
- def prefix
265
- self.class.namespace.prefix
266
- end
309
+ def prefix
310
+ self.class.namespace.prefix
311
+ end
267
312
 
268
- def each(&block)
269
- chunks.all.order_by([:n, :asc]).each do |chunk|
270
- block.call(chunk.to_s)
313
+ def each(&block)
314
+ chunks.all.order_by([:n, :asc]).each do |chunk|
315
+ block.call(chunk.to_s)
316
+ end
271
317
  end
272
- end
273
318
 
274
- def data
275
- data = ''
276
- each{|chunk| data << chunk}
277
- data
278
- end
319
+ def data
320
+ data = ''
321
+ each{|chunk| data << chunk}
322
+ data
323
+ end
279
324
 
280
- def base64
281
- Array(to_s).pack('m')
282
- end
325
+ def base64
326
+ Array(to_s).pack('m')
327
+ end
283
328
 
284
- def data_uri(options = {})
285
- data = base64.chomp
286
- "data:#{ content_type };base64,".concat(data)
287
- end
329
+ def data_uri(options = {})
330
+ data = base64.chomp
331
+ "data:#{ content_type };base64,".concat(data)
332
+ end
288
333
 
289
- def bytes(&block)
290
- if block
291
- each{|data| block.call(data)}
292
- length
293
- else
294
- bytes = []
295
- each{|data| bytes.push(*data)}
296
- bytes
334
+ def bytes(&block)
335
+ if block
336
+ each{|data| block.call(data)}
337
+ length
338
+ else
339
+ bytes = []
340
+ each{|data| bytes.push(*data)}
341
+ bytes
342
+ end
297
343
  end
298
- end
299
344
 
300
- def close
301
- self
302
- end
345
+ def close
346
+ self
347
+ end
303
348
 
304
- def content_type
305
- contentType
306
- end
349
+ def content_type
350
+ contentType
351
+ end
307
352
 
308
- def update_date
309
- updateDate
310
- end
353
+ def update_date
354
+ updateDate
355
+ end
311
356
 
312
- def created_at
313
- updateDate
314
- end
357
+ def created_at
358
+ updateDate
359
+ end
315
360
 
316
- def namespace
317
- self.class.namespace
361
+ def namespace
362
+ self.class.namespace
363
+ end
318
364
  end
319
365
  end
320
- end
321
366
 
322
- ##
323
- #
324
- def GridFS.build_chunk_model_for(namespace)
325
- prefix = namespace.name.split(/::/).last.downcase
326
- file_model_name = "#{ namespace.name }::File"
327
- chunk_model_name = "#{ namespace.name }::Chunk"
367
+ ##
368
+ #
369
+ def GridFS.build_chunk_model_for(namespace)
370
+ prefix = namespace.name.split(/::/).last.downcase
371
+ file_model_name = "#{ namespace.name }::File"
372
+ chunk_model_name = "#{ namespace.name }::Chunk"
328
373
 
329
- Class.new do
330
- include Mongoid::Document
374
+ Class.new do
375
+ include Mongoid::Document
331
376
 
332
- singleton_class = class << self; self; end
377
+ singleton_class = class << self; self; end
333
378
 
334
- singleton_class.instance_eval do
335
- define_method(:name){ chunk_model_name }
336
- attr_accessor :file_model
337
- attr_accessor :namespace
338
- end
379
+ singleton_class.instance_eval do
380
+ define_method(:name){ chunk_model_name }
381
+ attr_accessor :file_model
382
+ attr_accessor :namespace
383
+ end
339
384
 
340
- self.default_collection_name = "#{ prefix }.chunks"
385
+ self.default_collection_name = "#{ prefix }.chunks"
341
386
 
342
- field(:n, :type => Integer, :default => 0)
343
- field(:data, :type => Moped::BSON::Binary)
387
+ field(:n, :type => Integer, :default => 0)
388
+ field(:data, :type => (defined?(Moped) ? Moped::BSON::Binary : BSON::Binary))
344
389
 
345
- belongs_to(:file, :foreign_key => :files_id, :class_name => file_model_name)
390
+ belongs_to(:file, :foreign_key => :files_id, :class_name => file_model_name)
346
391
 
347
- index({:files_id => 1, :n => -1}, :unique => true)
392
+ index({:files_id => 1, :n => -1}, :unique => true)
348
393
 
349
- def namespace
350
- self.class.namespace
351
- end
352
-
353
- def to_s
354
- data.data
355
- end
394
+ def namespace
395
+ self.class.namespace
396
+ end
356
397
 
357
- alias_method 'to_str', 'to_s'
358
- end
359
- end
398
+ def to_s
399
+ data.data
400
+ end
360
401
 
361
- ##
362
- #
363
- def GridFS.reading(arg, &block)
364
- if arg.respond_to?(:read)
365
- rewind(arg) do |io|
366
- block.call(io)
367
- end
368
- else
369
- open(arg.to_s) do |io|
370
- block.call(io)
402
+ alias_method 'to_str', 'to_s'
371
403
  end
372
404
  end
373
- end
374
405
 
375
- def GridFS.rewind(io, &block)
376
- begin
377
- pos = io.pos
378
- io.flush
379
- io.rewind
380
- rescue
381
- nil
406
+ ##
407
+ #
408
+ def GridFS.reading(arg, &block)
409
+ if arg.respond_to?(:read)
410
+ rewind(arg) do |io|
411
+ block.call(io)
412
+ end
413
+ else
414
+ open(arg.to_s) do |io|
415
+ block.call(io)
416
+ end
417
+ end
382
418
  end
383
419
 
384
- begin
385
- block.call(io)
386
- ensure
420
+ def GridFS.rewind(io, &block)
387
421
  begin
388
- io.pos = pos
422
+ pos = io.pos
423
+ io.flush
424
+ io.rewind
389
425
  rescue
390
426
  nil
391
427
  end
428
+
429
+ begin
430
+ block.call(io)
431
+ ensure
432
+ begin
433
+ io.pos = pos
434
+ rescue
435
+ nil
436
+ end
437
+ end
392
438
  end
393
- end
394
439
 
395
- def GridFS.extract_basename(object)
396
- filename = nil
397
- [:original_path, :original_filename, :path, :filename, :pathname].each do |msg|
398
- if object.respond_to?(msg)
399
- filename = object.send(msg)
400
- break
440
+ def GridFS.extract_basename(object)
441
+ filename = nil
442
+ [:original_path, :original_filename, :path, :filename, :pathname].each do |msg|
443
+ if object.respond_to?(msg)
444
+ filename = object.send(msg)
445
+ break
446
+ end
401
447
  end
448
+ filename ? cleanname(filename) : nil
402
449
  end
403
- filename ? cleanname(filename) : nil
404
- end
405
450
 
406
- def GridFS.extract_content_type(filename)
407
- content_type = MIME::Types.type_for(::File.basename(filename.to_s)).first
408
- content_type.to_s if content_type
409
- end
451
+ def GridFS.extract_content_type(filename)
452
+ content_type = MIME::Types.type_for(::File.basename(filename.to_s)).first
453
+ content_type.to_s if content_type
454
+ end
410
455
 
411
- def GridFS.cleanname(pathname)
412
- basename = ::File.basename(pathname.to_s)
413
- CGI.unescape(basename).gsub(%r/[^0-9a-zA-Z_@)(~.-]/, '_').gsub(%r/_+/,'_')
456
+ def GridFS.cleanname(pathname)
457
+ basename = ::File.basename(pathname.to_s)
458
+ CGI.unescape(basename).gsub(%r/[^0-9a-zA-Z_@)(~.-]/, '_').gsub(%r/_+/,'_')
459
+ end
414
460
  end
415
- end
416
461
 
417
- GridFs = GridFS
462
+ ##
463
+ #
464
+ GridFs = GridFS
418
465
 
419
- GridFS.init!
466
+ GridFS.init!
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "mongoid-grid_fs"
6
- spec.version = "1.0.0"
6
+ spec.version = "1.1.0"
7
7
  spec.platform = Gem::Platform::RUBY
8
8
  spec.summary = "mongoid-grid_fs"
9
9
  spec.description = "a mongoid 3/moped compatible implementation of the grid_fs specification"
@@ -26,6 +26,10 @@ Gem::Specification::new do |spec|
26
26
  spec.test_files = nil
27
27
 
28
28
 
29
+ spec.add_dependency(*["mongoid", " >= 3.0.1"])
30
+
31
+ spec.add_dependency(*["mime-types", " >= 1.19"])
32
+
29
33
 
30
34
  spec.extensions.push(*[])
31
35
 
@@ -142,6 +142,8 @@ Testing GridFs do
142
142
 
143
143
  protected
144
144
  def object_id_re
145
- %r| \w{#{ BSON::ObjectId.new.to_s.size }} |iomx
145
+ object_id = defined?(Moped) ? Moped::BSON::ObjectId.new : BSON::ObjectId.new
146
+
147
+ %r| \w{#{ object_id.to_s.size }} |iomx
146
148
  end
147
149
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-grid_fs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,40 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-23 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-07-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: mime-types
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '1.19'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '1.19'
14
46
  description: a mongoid 3/moped compatible implementation of the grid_fs specification
15
47
  email: ara.t.howard@gmail.com
16
48
  executables: []