test-prof 0.3.0.pre → 0.3.0.pre2
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/lib/test_prof/recipes/rspec/let_it_be.rb +34 -10
- data/lib/test_prof/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e8016e347e65b9c7be1918f7d9b34f6cf0da920
|
4
|
+
data.tar.gz: 93e9805bad937f7eb0e2e730274c61e58018c52a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c837ef686608e8110cd6224e4fa2a942e287c5dc492dd6d8eb21b58399feed435e5470b8b91abfd8cc6f767360b6dd6800156d91b70145dca141477875f150a6
|
7
|
+
data.tar.gz: f800cc10fb99658fd3757259ca1e37afd2ccfab496b4f96bc455d09e781bdc2a2ed80128515ee8081918a933121af70643fa5f736ca4209b5824c3913dfbfeb8
|
@@ -6,18 +6,27 @@ module TestProf
|
|
6
6
|
# Just like `let`, but persist the result for the whole group.
|
7
7
|
# NOTE: Experimental and magical, for more control use `before_all`.
|
8
8
|
module LetItBe
|
9
|
+
class << self
|
10
|
+
def module_for(group)
|
11
|
+
modules.fetch(group) do
|
12
|
+
Module.new.tap { |mod| group.prepend(mod) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def modules
|
19
|
+
@modules ||= {}
|
20
|
+
end
|
21
|
+
end
|
9
22
|
# Use uniq prefix for instance variables to avoid collisions
|
10
23
|
# We want to use the power of Ruby's unicode support)
|
11
24
|
# And we love cats!)
|
12
25
|
PREFIX = "@😸"
|
13
26
|
|
14
|
-
def let_it_be(identifier,
|
15
|
-
raise ArgumentError, "Block is required!" unless block_given?
|
16
|
-
|
17
|
-
this = self
|
18
|
-
|
27
|
+
def let_it_be(identifier, **options, &block)
|
19
28
|
initializer = proc do
|
20
|
-
|
29
|
+
instance_variable_set(:"#{PREFIX}#{identifier}", instance_exec(&block))
|
21
30
|
end
|
22
31
|
|
23
32
|
if within_before_all?
|
@@ -26,20 +35,35 @@ module TestProf
|
|
26
35
|
before_all(&initializer)
|
27
36
|
end
|
28
37
|
|
29
|
-
|
38
|
+
define_let_it_be_methods(identifier, **options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def define_let_it_be_methods(identifier, reload: false, refind: false)
|
42
|
+
let_accessor = -> { instance_variable_get(:"#{PREFIX}#{identifier}") }
|
30
43
|
|
31
|
-
let_accessor = -> {
|
44
|
+
let_accessor = -> { instance_variable_get(:"#{PREFIX}#{identifier}")&.reload } if reload
|
32
45
|
|
33
46
|
if refind
|
34
47
|
let_accessor = lambda do
|
35
|
-
record =
|
48
|
+
record = instance_variable_get(:"#{PREFIX}#{identifier}")
|
36
49
|
next unless record.is_a?(::ActiveRecord::Base)
|
37
50
|
|
38
51
|
record.class.find(record.send(record.class.primary_key))
|
39
52
|
end
|
40
53
|
end
|
41
54
|
|
42
|
-
|
55
|
+
LetItBe.module_for(self).module_eval do
|
56
|
+
define_method(identifier) do
|
57
|
+
# Trying to detect the context (couldn't find other way so far)
|
58
|
+
if @__inspect_output =~ /\(:context\)/
|
59
|
+
instance_variable_get(:"#{PREFIX}#{identifier}")
|
60
|
+
else
|
61
|
+
# Fallback to let definition
|
62
|
+
super()
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
43
67
|
let(identifier, &let_accessor)
|
44
68
|
end
|
45
69
|
end
|
data/lib/test_prof/version.rb
CHANGED