prefactory 0.7.0 → 0.8.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
  SHA1:
3
- metadata.gz: 013e5ed5e0804fcc19fa994c3d64c0a1e4cede1d
4
- data.tar.gz: 553cc7b243284a49b97b247ee4b7d00a79904af8
3
+ metadata.gz: 0f879df086b73a9de646cc17f9a1f994d888957b
4
+ data.tar.gz: a8232c6538a32d3de8e65acb24ba1ff5cb7de924
5
5
  SHA512:
6
- metadata.gz: 79fdb0a121681e05a0d0617b5bb5b840da87c3442a6a64c363094c765d453b860aba29d10ccc3f142824e0710c61925c873fe687df5142b1e10ef19a49811e69
7
- data.tar.gz: d5f7469e71a6ffd0a60006905ee245c7aca644bd2753c1cd7fb6dee1eae8b6d2e0a091d67d6aa6f9d9568a95dc5d8c527e97534f872b2abdd635505900ec2f83
6
+ metadata.gz: 03e35db9745634b13cc2d87a5f7beb8bde287b8962da5256a64f10b5f5d09897034c8cf355f10e8282873c73fb2b6e570f4f9e84918ad1f7c7c46355484db7bc
7
+ data.tar.gz: d21a6f09b96f915bd3382c84b13521b1203a11d9986dea71002b19370e5e00d7f7d072af9544a8fab88e7e8dbc90a117d6b72a803f4694ade6d5823a090ea8cc
data/README.md CHANGED
@@ -103,6 +103,16 @@ end
103
103
 
104
104
  See also: [An example rails application with Prefactory configured](https://github.com/seanwalbran/prefactory-example)
105
105
 
106
+ If desired, it is also possible to only include the `set!` method and related
107
+ lookup functionality for e.g. specs that cannot use transactional isolation
108
+ (like some threaded Capybara feature specs, or similar):
109
+
110
+ ```ruby
111
+ RSpec.configure do |config|
112
+ config.include Prefactory::Lookups
113
+ end
114
+ ```
115
+
106
116
  ## Contributing
107
117
 
108
118
  1. Fork it ( http://github.com/socialcast/prefactory/fork )
@@ -21,5 +21,5 @@
21
21
  # SOFTWARE.
22
22
 
23
23
  module Prefactory
24
- VERSION = '0.7.0'
24
+ VERSION = '0.8.0'
25
25
  end
@@ -29,156 +29,169 @@ module Prefactory
29
29
  class NotDefined
30
30
  end
31
31
 
32
- def prefactory_lookup(key)
33
- PrefactoryLookup.where(:key => key).first
34
- end
35
-
36
- # instantiate, or access an already-instantiated-and-memoized, prefabricated object.
37
- def prefactory(key)
38
- @prefactory_memo ||= {}
39
- @prefactory_memo[key] ||= begin
40
- lookup = prefactory_lookup(key)
41
- if lookup.present?
42
- if lookup[:result_class]
43
- lookup[:result_class].constantize.find(lookup[:result_id])
44
- else
45
- YAML.load(lookup[:result_value])
46
- end
47
- else
48
- Prefactory::NotDefined
49
- end
50
- rescue
51
- end
32
+ def self.included(base)
33
+ base.include Transactionality
34
+ base.include Lookups
52
35
  end
53
36
 
54
- def in_before_all?
55
- @before_all_context
56
- end
37
+ module Lookups
38
+ def self.included(base)
39
+ PrefactoryLookup.create_table
40
+
41
+ base.instance_eval do
42
+ def before_with_detect_before_all(*args, &block)
43
+ before_all_context = (args.first == :all)
44
+ modified_block = proc do
45
+ @before_all_context = before_all_context
46
+ instance_eval(&block)
47
+ @before_all_context = nil
48
+ end
49
+ before_without_detect_before_all(*args, &modified_block)
50
+ end
57
51
 
58
- # prefabricate an object. Can be passed any block that returns a class accessible by Klass.find(id),
59
- # or, if no block is passed, invokes create(key, options) to use e.g. a FactoryGirl factory of that key name.
60
- def prefactory_add(key, *args, &block)
61
- raise "prefactory_add can only be used in a before(:all) context. Change to a before(:all) or set!, or use let/let! instead." unless in_before_all?
62
- result = nil
63
- clear_prefactory_memoizations
64
- if block
65
- result = yield
66
- else
67
- result = create(key, *args) if respond_to?(:create)
68
- end
69
- if result.present?
70
- if result.respond_to?(:id)
71
- PrefactoryLookup.where(:key => key).first_or_initialize.tap do |lookup|
72
- lookup.result_class = result.class.name
73
- lookup.result_id = result.id
74
- lookup.result_value = nil
75
- lookup.save!
52
+ class << self
53
+ alias_method :before_without_detect_before_all, :before
54
+ alias_method :before, :before_with_detect_before_all
76
55
  end
77
- else
78
- PrefactoryLookup.where(:key => key).first_or_initialize.tap do |lookup|
79
- lookup.result_class = nil
80
- lookup.result_id = nil
81
- lookup.result_value = YAML.dump(result)
82
- lookup.save!
56
+
57
+ def set!(key, *args, &set_block)
58
+ before(:all) do
59
+ modified_block = proc { instance_eval(&set_block) } if set_block
60
+ prefactory_add(key, *args, &modified_block)
61
+ end
83
62
  end
84
63
  end
85
- else
86
- warn "WARNING: Failed to add #{key} to prefactory: block result not present"
87
- end
88
- clear_prefactory_memoizations
89
- result
90
- end
91
-
92
- def self.included(base)
93
- base.extend RSpecAroundAll
94
- PrefactoryLookup.create_table
95
64
 
96
- # Wrap outermost describe block in a transaction, so before(:all) data is rolled back at the end of this suite.
97
- base.before(:all) do
98
- clear_prefactory_memoizations
99
- end
100
- base.around(:all) do |group|
101
- ActiveRecord::Base.with_disposable_transaction { group.run_examples }
102
- end
103
- base.after(:all) do
104
- clear_prefactory_memoizations
65
+ # allow shorthand access to a prefabricated object
66
+ # e.g. with prefactory_add(:active_user), calling the method active_user will return the (memoized) object,
67
+ # by invoking the 'prefactory' method.
68
+ base.class_eval do
69
+ def method_missing(key, *args, &block)
70
+ result = prefactory(key)
71
+ result == Prefactory::NotDefined ? super : result
72
+ end
73
+ end
105
74
  end
106
75
 
107
- # Wrap each example in a transaction, instead of using Rails' transactional
108
- # fixtures, which does not support itself being wrapped in an outermost transaction.
109
- base.around(:each) do |example|
110
- clear_prefactory_memoizations
111
- ActiveRecord::Base.with_disposable_transaction { example.run }
112
- clear_prefactory_memoizations
76
+ def prefactory_lookup(key)
77
+ PrefactoryLookup.where(:key => key).first
113
78
  end
114
79
 
115
- # Wrap each ExampleGroup in a transaction, so group-level before(:all) settings
116
- # are scoped only to the group.
117
- base.instance_eval do
118
- def describe_with_transaction(*args, &block)
119
- original_caller = caller
120
- modified_block = proc do
121
- instance_eval do
122
- before(:all) { clear_prefactory_memoizations }
123
- around(:all) do |group|
124
- ActiveRecord::Base.with_disposable_transaction { group.run_examples }
125
- end
126
- after(:all) { clear_prefactory_memoizations }
80
+ # instantiate, or access an already-instantiated-and-memoized, prefabricated object.
81
+ def prefactory(key)
82
+ @prefactory_memo ||= {}
83
+ @prefactory_memo[key] ||= begin
84
+ lookup = prefactory_lookup(key)
85
+ if lookup.present?
86
+ if lookup[:result_class]
87
+ lookup[:result_class].constantize.find(lookup[:result_id])
88
+ else
89
+ YAML.load(lookup[:result_value])
127
90
  end
128
- instance_eval(&block)
129
- end
130
-
131
- caller_metadata = { :caller => original_caller }
132
- if args.last.is_a?(Hash)
133
- args.last.merge!(caller_metadata)
134
91
  else
135
- args << caller_metadata
92
+ Prefactory::NotDefined
136
93
  end
137
-
138
- describe_without_transaction(*args, &modified_block)
94
+ rescue
139
95
  end
96
+ end
140
97
 
141
- def before_with_detect_before_all(*args, &block)
142
- before_all_context = (args.first == :all)
143
- modified_block = proc do
144
- @before_all_context = before_all_context
145
- instance_eval(&block)
146
- @before_all_context = nil
147
- end
148
- before_without_detect_before_all(*args, &modified_block)
149
- end
98
+ def in_before_all?
99
+ @before_all_context
100
+ end
150
101
 
151
- class << self
152
- alias_method_chain :describe, :transaction
153
- alias_method :context, :describe
154
- alias_method_chain :before, :detect_before_all
102
+ # prefabricate an object. Can be passed any block that returns a class accessible by Klass.find(id),
103
+ # or, if no block is passed, invokes create(key, options) to use e.g. a FactoryGirl factory of that key name.
104
+ def prefactory_add(key, *args, &block)
105
+ raise "prefactory_add can only be used in a before(:all) context. Change to a before(:all) or set!, or use let/let! instead." unless in_before_all?
106
+ result = nil
107
+ clear_prefactory_memoizations
108
+ if block
109
+ result = yield
110
+ else
111
+ result = create(key, *args) if respond_to?(:create)
155
112
  end
156
-
157
- def set!(key, *args, &set_block)
158
- before(:all) do
159
- modified_block = proc { instance_eval(&set_block) } if set_block
160
- prefactory_add(key, *args, &modified_block)
113
+ if result.present?
114
+ if result.respond_to?(:id)
115
+ PrefactoryLookup.where(:key => key).first_or_initialize.tap do |lookup|
116
+ lookup.result_class = result.class.name
117
+ lookup.result_id = result.id
118
+ lookup.result_value = nil
119
+ lookup.save!
120
+ end
121
+ else
122
+ PrefactoryLookup.where(:key => key).first_or_initialize.tap do |lookup|
123
+ lookup.result_class = nil
124
+ lookup.result_id = nil
125
+ lookup.result_value = YAML.dump(result)
126
+ lookup.save!
127
+ end
161
128
  end
129
+ else
130
+ warn "WARNING: Failed to add #{key} to prefactory: block result not present"
162
131
  end
132
+ clear_prefactory_memoizations
133
+ result
134
+ end
163
135
 
136
+ def clear_prefactory_memoizations
137
+ @prefactory_memo = {}
164
138
  end
139
+ end
165
140
 
166
- # allow shorthand access to a prefabricated object
167
- # e.g. with prefactory_add(:active_user), calling the method active_user will return the (memoized) object,
168
- # by invoking the 'prefactory' method.
169
- base.class_eval do
170
- def method_missing(key, *args, &block)
171
- result = prefactory(key)
172
- result == Prefactory::NotDefined ? super : result
141
+ module Transactionality
142
+ def self.included(base)
143
+ base.extend RSpecAroundAll
144
+ # Wrap outermost describe block in a transaction, so before(:all) data is rolled back at the end of this suite.
145
+ base.before(:all) do
146
+ clear_prefactory_memoizations
147
+ end
148
+ base.around(:all) do |group|
149
+ ActiveRecord::Base.with_disposable_transaction { group.run_examples }
150
+ end
151
+ base.after(:all) do
152
+ clear_prefactory_memoizations
173
153
  end
174
- end
175
154
 
176
- end
155
+ # Wrap each example in a transaction, instead of using Rails' transactional
156
+ # fixtures, which does not support itself being wrapped in an outermost transaction.
157
+ base.around(:each) do |example|
158
+ clear_prefactory_memoizations
159
+ ActiveRecord::Base.with_disposable_transaction { example.run }
160
+ clear_prefactory_memoizations
161
+ end
177
162
 
178
- private
163
+ # Wrap each ExampleGroup in a transaction, so group-level before(:all) settings
164
+ # are scoped only to the group.
165
+ base.instance_eval do
166
+ def describe_with_transaction(*args, &block)
167
+ original_caller = caller
168
+ modified_block = proc do
169
+ instance_eval do
170
+ before(:all) { clear_prefactory_memoizations }
171
+ around(:all) do |group|
172
+ ActiveRecord::Base.with_disposable_transaction { group.run_examples }
173
+ end
174
+ after(:all) { clear_prefactory_memoizations }
175
+ end
176
+ instance_eval(&block)
177
+ end
179
178
 
180
- def clear_prefactory_memoizations
181
- @prefactory_memo = {}
182
- end
179
+ caller_metadata = { :caller => original_caller }
180
+ if args.last.is_a?(Hash)
181
+ args.last.merge!(caller_metadata)
182
+ else
183
+ args << caller_metadata
184
+ end
183
185
 
186
+ describe_without_transaction(*args, &modified_block)
187
+ end
188
+
189
+ class << self
190
+ alias_method :describe_without_transaction, :describe
191
+ alias_method :describe, :describe_with_transaction
192
+ alias_method :context, :describe
193
+ end
194
+ end
195
+ end
196
+ end
184
197
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prefactory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - developers@socialcast.com
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-13 00:00:00.000000000 Z
11
+ date: 2016-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec_around_all