mongoid-grid_fs 2.3.0 → 2.4.0

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.
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  class GridFs
3
- VERSION = "2.3.0"
3
+ VERSION = '2.4.0'.freeze
4
4
  end
5
5
  end
@@ -1,8 +1,8 @@
1
1
  require_relative 'helper'
2
2
 
3
3
  Testing Mongoid::GridFs do
4
- ##
5
- #
4
+ ##
5
+ #
6
6
  GridFs = Mongoid::GridFs
7
7
 
8
8
  prepare do
@@ -10,239 +10,228 @@ Testing Mongoid::GridFs do
10
10
  GridFs::Chunk.delete_all
11
11
  end
12
12
 
13
- ##
14
- #
13
+ ##
14
+ #
15
15
  context '#put' do
16
-
17
16
  test 'default' do
18
17
  filename = __FILE__
19
18
  basename = File.basename(filename)
20
19
 
21
- g = assert{ GridFs.put(filename) }
20
+ g = assert { GridFs.put(filename) }
22
21
 
23
- assert{ g.filename =~ %r| #{ object_id_re } / #{ basename } \Z|imox }
24
- assert{ g.content_type == "application/x-ruby" }
25
- assert{ g.data == IO.read(filename) }
22
+ assert { g.filename =~ %r{ #{ object_id_re } / #{ basename } \Z}imox }
23
+ assert { g.content_type == 'application/x-ruby' }
24
+ assert { g.data == IO.read(filename) }
26
25
  end
27
26
 
28
27
  test 'with a :filename' do
29
28
  filename = 'path/info/a.rb'
30
29
 
31
- g = assert{ GridFs.put(__FILE__, :filename => filename) }
30
+ g = assert { GridFs.put(__FILE__, filename: filename) }
32
31
 
33
- assert{ g.filename == filename }
32
+ assert { g.filename == filename }
34
33
  end
35
34
 
36
35
  test 'with your own attributes' do
37
- my_value = "my_value"
36
+ my_value = 'my_value'
38
37
 
39
- g = assert{ GridFs.put(__FILE__, :my_value => my_value) }
38
+ g = assert { GridFs.put(__FILE__, my_value: my_value) }
40
39
 
41
- assert{ g.my_value == my_value }
40
+ assert { g.my_value == my_value }
42
41
  end
43
42
 
44
43
  test 'when error occurs (eg. missing file)' do
45
44
  file = '/path/to/missing'
46
45
 
47
- assert_raises(Errno::ENOENT){ GridFs.put(file) }
46
+ assert_raises(Errno::ENOENT) { GridFs.put(file) }
48
47
  end
49
-
50
48
  end
51
49
 
52
-
53
- ##
54
- #
50
+ ##
51
+ #
55
52
  context '#get' do
56
-
57
53
  test 'default' do
58
- id = assert{ GridFs::File.last.id }
59
- g = assert{ GridFs.get(id) }
54
+ id = assert { GridFs::File.last.id }
55
+ g = assert { GridFs.get(id) }
60
56
  end
61
-
62
57
  end
63
58
 
64
- ##
65
- #
59
+ ##
60
+ #
66
61
  context '#delete' do
67
-
68
62
  test 'default' do
69
- id = assert{ GridFs::File.last.id }
70
- g = assert{ GridFs.get(id) }
71
- assert{ GridFs.delete(id) }
72
- assert_raises(Mongoid::Errors::DocumentNotFound){ GridFs.get(id) }
63
+ id = assert { GridFs::File.last.id }
64
+ g = assert { GridFs.get(id) }
65
+ assert { GridFs.delete(id) }
66
+ assert_raises(Mongoid::Errors::DocumentNotFound) { GridFs.get(id) }
73
67
  end
74
-
75
68
  end
76
69
 
77
- ##
78
- #
70
+ ##
71
+ #
79
72
  context '[] and []=' do
80
-
81
73
  test 'default' do
82
74
  path = 'a.rb'
83
75
  data = IO.read(__FILE__)
84
76
 
85
77
  sio = SIO.new(path, data)
86
78
 
87
- g = assert{ GridFs[path] = sio and GridFs[path] }
79
+ g = assert { (GridFs[path] = sio) && GridFs[path] }
88
80
 
89
- assert{ g.data == data }
90
- assert{ g.content_type == "application/x-ruby" }
81
+ assert { g.data == data }
82
+ assert { g.content_type == 'application/x-ruby' }
91
83
 
92
84
  before = GridFs::File.count
93
85
 
94
- data = "#{ Time.now.to_f }-#{ rand }"
86
+ data = "#{Time.now.to_f}-#{rand}"
95
87
 
96
- assert{ GridFs[path] = SIO.new(path, data) }
97
- assert{ GridFs[path].data == data }
88
+ assert { GridFs[path] = SIO.new(path, data) }
89
+ assert { GridFs[path].data == data }
98
90
 
99
91
  after = GridFs::File.count
100
92
 
101
93
  created = after - before
102
94
 
103
- assert{ created == 1 }
95
+ assert { created == 1 }
104
96
  end
105
97
 
106
- ##
107
- #
108
- context 'data uris' do
98
+ ##
99
+ #
100
+ context 'data uris' do
101
+ test 'default' do
102
+ id = assert { GridFs::File.last.id }
103
+ g = assert { GridFs.get(id) }
109
104
 
110
- test 'default' do
111
- id = assert{ GridFs::File.last.id }
112
- g = assert{ GridFs.get(id) }
105
+ content_type = g.content_type
106
+ base64 = [g.to_s].pack('m').chomp
113
107
 
114
- content_type = g.content_type
115
- base64 = [g.to_s].pack('m').chomp
108
+ data_uri = "data:#{content_type};base64,".concat(base64)
116
109
 
117
- data_uri = "data:#{ content_type };base64,".concat(base64)
118
-
119
- assert{ g.data_uri == data_uri }
110
+ assert { g.data_uri == data_uri }
111
+ end
120
112
  end
121
113
 
122
- end
123
-
124
- ##
125
- #
126
- context 'slicing and dicing' do
127
-
128
- test 'range' do
129
- id = assert { GridFs::File.last.id }
130
- g = assert { GridFs.get(id) }
131
- assert { g.data[1..3] == g.slice(1..3) }
132
- end
114
+ ##
115
+ #
116
+ context 'slicing and dicing' do
117
+ test 'range' do
118
+ id = assert { GridFs::File.last.id }
119
+ g = assert { GridFs.get(id) }
120
+ assert { g.data[1..3] == g.slice(1..3) }
121
+ end
133
122
 
134
- test 'start and length' do
135
- id = assert { GridFs::File.last.id }
136
- g = assert { GridFs.get(id) }
137
- assert { g.data[1, 3] == g.slice(1, 3) }
138
- end
123
+ test 'start and length' do
124
+ id = assert { GridFs::File.last.id }
125
+ g = assert { GridFs.get(id) }
126
+ assert { g.data[1, 3] == g.slice(1, 3) }
127
+ end
139
128
 
140
- test 'just a single param' do
141
- id = assert { GridFs::File.last.id }
142
- g = assert {GridFs.get(id) }
129
+ test 'just a single param' do
130
+ id = assert { GridFs::File.last.id }
131
+ g = assert { GridFs.get(id) }
143
132
 
144
- assert {g.data[1] == g.slice(1) }
145
- end
133
+ assert { g.data[1] == g.slice(1) }
134
+ end
146
135
 
147
- test 'getting the last index' do
148
- id = assert { GridFs::File.last.id }
149
- g = assert {GridFs.get(id) }
150
- assert {g.data[-1] == g.slice(-1) }
151
- end
136
+ test 'getting the last index' do
137
+ id = assert { GridFs::File.last.id }
138
+ g = assert { GridFs.get(id) }
139
+ assert { g.data[-1] == g.slice(-1) }
140
+ end
152
141
 
153
- test 'yanking from the end of the data' do
154
- id = assert { GridFs::File.last.id }
155
- g = assert {GridFs.get(id) }
156
- assert {g.data[-3, 2] == g.slice(-3, 2) }
157
- end
142
+ test 'yanking from the end of the data' do
143
+ id = assert { GridFs::File.last.id }
144
+ g = assert { GridFs.get(id) }
145
+ assert { g.data[-3, 2] == g.slice(-3, 2) }
146
+ end
158
147
 
159
- test 'multiple chunks...' do
160
- path = 'slice_and_dice.txt'
148
+ test 'multiple chunks...' do
149
+ path = 'slice_and_dice.txt'
161
150
 
162
- assert { GridFs[path] = SIO.new(path, "foobar" * 256 * 1024) }
151
+ assert { GridFs[path] = SIO.new(path, 'foobar' * 256 * 1024) }
163
152
 
164
- g = GridFs[path]
153
+ g = GridFs[path]
165
154
 
166
- assert { g.chunks.count > 0 }
167
- assert { g.data[10, (256 * 1024 * 2)] == g.slice(10, (256 * 1024 * 2)) }
155
+ assert { g.chunks.count > 0 }
156
+ assert { g.data[10, (256 * 1024 * 2)] == g.slice(10, (256 * 1024 * 2)) }
157
+ end
168
158
  end
169
- end
170
159
 
171
- ##
172
- #
173
- context 'iterating each chunk' do
174
- test 'having file size more than 42mb' do
175
- require 'tempfile'
160
+ ##
161
+ #
162
+ context 'iterating each chunk' do
163
+ test 'having file size more than 42mb' do
164
+ require 'tempfile'
176
165
 
177
- orig, copy = %w{orig copy}.map do |suffix|
178
- Tempfile.new("mongoid-grid_fs~43mb.#{suffix}")
179
- end
166
+ orig, copy = %w(orig copy).map do |suffix|
167
+ Tempfile.new("mongoid-grid_fs~43mb.#{suffix}")
168
+ end
180
169
 
181
- kilobyte = "x" * 1024
182
- (43*1024).times { orig.write(kilobyte) }
170
+ kilobyte = 'x' * 1024
171
+ (43 * 1024).times { orig.write(kilobyte) }
183
172
 
184
- GridFs.get(GridFs.put(orig.path).id).each do |chunk|
185
- copy.print(chunk.to_s)
186
- end
173
+ GridFs.get(GridFs.put(orig.path).id).each do |chunk|
174
+ copy.print(chunk.to_s)
175
+ end
187
176
 
188
- assert { File.size(orig.path) == File.size(copy.path) }
177
+ assert { File.size(orig.path) == File.size(copy.path) }
178
+ end
189
179
  end
190
- end
191
180
 
192
- ##
193
- #
194
- context 'namespaces' do
195
- test 'default' do
196
- assert{ GridFs.namespace.prefix == 'fs' }
197
- assert{ GridFs.file_model.collection_name.to_s == 'fs.files' }
198
- assert{ GridFs.chunk_model.collection_name.to_s == 'fs.chunks' }
199
- end
181
+ ##
182
+ #
183
+ context 'namespaces' do
184
+ test 'default' do
185
+ assert { GridFs.namespace.prefix == 'fs' }
186
+ assert { GridFs.file_model.collection_name.to_s == 'fs.files' }
187
+ assert { GridFs.chunk_model.collection_name.to_s == 'fs.chunks' }
188
+ end
200
189
 
201
- test 'new' do
202
- ns = GridFs.namespace_for(:ns)
190
+ test 'new' do
191
+ ns = GridFs.namespace_for(:ns)
203
192
 
204
- assert{ ns.prefix == 'ns' }
193
+ assert { ns.prefix == 'ns' }
205
194
 
206
- assert{ ns.file_model < Mongoid::Document }
207
- assert{ ns.file_model.collection_name.to_s == 'ns.files' }
195
+ assert { ns.file_model < Mongoid::Document }
196
+ assert { ns.file_model.collection_name.to_s == 'ns.files' }
208
197
 
209
- assert{ ns.chunk_model < Mongoid::Document }
210
- assert{ ns.chunk_model.collection_name.to_s == 'ns.chunks' }
198
+ assert { ns.chunk_model < Mongoid::Document }
199
+ assert { ns.chunk_model.collection_name.to_s == 'ns.chunks' }
211
200
 
212
- assert{ ns.file_model.destroy_all }
201
+ assert { ns.file_model.destroy_all }
213
202
 
214
- count = GridFs::File.count
203
+ count = GridFs::File.count
215
204
 
216
- assert{ ns.file_model.count == 0}
217
- assert{ ns.put __FILE__ }
218
- assert{ ns.file_model.count == 1}
205
+ assert { ns.file_model.count == 0 }
206
+ assert { ns.put __FILE__ }
207
+ assert { ns.file_model.count == 1 }
219
208
 
220
- assert{ count == GridFs::File.count }
209
+ assert { count == GridFs::File.count }
210
+ end
221
211
  end
222
212
  end
223
213
 
224
- end
225
-
226
- ##
227
- #
214
+ ##
215
+ #
228
216
  context 'rails' do
229
217
  test 'paths' do
230
218
  testdir = File.dirname(__FILE__)
231
219
  gemdir = File.dirname(testdir)
232
220
  libdir = File.join(gemdir, 'lib')
233
221
 
234
- expanded = proc{|paths| Array(paths).map{|path| File.expand_path(path)}}
222
+ expanded = proc { |paths| Array(paths).map { |path| File.expand_path(path) } }
235
223
 
236
- assert{
237
- expanded[ Mongoid::GridFs::Engine.paths['app/models'] ] == expanded[ libdir ]
238
- }
224
+ assert do
225
+ expanded[Mongoid::GridFs::Engine.paths['app/models']] == expanded[libdir]
226
+ end
239
227
  end
240
228
  end
241
229
 
242
230
  protected
231
+
243
232
  def object_id_re
244
233
  object_id = defined?(Moped::BSON) ? Moped::BSON::ObjectId.new : BSON::ObjectId.new
245
234
 
246
- %r| \w{#{ object_id.to_s.size }} |iomx
235
+ / \w{#{ object_id.to_s.size }} /iomx
247
236
  end
248
237
  end
@@ -7,17 +7,17 @@ libdir = File.join(rootdir, 'lib')
7
7
 
8
8
  STDOUT.sync = true
9
9
 
10
- $:.unshift(testdir) unless $:.include?(testdir)
11
- $:.unshift(libdir) unless $:.include?(libdir)
12
- $:.unshift(rootdir) unless $:.include?(rootdir)
10
+ $LOAD_PATH.unshift(testdir) unless $LOAD_PATH.include?(testdir)
11
+ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
12
+ $LOAD_PATH.unshift(rootdir) unless $LOAD_PATH.include?(rootdir)
13
13
 
14
14
  class Testing
15
15
  class Slug < ::String
16
- def Slug.for(*args)
16
+ def self.for(*args)
17
17
  string = args.flatten.compact.join('-')
18
- words = string.to_s.scan(%r/\w+/)
19
- words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
20
- words.delete_if{|word| word.nil? or word.strip.empty?}
18
+ words = string.to_s.scan(/\w+/)
19
+ words.map! { |word| word.gsub /[^0-9a-zA-Z_-]/, '' }
20
+ words.delete_if { |word| word.nil? || word.strip.empty? }
21
21
  new(words.join('-').downcase)
22
22
  end
23
23
  end
@@ -25,7 +25,7 @@ class Testing
25
25
  class Context
26
26
  attr_accessor :name
27
27
 
28
- def initialize(name, *args)
28
+ def initialize(name, *_args)
29
29
  @name = name
30
30
  end
31
31
 
@@ -39,21 +39,21 @@ def Testing(*args, &block)
39
39
  Class.new(::Minitest::Test) do
40
40
  i_suck_and_my_tests_are_order_dependent!
41
41
 
42
- ## class methods
43
- #
42
+ ## class methods
43
+ #
44
44
  class << self
45
45
  def contexts
46
46
  @contexts ||= []
47
47
  end
48
48
 
49
49
  def context(*args, &block)
50
- return contexts.last if(args.empty? and block.nil?)
50
+ return contexts.last if args.empty? && block.nil?
51
51
 
52
52
  context = Testing::Context.new(*args)
53
53
  contexts.push(context)
54
54
 
55
55
  begin
56
- block.call(context)
56
+ yield(context)
57
57
  ensure
58
58
  contexts.pop
59
59
  end
@@ -61,22 +61,24 @@ def Testing(*args, &block)
61
61
 
62
62
  def slug_for(*args)
63
63
  string = [context, args].flatten.compact.join('-')
64
- words = string.to_s.scan(%r/\w+/)
65
- words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
66
- words.delete_if{|word| word.nil? or word.strip.empty?}
64
+ words = string.to_s.scan(/\w+/)
65
+ words.map! { |word| word.gsub /[^0-9a-zA-Z_-]/, '' }
66
+ words.delete_if { |word| word.nil? || word.strip.empty? }
67
67
  words.join('-').downcase.sub(/_$/, '')
68
68
  end
69
69
 
70
- def name() const_get(:Name) end
70
+ def name
71
+ const_get(:Name)
72
+ end
71
73
 
72
- def testno()
74
+ def testno
73
75
  '%05d' % (@testno ||= 0)
74
76
  ensure
75
77
  @testno += 1
76
78
  end
77
79
 
78
80
  def testing(*args, &block)
79
- method = ["test", testno, slug_for(*args)].delete_if{|part| part.empty?}.join('_')
81
+ method = ['test', testno, slug_for(*args)].delete_if(&:empty?).join('_')
80
82
  define_method(method, &block)
81
83
  end
82
84
 
@@ -105,43 +107,43 @@ def Testing(*args, &block)
105
107
  end
106
108
  end
107
109
 
108
- ## configure the subclass!
109
- #
110
+ ## configure the subclass!
111
+ #
110
112
  const_set(:Testno, '0')
111
- slug = slug_for(*args).gsub(%r/-/,'_')
112
- name = ['TESTING', '%03d' % const_get(:Testno), slug].delete_if{|part| part.empty?}.join('_')
113
+ slug = slug_for(*args).tr('-', '_')
114
+ name = ['TESTING', '%03d' % const_get(:Testno), slug].delete_if(&:empty?).join('_')
113
115
  name = name.upcase!
114
116
  const_set(:Name, name)
115
117
  const_set(:Missing, Object.new.freeze)
116
118
 
117
- ## instance methods
118
- #
119
+ ## instance methods
120
+ #
119
121
  alias_method('__assert__', 'assert')
120
122
 
121
123
  def assert(*args, &block)
122
- if args.size == 1 and args.first.is_a?(Hash)
124
+ if (args.size == 1) && args.first.is_a?(Hash)
123
125
  options = args.first
124
- expected = getopt(:expected, options){ missing }
125
- actual = getopt(:actual, options){ missing }
126
- if expected == missing and actual == missing
126
+ expected = getopt(:expected, options) { missing }
127
+ actual = getopt(:actual, options) { missing }
128
+ if (expected == missing) && (actual == missing)
127
129
  actual, expected, *ignored = options.to_a.flatten
128
130
  end
129
- expected = expected.call() if expected.respond_to?(:call)
130
- actual = actual.call() if actual.respond_to?(:call)
131
+ expected = expected.call if expected.respond_to?(:call)
132
+ actual = actual.call if actual.respond_to?(:call)
131
133
  assert_equal(expected, actual)
132
134
  end
133
135
 
134
- if block
135
- label = "assert(#{ args.join(' ') })"
136
- result = nil
137
- result = block.call
138
- __assert__(result, label)
139
- result
140
- else
141
- result = args.shift
142
- label = "assert(#{ args.join(' ') })"
143
- __assert__(result, label)
144
- result
136
+ result = if block
137
+ label = "assert(#{args.join(' ')})"
138
+ result = nil
139
+ result = yield
140
+ __assert__(result, label)
141
+ result
142
+ else
143
+ result = args.shift
144
+ label = "assert(#{args.join(' ')})"
145
+ __assert__(result, label)
146
+ result
145
147
  end
146
148
  end
147
149
 
@@ -151,46 +153,47 @@ def Testing(*args, &block)
151
153
 
152
154
  def getopt(opt, hash, options = nil, &block)
153
155
  [opt.to_s, opt.to_s.to_sym].each do |key|
154
- return hash[key] if hash.has_key?(key)
156
+ return hash[key] if hash.key?(key)
155
157
  end
156
158
  default =
157
159
  if block
158
- block.call
160
+ yield
159
161
  else
160
162
  options.is_a?(Hash) ? options[:default] : nil
161
163
  end
162
- return default
164
+ default
163
165
  end
164
166
 
165
- def subclass_of exception
167
+ def subclass_of(exception)
166
168
  class << exception
167
- def ==(other) super or self > other end
169
+ def ==(other)
170
+ super || self > other
171
+ end
168
172
  end
169
173
  exception
170
174
  end
171
175
 
172
- ##
173
- #
176
+ ##
177
+ #
174
178
  module_eval(&block)
175
179
 
176
- self.setup()
177
- self.prepare.each{|b| b.call()}
180
+ setup
181
+ prepare.each(&:call)
178
182
 
179
- at_exit{
180
- self.teardown()
181
- self.cleanup.each{|b| b.call()}
182
- }
183
+ at_exit do
184
+ teardown
185
+ cleanup.each(&:call)
186
+ end
183
187
 
184
188
  self
185
189
  end
186
190
  end
187
191
 
188
-
189
- if $0 == __FILE__
192
+ if $PROGRAM_NAME == __FILE__
190
193
 
191
194
  Testing 'Testing' do
192
- testing('foo'){ assert true }
193
- test{ assert true }
195
+ testing('foo') { assert true }
196
+ test { assert true }
194
197
  p instance_methods.grep(/test/)
195
198
  end
196
199