mongoid-grid_fs 2.6.0 → 2.7.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 731132f0dc42c4d794d1bfb55d276fec747898dea7b33205b674be4481d49cb6
4
- data.tar.gz: e38362e6fb7ec45e513281b9d8411cb51af3c3c838fd9cd20be2d83963412f17
3
+ metadata.gz: 95ada6ecabaffde5d369340eb5386d00e8918e59df9207380aa5842390302ae8
4
+ data.tar.gz: 424d0918d982f2b8c669cf8a6877cfb0a33a8883f3e667ea6e6413daf17528e1
5
5
  SHA512:
6
- metadata.gz: 2bddb50d507a94136b4370cedf504c5182461d293495b234c91c804145ea4dae27eb5185ec46212300fe93282fdd0273faed708c26034a75c1ffca6c08ede751
7
- data.tar.gz: 6f0561fdf134bc8f27d934babe67a377fa4c17982287c5de1b815331a4b55d2f28243fd91ac90a6c67b1660fe8409cd24eb56157fdf6882a3f11a0f6b334da89
6
+ metadata.gz: d89fa0a3cb32cd2130895c805f16b50c6f888e13af8f56330dabfcff320403d36f8ba48f309ff4097e2b369e48f488ceb691005741911ed431e651b1dfe80e2d
7
+ data.tar.gz: fa50db5f98bd9cbcca026a5ae0fc882b8cc9d9c83961ee9c947c82aad50d10e558630f508dd41779cf51d961082a394fd2cc4824fb8fb0993f67f76979bfc080
data/README.md CHANGED
@@ -1,21 +1,18 @@
1
- mongoid-grid_fs
2
- ---------------
1
+ # Mongoid::GridFs
3
2
 
4
- [![Gem Version](https://badge.fury.io/rb/mongoid-grid_fs.svg)](https://badge.fury.io/rb/mongoid-grid_fs)
5
- [![CI](https://github.com/mongoid/mongoid-grid_fs/actions/workflows/ci.yml/badge.svg)](https://github.com/mongoid/mongoid-grid_fs/actions/workflows/ci.yml)
3
+ [![Gem Version](https://badge.fury.io/rb/mongoid-grid_fs.svg)](https://badge.fury.io/rb/mongoid-grid_fs) [![CI](https://github.com/mongoid/mongoid-grid_fs/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/mongoid/mongoid-grid_fs/actions/workflows/ci.yml) [![Lint](https://github.com/mongoid/mongoid-grid_fs/actions/workflows/lint.yml/badge.svg?branch=master)](https://github.com/mongoid/mongoid-grid_fs/actions/workflows/lint.yml) [![Coverage Status](https://coveralls.io/repos/github/mongoid/mongoid-grid_fs/badge.svg?branch=master)](https://coveralls.io/github/mongoid/mongoid-grid_fs?branch=master)
6
4
 
7
- A pure Mongoid/Moped implementation of the MongoDB GridFS specification
5
+ A pure Mongoid implementation of the [MongoDB GridFS specification](https://www.mongodb.com/docs/manual/core/gridfs/).
8
6
 
9
- INSTALL
10
- -------
7
+ ## Installation
11
8
 
12
- ```
13
- gem install mongoid-grid_fs
14
- ```
9
+ Add the gem to your Gemfile and run `bundle install`.
15
10
 
11
+ ```ruby
12
+ gem 'mongoid-grid_fs'
13
+ ```
16
14
 
17
- SYNOPSIS
18
- --------
15
+ ## Usage
19
16
 
20
17
  ```ruby
21
18
  require 'mongoid/grid_fs'
@@ -29,47 +26,52 @@ grid_fs.delete(f.id)
29
26
  g = grid_fs.get(f.id)
30
27
  g.data # big huge blob
31
28
  g.each { |chunk| file.write(chunk) } # streaming write
32
-
33
29
  ```
34
30
 
35
- DESCRIPTION
36
- -----------
37
-
38
- mongoid_grid_fs is A pure Mongoid/Moped implementation of the MongoDB GridFS specification
39
-
40
- Reference: http://docs.mongodb.org/manual/reference/gridfs/
31
+ ## Features
41
32
 
42
- It has the following features:
33
+ - Implemented on top of Mongoid for portability, using the driver as little as possible.
34
+ - A simple, REST-like API.
35
+ - Custom namespaces, such as `fs.files` or `image.files`, as the specification allows.
36
+ - Pathnames and IO-like objects can be written to the grid.
37
+ - `put` generates unique pathnames by default to avoid collisions, for example `path/info/a.rb` becomes `$object_id/a.rb`.
38
+ - `[]` and `[]=` let you use the grid like a giant file hash.
39
+ - Data URIs, for example `image_tag file.data_uri`.
43
40
 
44
- - implementation is on top of mongoid for portability. moped (the driver) is
45
- barely used, so the library should be quite durable except in the face of
46
- massive changes to mongoid itself.
41
+ ## Querying and Storage
47
42
 
48
- - simple, REST-like api
43
+ `where` returns chainable Mongoid criteria for multiple files, while `find` returns the first match:
49
44
 
50
- - support for custom namespaces (fs.files vs. image.files, as per the spec)
45
+ ```ruby
46
+ Mongoid::GridFs.where(filename: 'report.pdf').each { |file| puts file.id }
47
+ Mongoid::GridFs.where(contentType: 'application/pdf').limit(10)
48
+ ```
51
49
 
52
- - pathnames and io-like objects can be written to the grid
50
+ To use `attachments.files` and `attachments.chunks` instead of `fs.files` and `fs.chunks`, use a named namespace for all operations:
53
51
 
54
- - auto-unique pathnames are generated (by default) to avoid collisions using #put
52
+ ```ruby
53
+ attachments = Mongoid::GridFs.namespace_for(:attachments)
54
+ file = attachments.put(readable, filename: 'report.pdf')
55
+ attachments.where(filename: 'report.pdf')
56
+ attachments.get(file.id)
57
+ attachments.delete(file.id)
58
+ ```
55
59
 
56
- 'path/info/a.rb' -> '$object_id/a.rb'
60
+ For a separate host on Mongoid 5+, configure a named Mongoid client (for example `gridfs`) in `mongoid.yml`, then select it on both models during application initialization, before any GridFS operations:
57
61
 
58
- - [] and []= methods which allow the grid to be used like a giant file
59
- hash in the sky
62
+ ```ruby
63
+ attachments.file_model.store_in client: :gridfs
64
+ attachments.chunk_model.store_in client: :gridfs
65
+ ```
60
66
 
61
- - support for data_uris, like a html5 boss
67
+ The named client supplies the hosts, database, and credentials through normal Mongoid configuration. To change only the database, use `store_in database: 'attachments_database'` on both models instead. Keep files and chunks on the same client and database. These settings are shared model configuration, not per-request switches. Mongoid 3/4 use named sessions and `store_in session: :gridfs` instead of clients.
62
68
 
63
- ```erb
64
- <%= image_tag :src => file.data_uri %>
65
- ```
69
+ Use `file.destroy`, `grid_fs.delete(id)`, or `namespace.clear` to remove files and associated chunks. Mongoid's callback-skipping operations such as `file.delete`, `delete_all`, and direct collection deletes do not perform dependent destruction and can leave orphaned chunks.
66
70
 
67
- CONTRIBUTING
68
- ------------
71
+ ## Contributing
69
72
 
70
- See [CONTRIBUTING](CONTRIBUTING.md).
73
+ See [CONTRIBUTING.md](CONTRIBUTING.md) and [CHANGELOG.md](CHANGELOG.md).
71
74
 
72
- LICENSE
73
- -------
75
+ ## License
74
76
 
75
- This is licensed under the Ruby License: http://www.ruby-lang.org/en/about/license.txt
77
+ Licensed under the [Ruby License](https://www.ruby-lang.org/en/about/license.txt), see [LICENSE](LICENSE).
data/Rakefile CHANGED
@@ -1,4 +1,16 @@
1
1
  require 'bundler/gem_tasks'
2
+
3
+ if RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3') &&
4
+ Gem.loaded_specs.key?('ruby-gem-release')
5
+ require_relative 'script/release'
6
+ MongoidGridFsRelease.workflow.install_tasks
7
+
8
+ %w[release release:rubygem_push release:source_control_push].each do |name|
9
+ Rake::Task[name].clear
10
+ desc 'Disabled; releases run through GitHub Actions, see RELEASING.md'
11
+ task(name) { abort "#{name} is disabled; releases run through GitHub Actions, see RELEASING.md" }
12
+ end
13
+ end
2
14
  require 'bundler/setup'
3
15
  require 'rake/testtask'
4
16
 
@@ -9,7 +21,14 @@ Rake::TestTask.new(:test) do |t|
9
21
  t.verbose = false
10
22
  end
11
23
 
12
- require 'rubocop/rake_task'
13
- RuboCop::RakeTask.new(:rubocop)
24
+ lint = RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3') &&
25
+ Gem.loaded_specs.key?('rubocop')
26
+ if lint
27
+ require 'rubocop/rake_task'
28
+ RuboCop::RakeTask.new(:rubocop)
29
+
30
+ desc 'Lint Ruby, Markdown, YAML, and spelling'
31
+ task lint: :rubocop
32
+ end
14
33
 
15
- task default: %i[rubocop test]
34
+ task default: lint ? %i[rubocop test] : %i[test]
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  class GridFs
3
- VERSION = '2.6.0'.freeze
3
+ VERSION = '2.7.0'.freeze
4
4
  end
5
5
  end
@@ -24,6 +24,7 @@ module Mongoid
24
24
  put
25
25
  get
26
26
  delete
27
+ where
27
28
  find
28
29
  []
29
30
  []=
@@ -152,7 +153,7 @@ module Mongoid
152
153
  end
153
154
 
154
155
  def binary_for(*buf)
155
- if defined?(Moped::BSON)
156
+ if defined?(Moped::BSON::Binary)
156
157
  Moped::BSON::Binary.new(:generic, buf.join)
157
158
  else
158
159
  BSON::Binary.new(buf.join, :generic)
@@ -353,7 +354,7 @@ module Mongoid
353
354
  end
354
355
 
355
356
  def base64
356
- Array(to_s).pack('m')
357
+ Array(data).pack('m')
357
358
  end
358
359
 
359
360
  def data_uri(_options = {})
@@ -415,7 +416,7 @@ module Mongoid
415
416
  store_in collection: "#{prefix}.chunks"
416
417
 
417
418
  field(:n, type: Integer, default: 0)
418
- field(:data, type: (defined?(Moped::BSON) ? Moped::BSON::Binary : BSON::Binary))
419
+ field(:data, type: (defined?(Moped::BSON::Binary) ? Moped::BSON::Binary : BSON::Binary))
419
420
 
420
421
  belongs_to(:file, foreign_key: :files_id, class_name: file_model_name)
421
422
 
metadata CHANGED
@@ -1,43 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-grid_fs
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.0
4
+ version: 2.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ara T. Howard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-19 00:00:00.000000000 Z
11
+ date: 2026-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: mongoid-danger
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
17
  - - "~>"
32
18
  - !ruby/object:Gem::Version
33
- version: '0.2'
19
+ version: '5.0'
34
20
  type: :development
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
38
24
  - - "~>"
39
25
  - !ruby/object:Gem::Version
40
- version: '0.2'
26
+ version: '5.0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: pry
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -66,20 +52,6 @@ dependencies:
66
52
  - - ">="
67
53
  - !ruby/object:Gem::Version
68
54
  version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: rubocop
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: 1.36.0
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: 1.36.0
83
55
  - !ruby/object:Gem::Dependency
84
56
  name: mime-types
85
57
  requirement: !ruby/object:Gem::Requirement
@@ -136,13 +108,11 @@ files:
136
108
  - lib/mongoid-grid_fs.rb
137
109
  - lib/mongoid/grid_fs.rb
138
110
  - lib/mongoid/grid_fs/version.rb
139
- - test/helper.rb
140
- - test/mongoid-grid_fs_test.rb
141
- - test/testing.rb
142
111
  homepage: https://github.com/mongoid/mongoid-grid_fs
143
112
  licenses:
144
113
  - Ruby
145
- metadata: {}
114
+ metadata:
115
+ rubygems_mfa_required: 'true'
146
116
  post_install_message:
147
117
  rdoc_options: []
148
118
  require_paths:
@@ -158,11 +128,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
128
  - !ruby/object:Gem::Version
159
129
  version: '0'
160
130
  requirements: []
161
- rubygems_version: 3.5.16
131
+ rubygems_version: 3.5.22
162
132
  signing_key:
163
133
  specification_version: 4
164
134
  summary: A MongoDB GridFS implementation for Mongoid
165
- test_files:
166
- - test/helper.rb
167
- - test/mongoid-grid_fs_test.rb
168
- - test/testing.rb
135
+ test_files: []
data/test/helper.rb DELETED
@@ -1,23 +0,0 @@
1
- require 'rails'
2
- require 'stringio'
3
-
4
- class SIO < StringIO
5
- attr_accessor :filename
6
-
7
- def initialize(filename, *args, &block)
8
- @filename = filename
9
- super(*args, &block)
10
- end
11
- end
12
-
13
- require_relative 'testing'
14
- require_relative '../lib/mongoid/grid_fs'
15
-
16
- Mongoid.configure do |config|
17
- config.connect_to('mongoid-grid_fs_test')
18
- end
19
-
20
- Mongo::Logger.logger.level = Logger::INFO if defined?(Mongo) == 'constant'
21
-
22
- # Avoid annoying deprecation warning
23
- I18n.enforce_available_locales = false if I18n.respond_to?(:enforce_available_locales=)
@@ -1,237 +0,0 @@
1
- require_relative 'helper'
2
-
3
- _testing Mongoid::GridFs do
4
- ##
5
- #
6
- GridFs = Mongoid::GridFs
7
-
8
- prepare do
9
- GridFs::File.delete_all
10
- GridFs::Chunk.delete_all
11
- end
12
-
13
- ##
14
- #
15
- context '#put' do
16
- test 'default' do
17
- filename = __FILE__
18
- basename = File.basename(filename)
19
-
20
- g = assert { GridFs.put(filename) }
21
-
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) }
25
- end
26
-
27
- test 'with a :filename' do
28
- filename = 'path/info/a.rb'
29
-
30
- g = assert { GridFs.put(__FILE__, filename: filename) }
31
-
32
- assert { g.filename == filename }
33
- end
34
-
35
- test 'with your own attributes' do
36
- my_value = 'my_value'
37
-
38
- g = assert { GridFs.put(__FILE__, my_value: my_value) }
39
-
40
- assert { g.my_value == my_value }
41
- end
42
-
43
- test 'when error occurs (eg. missing file)' do
44
- file = '/path/to/missing'
45
-
46
- assert_raises(Errno::ENOENT) { GridFs.put(file) }
47
- end
48
- end
49
-
50
- ##
51
- #
52
- context '#get' do
53
- test 'default' do
54
- id = assert { GridFs::File.last.id }
55
- assert { GridFs.get(id) }
56
- end
57
- end
58
-
59
- ##
60
- #
61
- context '#delete' do
62
- test 'default' do
63
- id = assert { GridFs::File.last.id }
64
- assert { GridFs.get(id) }
65
- assert { GridFs.delete(id) }
66
- assert_raises(Mongoid::Errors::DocumentNotFound) { GridFs.get(id) }
67
- end
68
- end
69
-
70
- ##
71
- #
72
- context '[] and []=' do
73
- test 'default' do
74
- path = 'a.rb'
75
- data = IO.read(__FILE__)
76
-
77
- sio = SIO.new(path, data)
78
-
79
- g = assert { (GridFs[path] = sio) && GridFs[path] }
80
-
81
- assert { g.data == data }
82
- assert { g.content_type == 'application/x-ruby' }
83
-
84
- before = GridFs::File.count
85
-
86
- data = "#{Time.now.to_f}-#{rand}"
87
-
88
- assert { GridFs[path] = SIO.new(path, data) }
89
- assert { GridFs[path].data == data }
90
-
91
- after = GridFs::File.count
92
-
93
- created = after - before
94
-
95
- assert { created == 1 }
96
- end
97
-
98
- ##
99
- #
100
- context 'data uris' do
101
- test 'default' do
102
- id = assert { GridFs::File.last.id }
103
- g = assert { GridFs.get(id) }
104
-
105
- content_type = g.content_type
106
- base64 = [g.to_s].pack('m').chomp
107
-
108
- data_uri = "data:#{content_type};base64,".concat(base64)
109
-
110
- assert { g.data_uri == data_uri }
111
- end
112
- end
113
-
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
122
-
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
128
-
129
- test 'just a single param' do
130
- id = assert { GridFs::File.last.id }
131
- g = assert { GridFs.get(id) }
132
-
133
- assert { g.data[1] == g.slice(1) }
134
- end
135
-
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
141
-
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
147
-
148
- test 'multiple chunks...' do
149
- path = 'slice_and_dice.txt'
150
-
151
- assert { GridFs[path] = SIO.new(path, 'foobar' * 256 * 1024) }
152
-
153
- g = GridFs[path]
154
-
155
- assert { g.chunks.count > 0 }
156
- assert { g.data[10, (256 * 1024 * 2)] == g.slice(10, (256 * 1024 * 2)) }
157
- end
158
- end
159
-
160
- ##
161
- #
162
- context 'iterating each chunk' do
163
- test 'having file size more than 42mb' do
164
- require 'tempfile'
165
-
166
- orig, copy = %w[orig copy].map do |suffix|
167
- Tempfile.new("mongoid-grid_fs~43mb.#{suffix}")
168
- end
169
-
170
- kilobyte = 'x' * 1024
171
- (43 * 1024).times { orig.write(kilobyte) }
172
-
173
- GridFs.get(GridFs.put(orig.path).id).each do |chunk|
174
- copy.print(chunk.to_s)
175
- end
176
-
177
- assert { File.size(orig.path) == File.size(copy.path) }
178
- end
179
- end
180
-
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
189
-
190
- test 'new' do
191
- ns = GridFs.namespace_for(:ns)
192
-
193
- assert { ns.prefix == 'ns' }
194
-
195
- assert { ns.file_model < Mongoid::Document }
196
- assert { ns.file_model.collection_name.to_s == 'ns.files' }
197
-
198
- assert { ns.chunk_model < Mongoid::Document }
199
- assert { ns.chunk_model.collection_name.to_s == 'ns.chunks' }
200
-
201
- assert { ns.file_model.destroy_all }
202
-
203
- count = GridFs::File.count
204
-
205
- assert { ns.file_model.count.zero? }
206
- assert { ns.put __FILE__ }
207
- assert { ns.file_model.count == 1 }
208
-
209
- assert { count == GridFs::File.count }
210
- end
211
- end
212
- end
213
-
214
- ##
215
- #
216
- context 'rails' do
217
- test 'paths' do
218
- testdir = File.dirname(__FILE__)
219
- gemdir = File.dirname(testdir)
220
- libdir = File.join(gemdir, 'lib')
221
-
222
- expanded = proc { |paths| Array(paths).map { |path| File.expand_path(path) } }
223
-
224
- assert do
225
- expanded[Mongoid::GridFs::Engine.paths['app/models']] == expanded[libdir]
226
- end
227
- end
228
- end
229
-
230
- protected
231
-
232
- def object_id_re
233
- object_id = defined?(Moped::BSON) ? Moped::BSON::ObjectId.new : BSON::ObjectId.new
234
-
235
- / \w{#{ object_id.to_s.size }} /iomx
236
- end
237
- end
data/test/testing.rb DELETED
@@ -1,196 +0,0 @@
1
-
2
- require 'minitest/autorun'
3
-
4
- testdir = __dir__
5
- rootdir = File.dirname(testdir)
6
- libdir = File.join(rootdir, 'lib')
7
-
8
- STDOUT.sync = true
9
-
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
-
14
- class Testing
15
- class Slug < ::String
16
- def self.for(*args)
17
- string = args.flatten.compact.join('-')
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
- new(words.join('-').downcase)
22
- end
23
- end
24
-
25
- class Context
26
- attr_accessor :name
27
-
28
- def initialize(name, *_args)
29
- @name = name
30
- end
31
-
32
- def to_s
33
- Slug.for(name)
34
- end
35
- end
36
- end
37
-
38
- def _testing(*args, &block)
39
- Class.new(::Minitest::Test) do
40
- i_suck_and_my_tests_are_order_dependent!
41
-
42
- ## class methods
43
- #
44
- class << self
45
- def contexts
46
- @contexts ||= []
47
- end
48
-
49
- def context(*args, &block)
50
- return contexts.last if args.empty? && block.nil?
51
-
52
- context = Testing::Context.new(*args)
53
- contexts.push(context)
54
-
55
- begin
56
- yield(context)
57
- ensure
58
- contexts.pop
59
- end
60
- end
61
-
62
- def slug_for(*args)
63
- string = [context, args].flatten.compact.join('-')
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
- words.join('-').downcase.sub(/_$/, '')
68
- end
69
-
70
- def name
71
- const_get(:Name)
72
- end
73
-
74
- def testno
75
- format('%05d', (@testno ||= 0))
76
- ensure
77
- @testno += 1
78
- end
79
-
80
- def testing(*args, &block)
81
- method = ['test', testno, slug_for(*args)].delete_if(&:empty?).join('_')
82
- define_method(method, &block)
83
- end
84
-
85
- def test(*args, &block)
86
- testing(*args, &block)
87
- end
88
-
89
- def setup(&block)
90
- define_method(:setup, &block) if block
91
- end
92
-
93
- def teardown(&block)
94
- define_method(:teardown, &block) if block
95
- end
96
-
97
- def prepare(&block)
98
- @prepare ||= []
99
- @prepare.push(block) if block
100
- @prepare
101
- end
102
-
103
- def cleanup(&block)
104
- @cleanup ||= []
105
- @cleanup.push(block) if block
106
- @cleanup
107
- end
108
- end
109
-
110
- ## configure the subclass!
111
- #
112
- const_set(:Testno, '0')
113
- slug = slug_for(*args).tr('-', '_')
114
- name = ['TESTING', format('%03d', const_get(:Testno)), slug].delete_if(&:empty?).join('_')
115
- name = name.upcase!
116
- const_set(:Name, name)
117
- const_set(:Missing, Object.new.freeze)
118
-
119
- ## instance methods
120
- #
121
- alias_method('__assert__', 'assert')
122
-
123
- def assert(*args, &block)
124
- if (args.size == 1) && args.first.is_a?(Hash)
125
- options = args.first
126
- expected = getopt(:expected, options) { missing }
127
- actual = getopt(:actual, options) { missing }
128
- actual, expected, = options.to_a.flatten if (expected == missing) && (actual == missing)
129
- expected = expected.call if expected.respond_to?(:call)
130
- actual = actual.call if actual.respond_to?(:call)
131
- assert_equal(expected, actual)
132
- end
133
-
134
- result = if block
135
- label = "assert(#{args.join(' ')})"
136
- yield
137
- else
138
- result = args.shift
139
- label = "assert(#{args.join(' ')})"
140
- result
141
- end
142
- __assert__(result, label)
143
- result
144
- end
145
-
146
- def missing
147
- self.class.const_get(:Missing)
148
- end
149
-
150
- def getopt(opt, hash, options = nil, &block)
151
- [opt.to_s, opt.to_s.to_sym].each do |key|
152
- return hash[key] if hash.key?(key)
153
- end
154
- default =
155
- if block
156
- yield
157
- else
158
- options.is_a?(Hash) ? options[:default] : nil
159
- end
160
- default
161
- end
162
-
163
- def subclass_of(exception)
164
- class << exception
165
- def ==(other)
166
- super || self > other
167
- end
168
- end
169
- exception
170
- end
171
-
172
- ##
173
- #
174
- module_eval(&block)
175
-
176
- setup
177
- prepare.each(&:call)
178
-
179
- at_exit do
180
- teardown
181
- cleanup.each(&:call)
182
- end
183
-
184
- self
185
- end
186
- end
187
-
188
- if $PROGRAM_NAME == __FILE__
189
-
190
- _testing 'Testing' do
191
- testing('foo') { assert true }
192
- test { assert true }
193
- p instance_methods.grep(/test/)
194
- end
195
-
196
- end