proc_rollback 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -0
- data/lib/proc_rollback.rb +43 -0
- data/lib/proc_rollback/version.rb +3 -0
- data/spec/proc_rollback_spec.rb +80 -0
- metadata +86 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2011 Michael Klaus
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# check fo tracing of attr_* methods
|
2
|
+
c = Class.new do
|
3
|
+
attr_accessor :foo
|
4
|
+
end
|
5
|
+
o = c.new
|
6
|
+
Thread.new do
|
7
|
+
result = false
|
8
|
+
Thread.current.send :set_trace_func, lambda {|type, file, line, mth, bnd, cls|
|
9
|
+
result = true if mth == :foo= and eval('self',bnd) == o
|
10
|
+
}
|
11
|
+
o.foo=o.foo
|
12
|
+
Thread.current.send :set_trace_func, nil
|
13
|
+
result
|
14
|
+
end.value or puts("Tracing of attr_* methods does not work. Check http://redmine.ruby-lang.org/issues/4583 to see which ruby version you can use. Proc_rollback will not work in some cases.")
|
15
|
+
|
16
|
+
# add rollbackablility to Proc
|
17
|
+
class Proc
|
18
|
+
def call_rollbackable(*args, &b)
|
19
|
+
vars = {}
|
20
|
+
in_trace = false
|
21
|
+
tf = proc{ |action, file, line, mth, bnd, cls|
|
22
|
+
unless in_trace or (file == __FILE__)
|
23
|
+
in_trace = true
|
24
|
+
obj = eval("self", bnd)
|
25
|
+
vars[obj] ||= obj.instance_variables.inject({}){ |r, n| r.merge n => obj.instance_variable_get(n)}
|
26
|
+
in_trace = false
|
27
|
+
end
|
28
|
+
}
|
29
|
+
|
30
|
+
set_trace_func tf
|
31
|
+
result = call(*args, &b)
|
32
|
+
set_trace_func nil
|
33
|
+
|
34
|
+
class << self; self; end.send :define_method, :rollback do
|
35
|
+
vars.each_pair do |obj, vars|
|
36
|
+
vars.each_pair do |n, v|
|
37
|
+
obj.instance_variable_set(n, v)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
result
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'proc_rollback'
|
2
|
+
|
3
|
+
class SomeClass
|
4
|
+
class << self
|
5
|
+
def modify_class_var(new_value)
|
6
|
+
@@class_var = new_value
|
7
|
+
end
|
8
|
+
def class_var
|
9
|
+
@@class_var
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_accessor :class_inst_var
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_accessor :inst_var
|
16
|
+
def modify_inst_var(new_value)
|
17
|
+
self.inst_var = new_value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module SomeModule
|
22
|
+
class << self
|
23
|
+
attr_accessor :inst_var
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe Proc do
|
28
|
+
it 'should provide a rollback method after the call' do
|
29
|
+
p = proc{}
|
30
|
+
p.call_rollbackable
|
31
|
+
p.respond_to?(:rollback).should == true
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should return the value of the proc' do
|
35
|
+
p = proc{ 'foo' }
|
36
|
+
p.call_rollbackable.should == 'foo'
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should rollback object instance variables' do
|
40
|
+
@obj = SomeClass.new
|
41
|
+
@obj.inst_var = 'foo'
|
42
|
+
p = proc{ @obj.inst_var = 'bar' }
|
43
|
+
p.call_rollbackable
|
44
|
+
@obj.inst_var.should == 'bar'
|
45
|
+
|
46
|
+
p.rollback
|
47
|
+
@obj.inst_var.should == 'foo'
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should rollback class instance variables' do
|
51
|
+
SomeClass.class_inst_var = 'foo'
|
52
|
+
p = proc{ SomeClass.class_inst_var = 'bar' }
|
53
|
+
p.call_rollbackable
|
54
|
+
SomeClass.class_inst_var.should == 'bar'
|
55
|
+
|
56
|
+
p.rollback
|
57
|
+
SomeClass.class_inst_var.should == 'foo'
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should rollback module instance variables' do
|
61
|
+
SomeModule.inst_var = 'foo'
|
62
|
+
p = proc{ SomeModule.inst_var = 'bar' }
|
63
|
+
p.call_rollbackable
|
64
|
+
SomeModule.inst_var.should == 'bar'
|
65
|
+
|
66
|
+
p.rollback
|
67
|
+
SomeModule.inst_var.should == 'foo'
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should rollback method calls' do
|
71
|
+
@obj = SomeClass.new
|
72
|
+
@obj.inst_var = 'foo'
|
73
|
+
p = proc{ @obj.modify_inst_var 'bar' }
|
74
|
+
p.call_rollbackable
|
75
|
+
@obj.inst_var.should == 'bar'
|
76
|
+
|
77
|
+
p.rollback
|
78
|
+
@obj.inst_var.should == 'foo'
|
79
|
+
end
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: proc_rollback
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Michael Klaus
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-04-18 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Rollback the side effects of a Proc by resetting the modified instance variables. Does not work on native code!
|
36
|
+
email:
|
37
|
+
- Michael.Klaus@gmx.net
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- lib/proc_rollback/version.rb
|
46
|
+
- lib/proc_rollback.rb
|
47
|
+
- spec/proc_rollback_spec.rb
|
48
|
+
- LICENSE
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/QaDeS/proc_rollback
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 23
|
73
|
+
segments:
|
74
|
+
- 1
|
75
|
+
- 3
|
76
|
+
- 6
|
77
|
+
version: 1.3.6
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project: proc_rollback
|
81
|
+
rubygems_version: 1.6.2
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Lets you rollback the side effects of a Proc by resetting the modified instance variables.
|
85
|
+
test_files: []
|
86
|
+
|