time_limits 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in time_limits.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,17 @@
1
+ Apply timeouts to methods via meta programming
2
+
3
+ Usage:
4
+
5
+ class Foo
6
+
7
+ def my_possibly_slow_method() sleep(rand 10); end
8
+ def my_possibly_slow_query() sleep(rand 10); rand; end
9
+
10
+ include TimeLimits
11
+ time_limit 1.second, :my_possibly_slow_method
12
+ time_limit 1.second, :my_possibly_slow_method, :rescue => false
13
+
14
+ end
15
+
16
+ a = Foo.new.my_possibly_slow_method # may throw Timeout
17
+ b = Foo.new.my_possibly_slow_query # may return false
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,32 @@
1
+ module TimeLimits
2
+ module ClassMethods
3
+
4
+ def time_limit( timeout, *names )
5
+ raise unless timeout.to_i > 0
6
+ options = names.last.kind_of?(Hash) ? names.pop : {}
7
+ raise unless names.size > 0
8
+ names.each do |name|
9
+ original = instance_method(name)
10
+ if options.has_key?(:rescue) then
11
+ rescue_value = options[:rescue]
12
+ define_method( name ) do |*args| # &block not supported in 1.8
13
+ TimeLimits::Timeout.in_time( timeout ) do
14
+ begin
15
+ original.bind(self).call( *args ) # &block not supported in 1.8
16
+ rescue ::Timeout::Error
17
+ rescue_value
18
+ end
19
+ end
20
+ end
21
+ else
22
+ define_method( name ) do |*args| # &block not supported in 1.8
23
+ TimeLimits::Timeout.in_time( timeout ) do
24
+ original.bind(self).call( *args ) # &block not supported in 1.8
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ if RUBY_VERSION =~ /^1\.8/ then
2
+ require "rubygems"
3
+ require "system_timer"
4
+ end
5
+
6
+ module TimeLimits
7
+ module Timeout
8
+
9
+ if RUBY_VERSION =~ /^1\.8/ then
10
+
11
+ def in_time( duration = 1, &block )
12
+ SystemTimer.timeout_after(duration) { yield }
13
+ end
14
+
15
+ else
16
+
17
+ def in_time( duration = 1, &block )
18
+ Timeout::timeout(duration) { yield }
19
+ end
20
+
21
+ end
22
+
23
+ # options :rescue value
24
+ def in_time_or_else( duration = 1, options = {}, &block )
25
+ in_time( duration, &block )
26
+ rescue ::Timeout::Error => x
27
+ return options[:rescue]
28
+ end
29
+
30
+ extend Timeout
31
+
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module TimeLimits
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "time_limits/timeout" # TimeLimits::Timeout.timeout( duration = 1.second, &block )
2
+ require "time_limits/class_methods" # time_limit timeout, *method_names, :rescue => value
3
+
4
+ module TimeLimits
5
+ class << self
6
+ def included( base )
7
+ base.send( :extend, ClassMethods )
8
+ end
9
+ end
10
+ end
data/test/test.rb ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+ $:.push File.expand_path("../../lib", __FILE__)
3
+ #require "rubygems"
4
+ #require "ruby-debug"
5
+ require "time_limits"
6
+
7
+ class X
8
+ def slow() sleep(2); end
9
+ def slow2() sleep(2); end
10
+ def slow3() sleep(3); end
11
+ def slow4() sleep(4); end
12
+ include TimeLimits
13
+ time_limit 1, :slow, :slow2, :slow3, :slow4
14
+ end
15
+
16
+ begin
17
+ X.new.slow2
18
+ raise "Failed"
19
+ rescue Timeout::Error => x
20
+ puts "Ok"
21
+ end
22
+
23
+ class X2
24
+ def slow() sleep(2); true; end
25
+ def slow2() sleep(2); end
26
+ include TimeLimits
27
+ time_limit 1, :slow, :slow2, :rescue => false
28
+ end
29
+
30
+ ok = X2.new.slow2
31
+ raise "Failed" unless ok == false
32
+
33
+ begin
34
+ TimeLimits::Timeout.in_time { sleep 2 }
35
+ raise "Failed"
36
+ rescue Timeout::Error => x
37
+ puts "Ok"
38
+ end
39
+
40
+ ok = TimeLimits::Timeout.in_time_or_else( 1, :rescue => false ) { sleep 2 }
41
+ raise unless ok == false
42
+ puts "Ok"
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "time_limits/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "time_limits"
7
+ s.version = TimeLimits::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Mark Lanett"]
10
+ s.email = ["mark.lanett@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Apply timeouts to methods via meta programming}
13
+
14
+ s.rubyforge_project = "time_limits"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_limits
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Mark Lanett
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-08 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email:
23
+ - mark.lanett@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - README
34
+ - Rakefile
35
+ - lib/time_limits.rb
36
+ - lib/time_limits/class_methods.rb
37
+ - lib/time_limits/timeout.rb
38
+ - lib/time_limits/version.rb
39
+ - test/test.rb
40
+ - time_limits.gemspec
41
+ has_rdoc: true
42
+ homepage: ""
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project: time_limits
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Apply timeouts to methods via meta programming
73
+ test_files:
74
+ - test/test.rb