retryable_record 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ coverage
2
+ pkg
3
+ doc
4
+ .yardoc
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Peter Suschlik
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.
data/README.rdoc ADDED
@@ -0,0 +1,41 @@
1
+ = retryable_record
2
+
3
+ Retries an operation on an ActiveRecord until no StaleObjectError is being raised.
4
+
5
+ Source[http://github.com/splattael/retryable_record] |
6
+ RDoc[http://rdoc.info/projects/splattael/retryable_record] |
7
+ Metrics[http://getcaliper.com/caliper/project?repo=http%3A%2F%2Fgemcutter.org%2Fgems%2Fretryable_record]
8
+
9
+ == Usage
10
+
11
+ require 'retryable_record'
12
+
13
+ class User < ActiveRecord::Base
14
+ include RetryableRecord
15
+ end
16
+
17
+ user = User.first
18
+
19
+ user.retryable do
20
+ user.username = "foo"
21
+ user.save!
22
+ end
23
+
24
+ == TODO
25
+
26
+ * Better example in README
27
+ * Intergration test with ActiveRecord
28
+
29
+ == Note on Patches/Pull Requests
30
+
31
+ * Fork the project.
32
+ * Make your feature addition or bug fix.
33
+ * Add tests for it. This is important so I don't break it in a
34
+ future version unintentionally.
35
+ * Commit, do not mess with rakefile, version, or history.
36
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
37
+ * Send me a pull request. Bonus points for topic branches.
38
+
39
+ == Copyright
40
+
41
+ Copyright (c) 2010 Peter Suschlik. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "retryable_record"
8
+ gem.summary = %Q{Retries an operation on an ActiveRecord until no StaleObjectError is being raised.}
9
+ # TODO gem.description = %Q{}
10
+ gem.email = "peter@suschlik.de"
11
+ gem.homepage = "http://github.com/splattael/retryable_record"
12
+ gem.authors = ["Peter Suschlik"]
13
+
14
+ gem.add_development_dependency "riot", ">= 0.10.11"
15
+ gem.add_development_dependency "riot_notifier"
16
+ gem.add_development_dependency "yard"
17
+
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
23
+ end
24
+
25
+ require 'rake/testtask'
26
+ Rake::TestTask.new(:test) do |test|
27
+ test.libs << 'lib' << 'test'
28
+ test.pattern = 'test/**/*_test.rb'
29
+ test.verbose = true
30
+ end
31
+
32
+ begin
33
+ require 'rcov/rcovtask'
34
+ Rcov::RcovTask.new do |test|
35
+ test.libs << 'test'
36
+ test.pattern = 'test/**/*_test.rb'
37
+ test.verbose = true
38
+ end
39
+ rescue LoadError
40
+ task :rcov do
41
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
42
+ end
43
+ end
44
+
45
+ task :test => :check_dependencies
46
+
47
+ task :default => :test
48
+
49
+ begin
50
+ require 'yard'
51
+ YARD::Rake::YardocTask.new
52
+ rescue LoadError
53
+ task :yardoc do
54
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
55
+ end
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,27 @@
1
+ require 'active_record'
2
+ require 'active_record/base'
3
+
4
+ # Retries an operation on an ActiveRecord until no StaleObjectError is being raised.
5
+ #
6
+ # @example
7
+ #
8
+ # class User < ActiveRecord::Base
9
+ # include RetryableRecord
10
+ # end
11
+ #
12
+ # user = User.first
13
+ #
14
+ # user.retryable do
15
+ # user.username = "foo"
16
+ # user.save!
17
+ # end
18
+ #
19
+ # @yield Operation that should be retried on failure ActiveRecord::StaleObjectError.
20
+ module RetryableRecord
21
+ def retryable(&block)
22
+ yield
23
+ rescue ActiveRecord::StaleObjectError
24
+ reload
25
+ retry
26
+ end
27
+ end
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{retryable_record}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Peter Suschlik"]
12
+ s.date = %q{2010-02-11}
13
+ s.email = %q{peter@suschlik.de}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/retryable_record.rb",
26
+ "retryable_record.gemspec",
27
+ "test.watchr",
28
+ "test/retryable_record_test.rb",
29
+ "test/teststrap.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/splattael/retryable_record}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{Retries an operation on an ActiveRecord until no StaleObjectError is being raised.}
36
+ s.test_files = [
37
+ "test/retryable_record_test.rb",
38
+ "test/teststrap.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<riot>, [">= 0.10.11"])
47
+ s.add_development_dependency(%q<riot_notifier>, [">= 0"])
48
+ s.add_development_dependency(%q<yard>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<riot>, [">= 0.10.11"])
51
+ s.add_dependency(%q<riot_notifier>, [">= 0"])
52
+ s.add_dependency(%q<yard>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<riot>, [">= 0.10.11"])
56
+ s.add_dependency(%q<riot_notifier>, [">= 0"])
57
+ s.add_dependency(%q<yard>, [">= 0"])
58
+ end
59
+ end
60
+
data/test.watchr ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env watchr
2
+
3
+ begin
4
+ require File.join(ENV["HOME"], ".watchr.test.rb")
5
+ rescue LoadError
6
+ warn "Unable to load #{File.join(ENV["HOME"], ".watchr.test.rb")}"
7
+ warn "You might try this: http://gist.github.com/raw/273574/8804dff44b104e9b8706826dc8882ed985b4fd13/.watchr.test.rb"
8
+ exit
9
+ end
10
+
11
+ run_tests
12
+
13
+ watch('test/.*_test\.rb') { |md| run md[0] }
14
+ watch('lib/(.*)\.rb') { |md| run "test/#{underscore(md[1])}_test.rb" }
15
+ watch('test/teststrap.rb') { run_tests }
@@ -0,0 +1,47 @@
1
+ require 'teststrap'
2
+
3
+ context "FakeRecord" do
4
+ setup do
5
+ FakeRecord.new
6
+ end
7
+
8
+ asserts("that block return value") do
9
+ topic.retryable { :return_value }
10
+ end.equals(:return_value)
11
+
12
+ asserts("that other errors") do
13
+ topic.retryable do
14
+ raise "other error"
15
+ end
16
+ end.raises(RuntimeError, "other error")
17
+
18
+ context "saves without retries" do
19
+ hookup do
20
+ topic.retryable do
21
+ topic.save
22
+ end
23
+ end
24
+
25
+ asserts("save counter") { topic.counter[:save] }.equals(1)
26
+ asserts("reload counter") { topic.counter[:reload] }.equals(0)
27
+ end
28
+
29
+ context "saves with 1 retry" do
30
+ hookup do
31
+ topic.retryable do
32
+ i = 0
33
+ topic.retryable do
34
+ if i == 0
35
+ i += 1
36
+ topic.concurrent_modification!
37
+ end
38
+
39
+ topic.save
40
+ end
41
+ end
42
+ end
43
+
44
+ asserts("save counter") { topic.counter[:save] }.equals(1)
45
+ asserts("reload counter") { topic.counter[:reload] }.equals(1)
46
+ end
47
+ end
data/test/teststrap.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'riot'
3
+ require 'riot_notifier'
4
+
5
+ require 'retryable_record'
6
+
7
+ Riot.reporter = RiotNotifier
8
+
9
+ class FakeRecord
10
+ include RetryableRecord
11
+
12
+ attr_accessor :counter
13
+
14
+ def initialize
15
+ @counter = Hash.new(0)
16
+ end
17
+
18
+ def reload
19
+ @counter[:reload] += 1
20
+ self
21
+ end
22
+
23
+ def save
24
+ @counter[:save] += 1
25
+ true
26
+ end
27
+
28
+ def concurrent_modification!
29
+ raise ActiveRecord::StaleObjectError
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: retryable_record
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Suschlik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-11 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: riot
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.10.11
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: riot_notifier
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: yard
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description:
46
+ email: peter@suschlik.de
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - .gitignore
57
+ - LICENSE
58
+ - README.rdoc
59
+ - Rakefile
60
+ - VERSION
61
+ - lib/retryable_record.rb
62
+ - retryable_record.gemspec
63
+ - test.watchr
64
+ - test/retryable_record_test.rb
65
+ - test/teststrap.rb
66
+ has_rdoc: true
67
+ homepage: http://github.com/splattael/retryable_record
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options:
72
+ - --charset=UTF-8
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.5
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Retries an operation on an ActiveRecord until no StaleObjectError is being raised.
94
+ test_files:
95
+ - test/retryable_record_test.rb
96
+ - test/teststrap.rb