prefactory 0.3.1 → 0.4.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 +4 -4
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/README.md +8 -8
- data/lib/prefactory/prefactory_lookup.rb +11 -0
- data/lib/prefactory/version.rb +1 -1
- data/lib/rspec/core/prefactory.rb +21 -20
- data/spec/prefactory_spec.rb +13 -1
- metadata +5 -3
- data/.rvmrc +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b705b09aea1ed091cbd6a46aabd4df33438cb214
|
4
|
+
data.tar.gz: 7f82cb399ee76a7d2aea8ceed15dc6d57b849b7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3e66b35c17a5cccda2760a423962ba89f04d9882aa6d7dbdebd006915ae1a706534bd5162ebaef35e04af29203024e86f11e726fc61dbf7b0c294e907049924
|
7
|
+
data.tar.gz: 6324783baa9b74fe4870ff811f21866b091aa4f89ca4ddc6eefde4359a6ac0a17e023335a63ab01d7903c11cb814ccdea80838c25daa6386fdb7a3bae08be434
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
prefactory
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
default
|
data/README.md
CHANGED
@@ -68,34 +68,34 @@ describe User do
|
|
68
68
|
# end
|
69
69
|
set!(:other_friend) { create :user }
|
70
70
|
|
71
|
-
context 'a new user
|
71
|
+
context 'a new user' do
|
72
72
|
|
73
|
-
it { user.friends.count.
|
73
|
+
it { expect(user.friends.count).to eq 0 }
|
74
74
|
|
75
75
|
context 'with a friend' do
|
76
76
|
before(:all) { user.add_friend(friend) } # executes once
|
77
77
|
|
78
|
-
it { user.friends.count.
|
78
|
+
it { expect(user.friends.count).to eq 1 }
|
79
79
|
|
80
80
|
# these changes will be transparently rolled back
|
81
81
|
it "allows removing the friend" do
|
82
82
|
expect { user.remove_friend(friend) }.to_not raise_error
|
83
|
-
user.friends.count.
|
83
|
+
expect(user.friends.count).to eq 0
|
84
84
|
end
|
85
85
|
|
86
86
|
it "disallows adding the same friend again" do
|
87
87
|
expect { user.add_friend(friend) }.to raise_error
|
88
|
-
user.friends.count.
|
88
|
+
expect(user.friends.count).to eq 1
|
89
89
|
end
|
90
90
|
|
91
91
|
# these changes will be transparently rolled back
|
92
92
|
it "allows adding a different friend" do
|
93
93
|
expect { user.add_friend(other_friend) }.to_not raise_error
|
94
|
-
user.friends.count.
|
94
|
+
expect(user.friends.count).to eq 2
|
95
95
|
end
|
96
96
|
|
97
|
-
it { user.friends.
|
98
|
-
it { user.friends.
|
97
|
+
it { expect(user.friends).to include friend }
|
98
|
+
it { expect(user.friends).not_to include other_friend }
|
99
99
|
end
|
100
100
|
end
|
101
101
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class PrefactoryLookup < ActiveRecord::Base
|
2
|
+
def self.create_table
|
3
|
+
conn = ActiveRecord::Base.connection
|
4
|
+
conn.create_table :prefactory_lookups, :force => true do |t|
|
5
|
+
t.column :key, :string
|
6
|
+
t.column :result_class, :string
|
7
|
+
t.column :result_id, :integer
|
8
|
+
t.index [:key], :unique => true
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/lib/prefactory/version.rb
CHANGED
@@ -22,23 +22,28 @@
|
|
22
22
|
|
23
23
|
require 'prefactory/active_record_integration'
|
24
24
|
require 'rspec_around_all'
|
25
|
+
require 'prefactory/prefactory_lookup'
|
25
26
|
|
26
27
|
module Prefactory
|
28
|
+
class NotDefined
|
29
|
+
end
|
27
30
|
|
28
31
|
def prefactory_lookup(key)
|
29
|
-
|
32
|
+
PrefactoryLookup.where(:key => key).first
|
30
33
|
end
|
31
34
|
|
32
35
|
# instantiate, or access an already-instantiated-and-memoized, prefabricated object.
|
33
36
|
def prefactory(key)
|
34
|
-
lookup = prefactory_lookup(key)
|
35
|
-
return nil unless lookup.present?
|
36
37
|
@prefactory_memo ||= {}
|
37
|
-
begin
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
38
|
+
@prefactory_memo[key] ||= begin
|
39
|
+
lookup = prefactory_lookup(key)
|
40
|
+
if lookup.present?
|
41
|
+
lookup[:result_class].constantize.find(lookup[:result_id])
|
42
|
+
else
|
43
|
+
Prefactory::NotDefined
|
44
|
+
end
|
45
|
+
rescue
|
46
|
+
end
|
42
47
|
end
|
43
48
|
|
44
49
|
def in_before_all?
|
@@ -57,7 +62,11 @@ module Prefactory
|
|
57
62
|
result = create(key, *args) if respond_to?(:create)
|
58
63
|
end
|
59
64
|
if result.present?
|
60
|
-
|
65
|
+
PrefactoryLookup.where(:key => key).first_or_initialize.tap do |lookup|
|
66
|
+
lookup.result_class = result.class.name
|
67
|
+
lookup.result_id = result.id
|
68
|
+
lookup.save!
|
69
|
+
end
|
61
70
|
else
|
62
71
|
warn "WARNING: Failed to add #{key} to prefactory: block result not present"
|
63
72
|
end
|
@@ -67,10 +76,10 @@ module Prefactory
|
|
67
76
|
|
68
77
|
def self.included(base)
|
69
78
|
base.extend RSpecAroundAll
|
79
|
+
PrefactoryLookup.create_table
|
70
80
|
|
71
81
|
# Wrap outermost describe block in a transaction, so before(:all) data is rolled back at the end of this suite.
|
72
82
|
base.before(:all) do
|
73
|
-
clear_prefactory_map
|
74
83
|
clear_prefactory_memoizations
|
75
84
|
end
|
76
85
|
base.around(:all) do |group|
|
@@ -78,7 +87,6 @@ module Prefactory
|
|
78
87
|
end
|
79
88
|
base.after(:all) do
|
80
89
|
clear_prefactory_memoizations
|
81
|
-
clear_prefactory_map
|
82
90
|
end
|
83
91
|
|
84
92
|
# Wrap each example in a transaction, instead of using Rails' transactional
|
@@ -145,11 +153,8 @@ module Prefactory
|
|
145
153
|
# by invoking the 'prefactory' method.
|
146
154
|
base.class_eval do
|
147
155
|
def method_missing(key, *args, &block)
|
148
|
-
|
149
|
-
|
150
|
-
else
|
151
|
-
super
|
152
|
-
end
|
156
|
+
result = prefactory(key)
|
157
|
+
result == Prefactory::NotDefined ? super : result
|
153
158
|
end
|
154
159
|
end
|
155
160
|
|
@@ -161,8 +166,4 @@ module Prefactory
|
|
161
166
|
@prefactory_memo = {}
|
162
167
|
end
|
163
168
|
|
164
|
-
def clear_prefactory_map
|
165
|
-
@prefactory = {}
|
166
|
-
end
|
167
|
-
|
168
169
|
end
|
data/spec/prefactory_spec.rb
CHANGED
@@ -238,7 +238,7 @@ describe Prefactory do
|
|
238
238
|
context "when passed a nonexistent key" do
|
239
239
|
let(:key) { :frog }
|
240
240
|
it do
|
241
|
-
is_expected.to
|
241
|
+
is_expected.to eq Prefactory::NotDefined
|
242
242
|
expect { frog }.to raise_error(NameError)
|
243
243
|
end
|
244
244
|
end
|
@@ -281,4 +281,16 @@ describe Prefactory do
|
|
281
281
|
expect(some_other_comment.text).to eq(blog.title)
|
282
282
|
end
|
283
283
|
end
|
284
|
+
|
285
|
+
describe "when the same key is overridden in different contexts at the same depth" do
|
286
|
+
set!(:some_blog) { create :blog, :title => 'original' }
|
287
|
+
it { expect(some_blog.title).to eq 'original' }
|
288
|
+
context "nested override" do
|
289
|
+
set!(:some_blog) { create :blog, :title => 'nested' }
|
290
|
+
it { expect(some_blog.title).to eq 'nested' }
|
291
|
+
end
|
292
|
+
context "nested unset" do
|
293
|
+
it { expect(some_blog.title).to eq 'original' }
|
294
|
+
end
|
295
|
+
end
|
284
296
|
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.
|
4
|
+
version: 0.4.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: 2014-
|
11
|
+
date: 2014-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec_around_all
|
@@ -130,7 +130,8 @@ extensions: []
|
|
130
130
|
extra_rdoc_files: []
|
131
131
|
files:
|
132
132
|
- ".gitignore"
|
133
|
-
- ".
|
133
|
+
- ".ruby-gemset"
|
134
|
+
- ".ruby-version"
|
134
135
|
- ".travis.yml"
|
135
136
|
- Gemfile
|
136
137
|
- LICENSE.txt
|
@@ -138,6 +139,7 @@ files:
|
|
138
139
|
- Rakefile
|
139
140
|
- lib/prefactory.rb
|
140
141
|
- lib/prefactory/active_record_integration.rb
|
142
|
+
- lib/prefactory/prefactory_lookup.rb
|
141
143
|
- lib/prefactory/version.rb
|
142
144
|
- lib/rspec/core/prefactory.rb
|
143
145
|
- prefactory.gemspec
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm use default@prefactory --create
|