mongoid-grid_fs 1.4.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ require 'mongoid-grid_fs.rb' unless defined?(Mongoid::GridFS::Fs::Chunk)
2
+ Mongoid::GridFS::Fs::Chunk
@@ -0,0 +1,2 @@
1
+ require 'mongoid-grid_fs.rb' unless defined?(Mongoid::GridFS::Fs::File)
2
+ Mongoid::GridFS::Fs::File
@@ -0,0 +1,2 @@
1
+ require 'mongoid-grid_fs.rb' unless defined?(Mongoid::GridFS)
2
+ Mongoid::GridFS
@@ -2,7 +2,7 @@
2
2
  #
3
3
  module Mongoid
4
4
  class GridFS
5
- const_set :Version, '1.4.0'
5
+ const_set :Version, '1.7.0'
6
6
 
7
7
  class << GridFS
8
8
  def version
@@ -269,6 +269,23 @@
269
269
  end
270
270
  end
271
271
 
272
+ ##
273
+ #
274
+ class Defaults < ::Hash
275
+ def method_missing(method, *args, &block)
276
+ case method.to_s
277
+ when /(.*)=/
278
+ key = $1
279
+ val = args.first
280
+ update(key => val)
281
+ else
282
+ key = method.to_s
283
+ super unless has_key?(key)
284
+ fetch(key)
285
+ end
286
+ end
287
+ end
288
+
272
289
  ##
273
290
  #
274
291
  def GridFS.build_file_model_for(namespace)
@@ -283,17 +300,22 @@
283
300
 
284
301
  singleton_class.instance_eval do
285
302
  define_method(:name){ file_model_name }
286
- attr_accessor :chunk_model
287
303
  attr_accessor :namespace
304
+ attr_accessor :chunk_model
305
+ attr_accessor :defaults
288
306
  end
289
307
 
290
308
  self.default_collection_name = "#{ prefix }.files"
309
+ self.defaults = Defaults.new
310
+
311
+ self.defaults.chunkSize = 4 * (mb = 2**20)
312
+ self.defaults.contentType = 'application/octet-stream'
291
313
 
292
314
  field(:filename, :type => String)
293
- field(:contentType, :type => String, :default => 'application/octet-stream')
315
+ field(:contentType, :type => String, :default => defaults.contentType)
294
316
 
295
317
  field(:length, :type => Integer, :default => 0)
296
- field(:chunkSize, :type => Integer, :default => (256 * (2 ** 10)))
318
+ field(:chunkSize, :type => Integer, :default => defaults.chunkSize)
297
319
  field(:uploadDate, :type => Date, :default => Time.now.utc)
298
320
  field(:md5, :type => String, :default => Digest::MD5.hexdigest(''))
299
321
 
@@ -324,6 +346,32 @@
324
346
  end
325
347
  end
326
348
 
349
+ def slice(*args)
350
+ case args.first
351
+ when Range
352
+ range = args.first
353
+ first_chunk = (range.min / chunkSize).floor
354
+ last_chunk = (range.max / chunkSize).ceil
355
+ offset = range.min % chunkSize
356
+ length = range.max - range.min + 1
357
+ when Fixnum
358
+ start = args.first
359
+ start = self.length + start if start < 0
360
+ length = args.size == 2 ? args.last : 1
361
+ first_chunk = (start / chunkSize).floor
362
+ last_chunk = ((start + length) / chunkSize).ceil
363
+ offset = start % chunkSize
364
+ end
365
+
366
+ data = ''
367
+
368
+ chunks.where(:n => (first_chunk..last_chunk)).order_by(n: 'asc').each do |chunk|
369
+ data << chunk
370
+ end
371
+
372
+ data[offset, length]
373
+ end
374
+
327
375
  def data
328
376
  data = ''
329
377
  each{|chunk| data << chunk}
@@ -491,3 +539,11 @@
491
539
  GridFs = GridFS
492
540
  GridFS.init!
493
541
  end
542
+
543
+ ##
544
+ #
545
+ if defined?(Rails)
546
+ class Mongoid::GridFS::Engine < Rails::Engine
547
+ paths['app/models'] = File.dirname(__FILE__)
548
+ end
549
+ end
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "mongoid-grid_fs"
6
- spec.version = "1.4.0"
6
+ spec.version = "1.7.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"
@@ -12,6 +12,14 @@ Gem::Specification::new do |spec|
12
12
  ["README.md",
13
13
  "Rakefile",
14
14
  "lib",
15
+ "lib/app",
16
+ "lib/app/models",
17
+ "lib/app/models/mongoid",
18
+ "lib/app/models/mongoid/grid_fs",
19
+ "lib/app/models/mongoid/grid_fs.rb",
20
+ "lib/app/models/mongoid/grid_fs/fs",
21
+ "lib/app/models/mongoid/grid_fs/fs/chunk.rb",
22
+ "lib/app/models/mongoid/grid_fs/fs/file.rb",
15
23
  "lib/mongoid-grid_fs.rb",
16
24
  "mongoid-grid_fs.gemspec",
17
25
  "test",
data/test/helper.rb CHANGED
@@ -1,10 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
- require_relative 'testing'
3
- require_relative '../lib/mongoid-grid_fs.rb'
4
-
5
- Mongoid.configure do |config|
6
- config.connect_to('mongoid-grid_fs_test')
7
- end
8
2
 
9
3
  require 'stringio'
10
4
 
@@ -16,3 +10,15 @@ class SIO < StringIO
16
10
  super(*args, &block)
17
11
  end
18
12
  end
13
+
14
+ # this triggers mongoid to load rails...
15
+ module Rails; end
16
+
17
+ require_relative 'testing'
18
+ require_relative '../lib/mongoid-grid_fs.rb'
19
+
20
+ Mongoid.configure do |config|
21
+ config.connect_to('mongoid-grid_fs_test')
22
+ end
23
+
24
+
@@ -107,6 +107,53 @@ Testing Mongoid::GridFs do
107
107
 
108
108
  end
109
109
 
110
+ ##
111
+ #
112
+ context 'slicing and dicing' do
113
+
114
+ test 'range' do
115
+ id = assert { GridFS::File.last.id }
116
+ g = assert { GridFs.get(id) }
117
+ assert { g.data[1..3] == g.slice(1..3) }
118
+ end
119
+
120
+ test 'start and length' do
121
+ id = assert { GridFS::File.last.id }
122
+ g = assert { GridFs.get(id) }
123
+ assert { g.data[1, 3] == g.slice(1, 3) }
124
+ end
125
+
126
+ test 'just a single param' do
127
+ id = assert { GridFS::File.last.id }
128
+ g = assert {GridFs.get(id) }
129
+
130
+ assert {g.data[1] == g.slice(1) }
131
+ end
132
+
133
+ test 'getting the last index' do
134
+ id = assert { GridFS::File.last.id }
135
+ g = assert {GridFs.get(id) }
136
+ assert {g.data[-1] == g.slice(-1) }
137
+ end
138
+
139
+ test 'yanking from the end of the data' do
140
+ id = assert { GridFS::File.last.id }
141
+ g = assert {GridFs.get(id) }
142
+ assert {g.data[-3, 2] == g.slice(-3, 2) }
143
+ end
144
+
145
+ test 'multiple chunks...' do
146
+ path = 'slice_and_dice.txt'
147
+
148
+ assert { GridFs[path] = SIO.new(path, "foobar" * 256 * 1024) }
149
+
150
+ g = GridFs[path]
151
+
152
+ assert { g.chunks.count > 0 }
153
+ assert { g.data[10, (256 * 1024 * 2)] == g.slice(10, (256 * 1024 * 2)) }
154
+ end
155
+ end
156
+
110
157
  ##
111
158
  #
112
159
  context 'namespaces' do
@@ -143,6 +190,19 @@ Testing Mongoid::GridFs do
143
190
 
144
191
  ##
145
192
  #
193
+ context 'rails' do
194
+ test 'paths' do
195
+ testdir = File.dirname(__FILE__)
196
+ gemdir = File.dirname(testdir)
197
+ libdir = File.join(gemdir, 'lib')
198
+
199
+ expanded = proc{|paths| Array(paths).map{|path| File.expand_path(path)}}
200
+
201
+ assert{
202
+ expanded[ Mongoid::GridFS::Engine.paths['app/models'] ] == expanded[ libdir ]
203
+ }
204
+ end
205
+ end
146
206
 
147
207
  protected
148
208
  def object_id_re
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.4.0
4
+ version: 1.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-05 00:00:00.000000000 Z
12
+ date: 2012-11-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -51,6 +51,9 @@ extra_rdoc_files: []
51
51
  files:
52
52
  - README.md
53
53
  - Rakefile
54
+ - lib/app/models/mongoid/grid_fs.rb
55
+ - lib/app/models/mongoid/grid_fs/fs/chunk.rb
56
+ - lib/app/models/mongoid/grid_fs/fs/file.rb
54
57
  - lib/mongoid-grid_fs.rb
55
58
  - mongoid-grid_fs.gemspec
56
59
  - test/helper.rb