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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b81e0cb37ac0120a12a8497dc9de6fe7d14c4c53
4
- data.tar.gz: b5f060c7d0ce985e5e4508be8cc2f161efe9389b
3
+ metadata.gz: 7e8016e347e65b9c7be1918f7d9b34f6cf0da920
4
+ data.tar.gz: 93e9805bad937f7eb0e2e730274c61e58018c52a
5
5
  SHA512:
6
- metadata.gz: c12c578c836c7553655925489fce0bdc1509b930f6f9170976e9b1d8eb4ba75fd60644e1f84eba92162cb9b365030d9a978b559635ce9b665c162fedf696b074
7
- data.tar.gz: f81248b95f3320095878ad45288766d5646dd6f3dab53ecae844cc3fe12fa6e7f31ce3ec1502105aa00e02fee04fa64fc458e14e43e78dca0eff7bac5f7ff79a
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, reload: false, refind: false, &block)
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
- this.instance_variable_set(:"#{PREFIX}#{identifier}", this.instance_exec(&block))
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
- let_accessor = accessor = -> { this.instance_variable_get(:"#{PREFIX}#{identifier}") }
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 = -> { this.instance_variable_get(:"#{PREFIX}#{identifier}")&.reload } if reload
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 = this.instance_variable_get(:"#{PREFIX}#{identifier}")
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
- define_singleton_method(identifier, &accessor)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TestProf
4
- VERSION = "0.3.0.pre"
4
+ VERSION = "0.3.0.pre2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: test-prof
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.pre
4
+ version: 0.3.0.pre2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev