the_force 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +13 -3
- data/lib/the_force/keep_trying.rb +20 -4
- data/lib/the_force/object_support.rb +52 -0
- data/lib/the_force/timer.rb +1 -0
- data/lib/the_force.rb +6 -3
- data/test/unit/test_keep_trying.rb +55 -0
- data/test/unit/test_object_support.rb +61 -0
- metadata +9 -5
data/Rakefile
CHANGED
@@ -1,17 +1,19 @@
|
|
1
|
+
require 'rubygems'
|
1
2
|
require 'rake'
|
3
|
+
require 'rake/testtask'
|
2
4
|
|
3
5
|
include_file_globs = ['README.rdoc', 'LICENSE', 'Rakefile', 'init.rb', '{lib}/**/*']
|
4
6
|
exclude_file_globs = []
|
5
7
|
|
6
8
|
spec = Gem::Specification.new do |s|
|
7
9
|
s.name = "the_force"
|
8
|
-
s.version = '0.0.
|
10
|
+
s.version = '0.0.2'
|
9
11
|
s.author = "Ryan Ziegler"
|
10
12
|
s.email = "info@symbolforce.com"
|
11
13
|
s.homepage = "http://www.symbolforce.com"
|
12
14
|
s.platform = Gem::Platform::RUBY
|
13
15
|
s.summary = "Common code for Symbolforce"
|
14
|
-
s.description = "Common code for Symbolforce"
|
16
|
+
s.description = "Common code for Symbolforce. tests pass in 1.8.6, 1.8.7, 1.9.1, and jruby 1.4"
|
15
17
|
s.files = FileList[include_file_globs].to_a - FileList[exclude_file_globs].to_a
|
16
18
|
s.require_path = "lib"
|
17
19
|
s.test_files = FileList["test/**/test_*.rb"].to_a
|
@@ -34,5 +36,13 @@ task :clean do
|
|
34
36
|
end
|
35
37
|
|
36
38
|
desc 'Default, do nothing'
|
37
|
-
task :default do
|
39
|
+
task :default => :test do
|
40
|
+
end
|
41
|
+
|
42
|
+
Rake::TestTask.new do |t|
|
43
|
+
t.libs << "lib/the_force/"
|
44
|
+
t.test_files = FileList['test/**/*.rb']
|
45
|
+
t.ruby_opts = ["-r rubygems"]
|
46
|
+
t.verbose = true
|
47
|
+
t.warning = true
|
38
48
|
end
|
@@ -1,26 +1,42 @@
|
|
1
|
-
|
1
|
+
module TheForce; end
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'system_timer'
|
5
|
+
def TheForce.timeout(*args, &b)
|
6
|
+
SystemTimer.timeout_after(*args, &b)
|
7
|
+
end
|
8
|
+
rescue LoadError => e
|
9
|
+
puts "WARNING - SystemTimer gem not found...reverting to Timeout::timeout"
|
10
|
+
require 'timeout'
|
11
|
+
def TheForce.timeout(*args, &b)
|
12
|
+
Timeout.timeout(*args, &b)
|
13
|
+
end
|
14
|
+
end
|
2
15
|
|
3
16
|
#CRZ - :exceptions uses is_a? not instance_of?
|
4
17
|
module TheForce
|
5
|
-
def
|
18
|
+
def keep_trying(options = {}, &b)
|
6
19
|
options = {:exceptions => [StandardError], :timeout => false}.merge(options)
|
7
20
|
options[:exceptions] = [options[:exceptions]] unless options[:exceptions].is_a? Array
|
8
21
|
options[:exceptions] << Timeout::Error if options[:timeout]
|
22
|
+
options[:times] ||= 3
|
9
23
|
|
10
24
|
try = 0;
|
11
25
|
loop do
|
12
26
|
begin
|
13
27
|
try += 1
|
14
28
|
if options[:timeout]
|
15
|
-
|
29
|
+
TheForce.timeout(options[:timeout]) do
|
16
30
|
return yield(try)
|
17
31
|
end
|
18
32
|
else
|
19
33
|
return yield(try)
|
20
34
|
end
|
21
35
|
rescue Exception => e
|
22
|
-
raise if (try >= times) or not options[:exceptions].any? {|exception| e.is_a? exception}
|
36
|
+
raise if (try >= options[:times]) or not options[:exceptions].any? {|exception| e.is_a? exception}
|
23
37
|
end
|
24
38
|
end
|
25
39
|
end
|
40
|
+
|
41
|
+
module_function :keep_trying
|
26
42
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#CRZ - imitate andand to a great degree.
|
2
|
+
# Could have something more complex than if or unless, but they're enough, and didnt want to introduce proc overhead.
|
3
|
+
# - logic duplication, three ""inverted^ return ? caller : nil""s
|
4
|
+
### - better behavior for args WITH a block?
|
5
|
+
|
6
|
+
if Module.constants.include?('BasicObject')
|
7
|
+
class ConditionalReturningMe < BasicObject
|
8
|
+
end
|
9
|
+
else
|
10
|
+
class ConditionalReturningMe
|
11
|
+
instance_methods.reject { |m| m =~ /^__/ }.each { |m| undef_method m }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ConditionalReturningMe
|
16
|
+
def initialize(me, inverted)
|
17
|
+
super()
|
18
|
+
@me = me
|
19
|
+
@inverted = inverted
|
20
|
+
end
|
21
|
+
|
22
|
+
def method_missing(sym, *args, &b)
|
23
|
+
(@inverted ^ @me.send(sym, *args, &b)) ? @me : nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module TheForce
|
28
|
+
module ObjectSupport
|
29
|
+
def if(*args, &b)
|
30
|
+
ObjectSupport.conditional(self, false, *args, &b)
|
31
|
+
end
|
32
|
+
|
33
|
+
def unless(*args, &b)
|
34
|
+
ObjectSupport.conditional(self, true, *args, &b)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def self.conditional(who, inverted, *args, &b)
|
39
|
+
if block_given?
|
40
|
+
(inverted ^ yield(who)) ? who : nil
|
41
|
+
elsif args.length > 0
|
42
|
+
(inverted ^ who.send(*args)) ? who : nil
|
43
|
+
else
|
44
|
+
ConditionalReturningMe.new(who, inverted)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class Object
|
51
|
+
include TheForce::ObjectSupport
|
52
|
+
end
|
data/lib/the_force/timer.rb
CHANGED
@@ -11,6 +11,7 @@ module TheForce
|
|
11
11
|
|
12
12
|
#CRZ - for mixin; expects just a symbol and list of args
|
13
13
|
# - helps solve passing symbol problem
|
14
|
+
###CRZ - rewrite with module_function? and move send()'ing into self.timer as well...
|
14
15
|
def timer(*args, &b);
|
15
16
|
if block_given?
|
16
17
|
Force::Timer.timer(*args, &b)
|
data/lib/the_force.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
-
|
2
|
-
require '
|
3
|
-
require '
|
1
|
+
ROOT = File.join(File.dirname(__FILE__), 'the_force')
|
2
|
+
require File.join(ROOT, 'keep_trying')
|
3
|
+
require File.join(ROOT, 'thread_pool')
|
4
|
+
require File.join(ROOT, 'timer')
|
5
|
+
#require File.join(ROOT, 'object_support')
|
6
|
+
true
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
begin
|
3
|
+
require File.join(File.dirname(__FILE__), '../../lib/the_force/keep_trying.rb')
|
4
|
+
rescue LoadError
|
5
|
+
require File.expand_path('../../../lib/the_force/keep_trying.rb', __FILE__)
|
6
|
+
end
|
7
|
+
|
8
|
+
class TestKeepTrying < Test::Unit::TestCase
|
9
|
+
def test_defaults_to_three_tries
|
10
|
+
assert_raise TestKeepTryingSpecialError do
|
11
|
+
TheForce.keep_trying do |try|
|
12
|
+
@try = try
|
13
|
+
raise TestKeepTryingSpecialError
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
assert_equal 3, @try
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_will_throw_after_one_thousand_tries
|
21
|
+
assert_raise TestKeepTryingSpecialError do
|
22
|
+
TheForce.keep_trying(:times => 1000) do
|
23
|
+
raise TestKeepTryingSpecialError
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_will_throw_after_specific_number_of_tries
|
29
|
+
succeeded = false
|
30
|
+
|
31
|
+
begin
|
32
|
+
TheForce.keep_trying(:times => 5) do |try|
|
33
|
+
raise StandardError, "try ##{try}"
|
34
|
+
succeeded = true
|
35
|
+
end
|
36
|
+
rescue StandardError => e
|
37
|
+
assert_equal "try #5", e.message
|
38
|
+
end
|
39
|
+
|
40
|
+
assert_not_equal true, succeeded
|
41
|
+
end
|
42
|
+
|
43
|
+
include TheForce
|
44
|
+
def test_keep_trying_is_a_module_function
|
45
|
+
assert self.respond_to?(:keep_trying, true)
|
46
|
+
assert !self.respond_to?(:keep_trying, false)
|
47
|
+
assert_respond_to TheForce, :keep_trying
|
48
|
+
end
|
49
|
+
|
50
|
+
#def test_works_without_systemtimer
|
51
|
+
#def test_works_with_systemtimer
|
52
|
+
end
|
53
|
+
|
54
|
+
class TestKeepTryingSpecialError < StandardError
|
55
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
begin
|
3
|
+
require File.join(File.dirname(__FILE__), '../../lib/the_force/object_support.rb')
|
4
|
+
rescue LoadError
|
5
|
+
require File.expand_path('../../../lib/the_force/keep_trying.rb', __FILE__)
|
6
|
+
end
|
7
|
+
|
8
|
+
class TestObjectSupport < Test::Unit::TestCase
|
9
|
+
def test_blank_slate_is_truly_blank
|
10
|
+
assert_equal "a", "a".if.length
|
11
|
+
assert_equal nil, "a".unless.length
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def test_if_as_blank_slate_when_it_returns_true
|
16
|
+
assert_equal "", "".if.empty?
|
17
|
+
end
|
18
|
+
def test_unless_as_blank_slate_when_it_returns_true
|
19
|
+
assert_equal nil, "".unless.empty?
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
def test_if_as_blank_slate_when_it_returns_false
|
24
|
+
assert_equal nil, "a".if.empty?
|
25
|
+
end
|
26
|
+
def test_unless_as_blank_slate_when_it_returns_false
|
27
|
+
assert_equal "a", "a".unless.nil?
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def test_if_with_block_when_it_returns_true
|
32
|
+
assert_equal "", "".if {|x| x.empty? }
|
33
|
+
end
|
34
|
+
def test_unless_with_block_when_it_returns_true
|
35
|
+
assert_equal nil, "".unless {|x| x.empty? }
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
def test_if_with_block_when_it_returns_false
|
40
|
+
assert_equal nil, "a".if {|x| x.nil? }
|
41
|
+
end
|
42
|
+
def test_unless_with_block_when_it_returns_false
|
43
|
+
assert_equal "a", "a".unless {|x| x.nil? }
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
def test_if_with_symbol_and_args_when_it_return_true
|
48
|
+
assert_equal "a", "a".if(:eql?, "a")
|
49
|
+
end
|
50
|
+
def test_unless_with_symbol_and_args_when_it_return_true
|
51
|
+
assert_equal nil, "a".unless(:eql?, "a")
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def test_if_with_symbol_and_args_when_it_return_false
|
56
|
+
assert_equal nil, "a".if(:eql?, "b")
|
57
|
+
end
|
58
|
+
def test_unless_with_symbol_and_args_when_it_return_false
|
59
|
+
assert_equal "a", "a".unless(:eql?, "b")
|
60
|
+
end
|
61
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: the_force
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Ziegler
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-02-05 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description: Common code for Symbolforce
|
16
|
+
description: Common code for Symbolforce. tests pass in 1.8.6, 1.8.7, 1.9.1, and jruby 1.4
|
17
17
|
email: info@symbolforce.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -27,9 +27,12 @@ files:
|
|
27
27
|
- Rakefile
|
28
28
|
- init.rb
|
29
29
|
- lib/the_force/keep_trying.rb
|
30
|
+
- lib/the_force/object_support.rb
|
30
31
|
- lib/the_force/thread_pool.rb
|
31
32
|
- lib/the_force/timer.rb
|
32
33
|
- lib/the_force.rb
|
34
|
+
- test/unit/test_keep_trying.rb
|
35
|
+
- test/unit/test_object_support.rb
|
33
36
|
has_rdoc: true
|
34
37
|
homepage: http://www.symbolforce.com
|
35
38
|
licenses: []
|
@@ -59,5 +62,6 @@ rubygems_version: 1.3.5
|
|
59
62
|
signing_key:
|
60
63
|
specification_version: 3
|
61
64
|
summary: Common code for Symbolforce
|
62
|
-
test_files:
|
63
|
-
|
65
|
+
test_files:
|
66
|
+
- test/unit/test_keep_trying.rb
|
67
|
+
- test/unit/test_object_support.rb
|