with_model 2.1.0 → 2.1.1
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 +4 -4
- data/.gitignore +0 -13
- data/.yardopts +1 -0
- data/CHANGELOG.md +13 -0
- data/README.md +13 -3
- data/lib/with_model.rb +8 -0
- data/lib/with_model/constant_stubber.rb +5 -3
- data/lib/with_model/methods.rb +3 -0
- data/lib/with_model/model.rb +5 -0
- data/lib/with_model/model/dsl.rb +1 -0
- data/lib/with_model/table.rb +9 -1
- data/lib/with_model/version.rb +1 -1
- data/spec/constant_stubber_spec.rb +19 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a9994ddf84f8fe654f6670173ea2cf6d8a739c1033bc17ca9765001fc0f8f50
|
4
|
+
data.tar.gz: f432e1faa7d071b4219dadf9f64a4d7202d67d2fcb0c95b01157848be12252d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7654d570583cea72d142bf0d70416d539704198f7ca9d447fb52b1917e016f05622a4d7f53a03ca6c3002d61ce3a2301ddae4ee6982312ff0c3e5d2b57fa4f7c
|
7
|
+
data.tar.gz: b90401f8f508ab719cddd294dd5bcc104c09e075df83f55e65e4884cfc0202ed38a1240a8098dc2443fbeba3c6957d392ec17bf717e121d82922e6bdefc128ec
|
data/.gitignore
CHANGED
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
### 2.1.1
|
2
|
+
|
3
|
+
- Don't fail erroneously when running the after hook when the before hook failed
|
4
|
+
- Improve documentation
|
5
|
+
|
6
|
+
### 2.1.0
|
7
|
+
|
8
|
+
- Support creating constant in an existing namespace (#26)
|
9
|
+
- Prefer keyword arguments over manually-validated hash argument
|
10
|
+
- Improve documentation
|
11
|
+
- Remove support for obsolete Ruby
|
12
|
+
- Internal cleanup
|
13
|
+
|
1
14
|
### 2.0.0
|
2
15
|
|
3
16
|
- Require Ruby 2.1 or later
|
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
[](https://rubygems.org/gems/with_model)
|
4
4
|
[](https://travis-ci.org/Casecommons/with_model)
|
5
|
+
[](https://www.rubydoc.info/gems/with_model)
|
5
6
|
|
6
7
|
`with_model` dynamically builds an ActiveRecord model (with table) before each test in a group and destroys it afterwards.
|
7
8
|
|
@@ -161,9 +162,18 @@ end
|
|
161
162
|
|
162
163
|
## Requirements
|
163
164
|
|
164
|
-
|
165
|
-
|
166
|
-
-
|
165
|
+
See the [gemspec metadata](https://rubygems.org/gems/with_model) for dependency requirements. RSpec and minitest are indirect dependencies, and `with_model` should support any maintained version of both.
|
166
|
+
|
167
|
+
## Thread-safety
|
168
|
+
|
169
|
+
- A unique table name is used for tables generated via `with_model`/`WithModel::Model.new`. This allows `with_model` (when limited to this API) to run concurrently (in processes or threads) with a single database schema. While there is a possibility of collision, it is very small.
|
170
|
+
- A user-supplied table name is used for tables generated via `with_table`/`WithModel::Table.new`. This may cause collisions at runtime if tests are run concurrently against a single database schema, unless the caller takes care to ensure the table names passed as arguments are unique across threads/processes.
|
171
|
+
- Generated models are created in stubbed constants, which are global; no guarantee is made to the uniqueness of a constant, and this may be unsafe.
|
172
|
+
- Generated classes are ActiveRecord subclasses:
|
173
|
+
- This library makes no guarantee is made as to the thread-safety of creating ActiveRecord subclasses concurrently.
|
174
|
+
- This library makes no guarantee is made as to the thread-safety of cleaning up ActiveRecord/ActiveSupport’s internals which are polluted upon class creation.
|
175
|
+
|
176
|
+
In general, `with_model` is not guaranteed to be thread-safe, but is, in certain usages, safe to use concurrently across multiple processes with a single database schema.
|
167
177
|
|
168
178
|
## Versioning
|
169
179
|
|
data/lib/with_model.rb
CHANGED
@@ -6,6 +6,10 @@ require 'with_model/table'
|
|
6
6
|
require 'with_model/version'
|
7
7
|
|
8
8
|
module WithModel
|
9
|
+
# @param name The constant name (as a symbol) to assign the model class to.
|
10
|
+
# @param scope Passed to `before`/`after` in the test context.
|
11
|
+
# @param options Passed to {WithModel::Model#initialize}.
|
12
|
+
# @param block Yielded an instance of {WithModel::Model::DSL}.
|
9
13
|
def with_model(name, scope: nil, **options, &block)
|
10
14
|
model = Model.new name, options
|
11
15
|
dsl = Model::DSL.new model
|
@@ -20,6 +24,10 @@ module WithModel
|
|
20
24
|
end
|
21
25
|
end
|
22
26
|
|
27
|
+
# @param name The table name (as a symbol) to create.
|
28
|
+
# @param scope Passed to `before`/`after` in the test context.
|
29
|
+
# @param options Passed to {WithModel::Table#initialize}.
|
30
|
+
# @param block Passed to {WithModel::Table#initialize} (like {WithModel::Model::DSL#table}).
|
23
31
|
def with_table(name, scope: nil, **options, &block)
|
24
32
|
table = Table.new name, options, &block
|
25
33
|
|
@@ -19,9 +19,11 @@ module WithModel
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def unstub_const
|
22
|
-
@namespace
|
23
|
-
|
24
|
-
|
22
|
+
if @namespace
|
23
|
+
@namespace.__send__ :remove_const, basename
|
24
|
+
@namespace.const_set basename, @original_value if @original_value
|
25
|
+
@namespace = nil
|
26
|
+
end
|
25
27
|
@original_value = nil
|
26
28
|
end
|
27
29
|
|
data/lib/with_model/methods.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module WithModel
|
4
|
+
# Extended into all ActiveRecord models created by with_model.
|
4
5
|
module Methods
|
6
|
+
# Since model classes not created by with_model won't have this
|
7
|
+
# method, one should instead test `respond_to?(:with_model?)`.
|
5
8
|
def with_model?
|
6
9
|
true
|
7
10
|
end
|
data/lib/with_model/model.rb
CHANGED
@@ -8,9 +8,14 @@ require 'with_model/methods'
|
|
8
8
|
require 'with_model/table'
|
9
9
|
|
10
10
|
module WithModel
|
11
|
+
# In general, direct use of this class should be avoided. Instead use
|
12
|
+
# either the {WithModel high-level API} or {WithModel::Model::DSL low-level API}.
|
11
13
|
class Model
|
12
14
|
attr_writer :model_block, :table_block, :table_options
|
13
15
|
|
16
|
+
# @param name The constant name (as a symbol) to assign the model class to.
|
17
|
+
# @param superclass The superclass for the created class. Should
|
18
|
+
# have `ActiveRecord::Base` as an ancestor.
|
14
19
|
def initialize(name, superclass: ActiveRecord::Base)
|
15
20
|
@name = name.to_sym
|
16
21
|
@model_block = nil
|
data/lib/with_model/model/dsl.rb
CHANGED
data/lib/with_model/table.rb
CHANGED
@@ -3,20 +3,28 @@
|
|
3
3
|
require 'active_record'
|
4
4
|
|
5
5
|
module WithModel
|
6
|
+
# In general, direct use of this class should be avoided. Instead use
|
7
|
+
# either the {WithModel high-level API} or {WithModel::Model::DSL low-level API}.
|
6
8
|
class Table
|
9
|
+
# @param name The name of the table to create.
|
10
|
+
# @param options Passed to ActiveRecord `create_table`.
|
11
|
+
# @param block Passed to ActiveRecord `create_table`.
|
12
|
+
# @see https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-create_table
|
7
13
|
def initialize(name, options = {}, &block)
|
8
14
|
@name = name.freeze
|
9
15
|
@options = options.freeze
|
10
16
|
@block = block
|
11
17
|
end
|
12
18
|
|
19
|
+
# Creates the table with the initialized options. Drops the table if
|
20
|
+
# it already exists.
|
13
21
|
def create
|
14
22
|
connection.drop_table(@name) if exists?
|
15
23
|
connection.create_table(@name, @options, &@block)
|
16
24
|
end
|
17
25
|
|
18
26
|
def destroy
|
19
|
-
|
27
|
+
connection.drop_table(@name)
|
20
28
|
end
|
21
29
|
|
22
30
|
private
|
data/lib/with_model/version.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module WithModel
|
6
|
+
describe ConstantStubber do
|
7
|
+
it 'allows calling unstub_const multiple times' do
|
8
|
+
stubber = described_class.new('Foo')
|
9
|
+
stubber.stub_const(1)
|
10
|
+
stubber.unstub_const
|
11
|
+
stubber.unstub_const
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'allows calling unstub_const without stub_const' do
|
15
|
+
stubber = described_class.new('Foo')
|
16
|
+
stubber.unstub_const
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: with_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Case Commons, LLC
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2018-
|
13
|
+
date: 2018-11-04 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -116,6 +116,7 @@ files:
|
|
116
116
|
- ".rspec"
|
117
117
|
- ".rubocop.yml"
|
118
118
|
- ".travis.yml"
|
119
|
+
- ".yardopts"
|
119
120
|
- CHANGELOG.md
|
120
121
|
- Gemfile
|
121
122
|
- LICENSE
|
@@ -130,6 +131,7 @@ files:
|
|
130
131
|
- lib/with_model/table.rb
|
131
132
|
- lib/with_model/version.rb
|
132
133
|
- spec/active_record_behaviors_spec.rb
|
134
|
+
- spec/constant_stubber_spec.rb
|
133
135
|
- spec/readme_spec.rb
|
134
136
|
- spec/spec_helper.rb
|
135
137
|
- spec/with_model_spec.rb
|
@@ -154,12 +156,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
156
|
version: '0'
|
155
157
|
requirements: []
|
156
158
|
rubyforge_project:
|
157
|
-
rubygems_version: 2.7.
|
159
|
+
rubygems_version: 2.7.6
|
158
160
|
signing_key:
|
159
161
|
specification_version: 4
|
160
162
|
summary: Dynamically build a model within an RSpec context
|
161
163
|
test_files:
|
162
164
|
- spec/active_record_behaviors_spec.rb
|
165
|
+
- spec/constant_stubber_spec.rb
|
163
166
|
- spec/readme_spec.rb
|
164
167
|
- spec/spec_helper.rb
|
165
168
|
- spec/with_model_spec.rb
|