transactor 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d8d6c6a970b35c85e5a2a609ac666587c91179c
4
- data.tar.gz: 3065ef3b42767dff92f403034c3fa1b568e8cb86
3
+ metadata.gz: 0392c2682f4ed22ea823090f2d75a4b6b4125361
4
+ data.tar.gz: e1081c5a720c23bc9f11992b14d7fa23b3994d00
5
5
  SHA512:
6
- metadata.gz: 4dc2cde00f77e7742000a41d5f8b3487cde6fbfc039f554fc07abdb844a35a61d233d26b18d86c96cf5fd47703160ee10833599a5e6cf0332afb8b7d8f2c4c89
7
- data.tar.gz: a739ebfc8a4751838aa3e31a04e5f28a66d4d92c614fe333dee4dabec830574e57283f8e992e8c618a0c0c9daca3e5de6c0e97f05f03e98a7fd954df9c183a74
6
+ metadata.gz: 2f97d27444ceb76a2f3a6ff52728702086be33847064abfbe64231214c1ec708efb579f147e83eec4b738778f5990d50f10151237979e91dfcb7b5cbd364b6b2
7
+ data.tar.gz: 67643d51a70a006ca5fdddb8df47d492a008a8bf80195c8a48d70a3a9f4c08a42390e3ad82cc3dc1cf661f3b39d93b67cb35f09ee629ff15cd20c94354bd8093
data/lib/transactor.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'active_support/core_ext/module/attribute_accessors'
2
+ require 'active_support/core_ext/array/extract_options'
3
+ require 'active_support/core_ext/hash/keys'
2
4
  require 'active_support/core_ext/string/inflections'
3
5
  require 'active_support/core_ext/string/output_safety'
4
6
  require 'canfig'
@@ -17,7 +19,11 @@ module Transactor
17
19
  config.rollback_failed_actors = false
18
20
  end
19
21
 
20
- def self.transaction(&block)
21
- Transactor::Transaction.new &block
22
+ def self.transaction(*args, &block)
23
+ Transactor::Transaction.new *args, &block
24
+ end
25
+
26
+ def self.improvise(*args, &block)
27
+ Transactor::Improv::Transaction.new *args, &block
22
28
  end
23
29
  end
@@ -17,24 +17,21 @@ module Transactor
17
17
  end
18
18
  end
19
19
 
20
+ def rollback_on_failure!
21
+ @rollback_on_failure = true
22
+ end
23
+
20
24
  def rollback_on_failure?
21
- self.class.rollback_on_failure?
25
+ return self.class.rollback_on_failure? if @rollback_on_failure.nil?
26
+ @rollback_on_failure
22
27
  end
23
28
 
24
29
  def perform(&block)
25
- if block_given?
26
- instance_eval &block
27
- else
28
- raise PerformNotImplemented, "#{self.class.name}##{__method__} has not been implemented"
29
- end
30
+ raise PerformNotImplemented, "#{self.class.name}##{__method__} has not been implemented"
30
31
  end
31
32
 
32
33
  def rollback(&block)
33
- if block_given?
34
- instance_eval &block
35
- else
36
- raise RollbackNotImplemented, "#{self.class.name}##{__method__} has not been implemented"
37
- end
34
+ raise RollbackNotImplemented, "#{self.class.name}##{__method__} has not been implemented"
38
35
  end
39
36
 
40
37
  def state
@@ -1,25 +1,8 @@
1
- module Transactor
2
- class Improv < Performance
3
- def perform(&block)
4
- if block_given?
5
- super(&block)
6
- else
7
- super { nil }
8
- end
9
- end
10
-
11
- def rollback(&block)
12
- if block_given?
13
- super(&block)
14
- else
15
- super { nil }
16
- end
17
- end
1
+ require 'transactor/improv/performance'
2
+ require 'transactor/improv/actor'
3
+ require 'transactor/improv/transaction'
18
4
 
19
- protected
20
-
21
- def initialize(*args)
22
- super(Actor, *args)
23
- end
5
+ module Transactor
6
+ module Improv
24
7
  end
25
8
  end
@@ -0,0 +1,13 @@
1
+ module Transactor
2
+ module Improv
3
+ class Actor < Transactor::Actor
4
+ def perform(&block)
5
+ block_given? ? instance_eval(&block) : super
6
+ end
7
+
8
+ def rollback(&block)
9
+ block_given? ? instance_eval(&block) : super
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+ module Transactor
2
+ module Improv
3
+ class Performance < Transactor::Performance
4
+
5
+ def perform(&block)
6
+ if block_given?
7
+ @perform_block = block
8
+ else
9
+ super(&perform_block)
10
+ end
11
+ self
12
+ rescue => e
13
+ raise PerformanceBombed.new(e, self)
14
+ end
15
+
16
+ def rollback(&block)
17
+ if block_given?
18
+ @rollback_block = block
19
+ else
20
+ super(&rollback_block)
21
+ end
22
+ self
23
+ end
24
+
25
+ def perform_block
26
+ @perform_block ||= Proc.new {}
27
+ end
28
+
29
+ def rollback_block
30
+ @rollback_block ||= Proc.new {}
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,36 @@
1
+ module Transactor
2
+ module Improv
3
+ class Transaction
4
+ attr_reader :performance
5
+
6
+ def perform
7
+ @transaction.performances << @performance
8
+ @transaction.transaction! do
9
+ performances.last.perform
10
+ end
11
+ end
12
+
13
+ def rollback(&block)
14
+ @performance.rollback(&block)
15
+ self
16
+ end
17
+
18
+ def actor
19
+ @performance.actor
20
+ end
21
+
22
+ def result
23
+ @performance.result
24
+ end
25
+
26
+ protected
27
+
28
+ def initialize(*args, &block)
29
+ @transaction = Transactor::Transaction.new
30
+ @performance = Improv::Performance.new(Improv::Actor, *args)
31
+ @performance.actor.rollback_on_failure!
32
+ @performance.perform(&block)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -2,7 +2,7 @@ module Transactor
2
2
  class Transaction
3
3
  attr_reader :performances, :result
4
4
 
5
- def in_transaction(&block)
5
+ def in_transaction(*args, &block)
6
6
  begin
7
7
  @result = instance_eval &block if block_given?
8
8
  rescue Exception => e # yes, we want to catch everything
@@ -16,22 +16,23 @@ module Transactor
16
16
  end
17
17
  end
18
18
 
19
- def transaction!(&block)
19
+ def transaction!(*args, &block)
20
20
  if defined?(ActiveRecord::Base) && ActiveRecord::Base.respond_to?(:transaction)
21
- ActiveRecord::Base.transaction { in_transaction &block }
21
+ ActiveRecord::Base.transaction { in_transaction *args, &block }
22
22
  else
23
- in_transaction &block
23
+ in_transaction *args, &block
24
24
  end
25
25
  self
26
26
  end
27
27
 
28
- def transaction(&block)
29
- transaction!(&block)
28
+ def transaction(*args, &block)
29
+ transaction!(*args, &block)
30
30
  rescue => e
31
31
  false
32
32
  end
33
33
 
34
34
  def perform(actor, *args, &block)
35
+ args = performance_args(*args)
35
36
  performance = Performance.new(actor, *args)
36
37
  performances << performance
37
38
  performance.perform(&block)
@@ -41,10 +42,14 @@ module Transactor
41
42
  end
42
43
 
43
44
  def improvise(*args, &block)
44
- performance = Improv.new(*args)
45
+ block ||= Proc.new {}
46
+ args = performance_args(*args)
47
+ performance = Improv::Performance.new(Improv::Actor, *args)
45
48
  performances << performance
46
49
  performance.perform(&block)
47
50
  performance
51
+ rescue PerformanceBombed => e
52
+ raise e
48
53
  rescue => e
49
54
  raise PerformanceBombed.new(e, performance)
50
55
  end
@@ -77,29 +82,47 @@ module Transactor
77
82
  end
78
83
  end
79
84
 
80
- def last_performance
81
- performances.last
85
+ def set_context!(*args)
86
+ @context = args.extract_options!.symbolize_keys
82
87
  end
83
88
 
84
- def rollback_last_performance?
85
- # only rollback the last performance if it didn't bomb OR if
86
- # it did and is configured to rollback on failure
87
- !last_performance.failed? || (last_performance.failed? && last_performance.rollback_on_failure?)
89
+ def clear_context!
90
+ @context = {}
88
91
  end
89
92
 
90
93
  def method_missing(meth, *args, &block)
91
- perform meth, *args, &block
92
- end
94
+ if meth.to_s.match(/=\Z/)
95
+ key = meth.to_s.gsub(/=/,'').to_sym
96
+ return (@context[key] = args.first) if @context.key?(key)
97
+ else
98
+ return @context[meth] if @context.key?(meth)
99
+ end
93
100
 
94
- def respond_to_missing?(meth, include_private=false)
95
- true # *shrug* we try to perform just about anything
101
+ perform meth, *args, &block
96
102
  end
97
103
 
98
104
  protected
99
105
 
100
106
  def initialize(*args, &block)
101
107
  @performances = []
108
+ set_context! *args
102
109
  transaction! &block if block_given?
103
110
  end
111
+
112
+ def performance_args(*args)
113
+ context = @context.merge(args.extract_options!.symbolize_keys)
114
+ args << context
115
+ args
116
+ end
117
+
118
+ def last_performance
119
+ performances.last
120
+ end
121
+
122
+ def rollback_last_performance?
123
+ # only rollback the last performance if it didn't bomb OR if
124
+ # it did and is configured to rollback on failure
125
+ !last_performance.failed? || (last_performance.failed? && last_performance.rollback_on_failure?)
126
+ end
104
127
  end
105
128
  end
@@ -1,3 +1,3 @@
1
1
  module Transactor
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -19,8 +19,8 @@ RSpec.describe Transactor::Actor do
19
19
 
20
20
  describe '#perform' do
21
21
  context 'when passed a block' do
22
- it 'executes the block and returns the value' do
23
- expect(subject.perform { 5+5 }).to eql(10)
22
+ it 'raises a PerformNotImplemented error' do
23
+ expect { subject.perform }.to raise_exception(Transactor::PerformNotImplemented)
24
24
  end
25
25
  end
26
26
 
@@ -33,8 +33,8 @@ RSpec.describe Transactor::Actor do
33
33
 
34
34
  describe '#rollback' do
35
35
  context 'when passed a block' do
36
- it 'executes the block and returns the value' do
37
- expect(subject.rollback { 5+5 }).to eql(10)
36
+ it 'raises a PerformNotImplemented error' do
37
+ expect { subject.rollback }.to raise_exception(Transactor::RollbackNotImplemented)
38
38
  end
39
39
  end
40
40
 
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Transactor::Improv::Actor do
4
+ subject { Transactor::Improv::Actor.new(test_one: 'one', test_two: 2) }
5
+
6
+ describe '#perform' do
7
+ context 'when passed a block' do
8
+ it 'executes the block and returns the value' do
9
+ expect(subject.perform { 5+5 }).to eql(10)
10
+ end
11
+ end
12
+
13
+ context 'when not passed a block' do
14
+ it 'raises a PerformNotImplemented error' do
15
+ expect { subject.perform }.to raise_exception(Transactor::PerformNotImplemented)
16
+ end
17
+ end
18
+ end
19
+
20
+ describe '#rollback' do
21
+ context 'when passed a block' do
22
+ it 'executes the block and returns the value' do
23
+ expect(subject.rollback { 5+5 }).to eql(10)
24
+ end
25
+ end
26
+
27
+ context 'when not passed a block' do
28
+ it 'raises a PerformNotImplemented error' do
29
+ expect { subject.rollback }.to raise_exception(Transactor::RollbackNotImplemented)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,9 +1,4 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe Transactor::Improv do
4
- describe '#initialize' do
5
- it 'uses the default actor' do
6
- expect(subject.actor).to be_an_instance_of(Transactor::Actor)
7
- end
8
- end
9
4
  end
@@ -67,7 +67,7 @@ RSpec.describe Transactor::Transaction do
67
67
 
68
68
  describe '#improvise' do
69
69
  it 'returns an improv performance' do
70
- expect(subject.improvise).to be_an_instance_of(Transactor::Improv)
70
+ expect(subject.improvise).to be_an_instance_of(Transactor::Improv::Performance)
71
71
  end
72
72
 
73
73
  it 'adds a performance to the performances array' do
@@ -77,11 +77,11 @@ RSpec.describe Transactor::Transaction do
77
77
 
78
78
  it 'casts the default actor in the performance' do
79
79
  subject.improvise
80
- expect(subject.performances.last.actor).to be_an_instance_of(Transactor::Actor)
80
+ expect(subject.performances.last.actor).to be_an_instance_of(Transactor::Improv::Actor)
81
81
  end
82
82
 
83
83
  it 'executes the improv block' do
84
- subject.improvise { 5+5 }
84
+ subject.improvise { 5+5 }.perform
85
85
  expect(subject.performances.last.result).to eql(10)
86
86
  end
87
87
 
@@ -89,7 +89,9 @@ RSpec.describe Transactor::Transaction do
89
89
  expect do
90
90
  subject.improvise do
91
91
  raise "Bombing"
92
- end
92
+ end.rollback do
93
+ puts "Rolling Back"
94
+ end.perform
93
95
  end.to raise_exception(Transactor::PerformanceBombed)
94
96
  end
95
97
  end
@@ -7,11 +7,9 @@ RSpec.describe Transactor do
7
7
  end
8
8
  end
9
9
 
10
- context 'configuration' do
11
- describe 'rollback_failed_actors' do
12
- it 'defaults to false' do
13
- expect(Transactor.configuration.rollback_failed_actors).to be_false
14
- end
10
+ describe '#improvise' do
11
+ it 'returns a Transactor::Improv::Transaction' do
12
+ expect(Transactor.improvise).to be_an_instance_of(Transactor::Improv::Transaction)
15
13
  end
16
14
  end
17
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transactor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rebec
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-22 00:00:00.000000000 Z
11
+ date: 2015-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -77,6 +77,9 @@ files:
77
77
  - lib/transactor/actor.rb
78
78
  - lib/transactor/errors.rb
79
79
  - lib/transactor/improv.rb
80
+ - lib/transactor/improv/actor.rb
81
+ - lib/transactor/improv/performance.rb
82
+ - lib/transactor/improv/transaction.rb
80
83
  - lib/transactor/performance.rb
81
84
  - lib/transactor/props.rb
82
85
  - lib/transactor/transaction.rb
@@ -84,6 +87,7 @@ files:
84
87
  - spec/spec_helper.rb
85
88
  - spec/support/test_actors.rb
86
89
  - spec/transactor/actor_spec.rb
90
+ - spec/transactor/improv/actor_spec.rb
87
91
  - spec/transactor/improv_spec.rb
88
92
  - spec/transactor/performance_spec.rb
89
93
  - spec/transactor/transaction_spec.rb
@@ -115,6 +119,7 @@ test_files:
115
119
  - spec/spec_helper.rb
116
120
  - spec/support/test_actors.rb
117
121
  - spec/transactor/actor_spec.rb
122
+ - spec/transactor/improv/actor_spec.rb
118
123
  - spec/transactor/improv_spec.rb
119
124
  - spec/transactor/performance_spec.rb
120
125
  - spec/transactor/transaction_spec.rb