once-ler 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -101,7 +101,8 @@ module Onceler
101
101
  end
102
102
 
103
103
  def add_onceler_hooks!
104
- prepend_before(:all) do |group|
104
+ before(:all) do |group|
105
+ Onceler.configuration.run_callbacks(:record, :before)
105
106
  group.onceler.record!
106
107
  end
107
108
 
@@ -32,7 +32,7 @@ module Onceler
32
32
  end
33
33
 
34
34
  def __data
35
- [__ivars, @__retvals]
35
+ @__data ||= Marshal.dump([__ivars, @__retvals])
36
36
  end
37
37
 
38
38
  def copy(mixins)
@@ -42,7 +42,7 @@ module Onceler
42
42
  end
43
43
 
44
44
  def copy_from(other)
45
- ivars, @__retvals = Marshal.load(Marshal.dump(other.__data))
45
+ ivars, @__retvals = Marshal.load(other.__data)
46
46
  ivars.each do |key, value|
47
47
  instance_variable_set(key, value)
48
48
  end
@@ -15,5 +15,21 @@ module Onceler
15
15
  def include(mod)
16
16
  modules << mod
17
17
  end
18
+
19
+ def before(scope, &block)
20
+ callbacks[scope][:before] << block
21
+ end
22
+
23
+ def callbacks
24
+ @callbacks ||= Hash.new do |scopes, scope|
25
+ scopes[scope] = Hash.new do |timings, timing|
26
+ timings[timing] = []
27
+ end
28
+ end
29
+ end
30
+
31
+ def run_callbacks(scope, timing)
32
+ callbacks[scope][timing].each(&:call)
33
+ end
18
34
  end
19
35
  end
@@ -1,3 +1,11 @@
1
- module ActiveRecord::TestFixtures
2
- def teardown_fixtures; end # we manage it ourselves
1
+ require "active_record"
2
+
3
+ if ActiveRecord::VERSION::STRING >= "4.1."
4
+ require "onceler/extensions/active_record_4_1"
5
+ elsif ActiveRecord::VERSION::STRING >= "4.0."
6
+ require "onceler/extensions/active_record_4_0"
7
+ elsif ActiveRecord::VERSION::STRING >= "3.2."
8
+ require "onceler/extensions/active_record_3_2"
9
+ else
10
+ require "onceler/extensions/active_record_3_0"
3
11
  end
@@ -0,0 +1,55 @@
1
+ require "onceler/transactions"
2
+
3
+ # monkey-patch these to:
4
+ # 1. not clear connections so that we don't lose our transactions
5
+ # 2. use savepoints (if necessary)
6
+
7
+ module ActiveRecord::TestFixtures
8
+ include Onceler::Transactions
9
+
10
+ def setup_fixtures
11
+ return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
12
+
13
+ if pre_loaded_fixtures && !use_transactional_fixtures
14
+ raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures'
15
+ end
16
+
17
+ @fixture_cache = {}
18
+ @@already_loaded_fixtures ||= {}
19
+
20
+ # Load fixtures once and begin transaction.
21
+ if run_in_transaction?
22
+ if @@already_loaded_fixtures[self.class]
23
+ @loaded_fixtures = @@already_loaded_fixtures[self.class]
24
+ else
25
+ load_fixtures
26
+ @@already_loaded_fixtures[self.class] = @loaded_fixtures
27
+ end
28
+ ### ONCELER'd
29
+ begin_transaction(ActiveRecord::Base.connection)
30
+ # Load fixtures for every test.
31
+ else
32
+ Fixtures.reset_cache
33
+ @@already_loaded_fixtures[self.class] = nil
34
+ load_fixtures
35
+ end
36
+
37
+ # Instantiate fixtures for every test if requested.
38
+ instantiate_fixtures if use_instantiated_fixtures
39
+ end
40
+
41
+ def teardown_fixtures
42
+ return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
43
+
44
+ unless run_in_transaction?
45
+ ActiveRecord::Fixtures.reset_cache
46
+ end
47
+
48
+ # Rollback changes if a transaction is active.
49
+ if run_in_transaction? && ActiveRecord::Base.connection.open_transactions != 0
50
+ ### ONCELER'd
51
+ rollback_transaction(ActiveRecord::Base.connection)
52
+ end
53
+ ### ONCELER'd
54
+ end
55
+ end
@@ -0,0 +1,65 @@
1
+ require "onceler/transactions"
2
+
3
+ # monkey-patch these to:
4
+ # 1. not clear connections so that we don't lose our transactions
5
+ # 2. use savepoints (if necessary)
6
+
7
+ module ActiveRecord::TestFixtures
8
+ include ::Onceler::Transactions
9
+
10
+ def setup_fixtures
11
+ return unless !ActiveRecord::Base.configurations.blank?
12
+
13
+ if pre_loaded_fixtures && !use_transactional_fixtures
14
+ raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures'
15
+ end
16
+
17
+ @fixture_cache = {}
18
+ @fixture_connections = []
19
+ @@already_loaded_fixtures ||= {}
20
+
21
+ # Load fixtures once and begin transaction.
22
+ if run_in_transaction?
23
+ if @@already_loaded_fixtures[self.class]
24
+ @loaded_fixtures = @@already_loaded_fixtures[self.class]
25
+ else
26
+ @loaded_fixtures = load_fixtures
27
+ @@already_loaded_fixtures[self.class] = @loaded_fixtures
28
+ end
29
+ @fixture_connections = enlist_fixture_connections
30
+ @fixture_connections.each do |connection|
31
+ ### ONCELER'd
32
+ begin_transaction(connection)
33
+ end
34
+ # Load fixtures for every test.
35
+ else
36
+ ActiveRecord::Fixtures.reset_cache
37
+ @@already_loaded_fixtures[self.class] = nil
38
+ @loaded_fixtures = load_fixtures
39
+ end
40
+
41
+ # Instantiate fixtures for every test if requested.
42
+ instantiate_fixtures if use_instantiated_fixtures
43
+ end
44
+
45
+ def teardown_fixtures
46
+ return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
47
+
48
+ unless run_in_transaction?
49
+ ActiveRecord::Fixtures.reset_cache
50
+ end
51
+
52
+ # Rollback changes if a transaction is active.
53
+ if run_in_transaction?
54
+ @fixture_connections.each do |connection|
55
+ if connection.open_transactions != 0
56
+ ### ONCELER'd
57
+ rollback_transaction(connection)
58
+ end
59
+ end
60
+ @fixture_connections.clear
61
+ end
62
+ ### ONCELER'd
63
+ end
64
+ end
65
+
@@ -0,0 +1,19 @@
1
+ # monkey-patch this to not clear connections so that we don't lose our
2
+ # transactions
3
+
4
+ module ActiveRecord::TestFixtures
5
+ def teardown_fixtures
6
+ return if ActiveRecord::Base.configurations.blank?
7
+
8
+ # Rollback changes if a transaction is active.
9
+ if run_in_transaction?
10
+ @fixture_connections.each do |connection|
11
+ connection.rollback_transaction if connection.transaction_open?
12
+ end
13
+ @fixture_connections.clear
14
+ else
15
+ ActiveRecord::FixtureSet.reset_cache
16
+ end
17
+ ### ONCELER'd
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ # monkey-patch this to not clear connections so that we don't lose our
2
+ # transactions
3
+
4
+ module ActiveRecord::TestFixtures
5
+ def teardown_fixtures
6
+ # Rollback changes if a transaction is active.
7
+ if run_in_transaction?
8
+ @fixture_connections.each do |connection|
9
+ connection.rollback_transaction if connection.transaction_open?
10
+ end
11
+ @fixture_connections.clear
12
+ else
13
+ ActiveRecord::FixtureSet.reset_cache
14
+ end
15
+ ### ONCELER'd
16
+ end
17
+ end
@@ -1,8 +1,10 @@
1
1
  require "onceler/blank_tape"
2
- require "active_record"
2
+ require "onceler/transactions"
3
3
 
4
4
  module Onceler
5
5
  class Recorder
6
+ include Transactions
7
+
6
8
  attr_accessor :tape, :helper_proxy
7
9
 
8
10
  def initialize(parent)
@@ -37,7 +39,7 @@ module Onceler
37
39
  @recordings.each do |recording|
38
40
  recording.record_onto!(@tape)
39
41
  end
40
- @data = Marshal.dump(@tape.__data)
42
+ @data = @tape.__data
41
43
  end
42
44
 
43
45
  def reset!
@@ -116,39 +118,6 @@ module Onceler
116
118
  rollback_transaction(klass.connection)
117
119
  end
118
120
  end
119
-
120
- if ActiveRecord::VERSION::MAJOR >= 4
121
- def begin_transaction(conn)
122
- conn.begin_transaction requires_new: true
123
- end
124
-
125
- def rollback_transaction(conn)
126
- conn.rollback_transaction
127
- end
128
- else
129
- def begin_transaction(klass)
130
- unless conn.instance_variable_get(:@_current_transaction_records)
131
- conn.instance_variable_set(:@_current_transaction_records, [])
132
- end
133
- if conn.open_transactions == 0
134
- conn.begin_db_transaction
135
- else
136
- conn.create_savepoint
137
- end
138
- conn.increment_open_transactions
139
- end
140
-
141
- def begin_transaction(klass)
142
- conn.decrement_open_transactions
143
- if conn.open_transactions == 0
144
- conn.rollback_db_transaction
145
- conn.send :rollback_transaction_records, true
146
- else
147
- conn.rollback_to_savepoint
148
- conn.send :rollback_transaction_records, false
149
- end
150
- end
151
- end
152
121
  end
153
122
 
154
123
  class Recording
@@ -0,0 +1,7 @@
1
+ require "active_record"
2
+ if ActiveRecord::VERSION::MAJOR >= 4
3
+ require "onceler/transactions/active_record_4"
4
+ else
5
+ require "onceler/transactions/active_record_3"
6
+ end
7
+
@@ -0,0 +1,28 @@
1
+ module Onceler
2
+ module Transactions
3
+ def begin_transaction(conn)
4
+ unless conn.instance_variable_get(:@_current_transaction_records)
5
+ conn.instance_variable_set(:@_current_transaction_records, [])
6
+ end
7
+ if conn.open_transactions == 0
8
+ conn.transaction_joinable = false
9
+ conn.begin_db_transaction
10
+ else
11
+ conn.create_savepoint
12
+ end
13
+ conn.increment_open_transactions
14
+ end
15
+
16
+ def rollback_transaction(conn)
17
+ conn.decrement_open_transactions
18
+ if conn.open_transactions == 0
19
+ conn.rollback_db_transaction
20
+ conn.send :rollback_transaction_records, true
21
+ else
22
+ conn.rollback_to_savepoint
23
+ conn.send :rollback_transaction_records, false
24
+ end
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,12 @@
1
+ module Onceler
2
+ module Transactions
3
+ def begin_transaction(conn)
4
+ conn.begin_transaction requires_new: true
5
+ end
6
+
7
+ def rollback_transaction(conn)
8
+ conn.rollback_transaction
9
+ end
10
+ end
11
+ end
12
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: once-ler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-27 00:00:00.000000000 Z
12
+ date: 2014-06-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -57,7 +57,14 @@ files:
57
57
  - lib/onceler/blank_tape.rb
58
58
  - lib/onceler/configuration.rb
59
59
  - lib/onceler/extensions/active_record.rb
60
+ - lib/onceler/extensions/active_record_3_0.rb
61
+ - lib/onceler/extensions/active_record_3_2.rb
62
+ - lib/onceler/extensions/active_record_4_0.rb
63
+ - lib/onceler/extensions/active_record_4_1.rb
60
64
  - lib/onceler/recorder.rb
65
+ - lib/onceler/transactions/active_record_3.rb
66
+ - lib/onceler/transactions/active_record_4.rb
67
+ - lib/onceler/transactions.rb
61
68
  - lib/onceler.rb
62
69
  homepage: http://github.com/instructure/onceler
63
70
  licenses: []