delorean 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +31 -0
  2. data/lib/delorean.rb +53 -0
  3. metadata +77 -0
@@ -0,0 +1,31 @@
1
+ = Delorean
2
+
3
+ Delorean let you time-travel in Ruby by mocking <code>Time.now</code>
4
+
5
+ == Install
6
+
7
+ $ [sudo] gem install delorean
8
+
9
+ == Usage
10
+
11
+ # Time.now => Wed Feb 24 00:33:09 +0100 2010
12
+ Delorean.time_travel_to "1 month ago"
13
+ # Time.now => Sun Jan 24 00:33:09 +0100 2010
14
+ Delorean.back_to_the_present
15
+ # Time.now => Wed Feb 24 00:33:09 +0100 2010
16
+
17
+ Delorean.time_travel_to("1 month ago") do
18
+ # Inside the block:
19
+ # Time.now => Sun Jan 24 00:34:32 +0100 2010
20
+ sleep(5)
21
+ # And the time still goes on... Time.now => Sun Jan 24 00:34:37 +0100 2010
22
+ end
23
+ # Outside the block:
24
+ # Time.now => Wed Feb 24 00:34:35 +0100 2010
25
+
26
+ # Time.now => Wed Feb 24 00:34:04 +0100 2010
27
+ Delorean.jump 30
28
+ # Time.now => Wed Feb 24 00:34:34 +0100 2010
29
+
30
+ Copyright (c) 2010 Luismi Cavallé, Sergio Gil and BeBanjo S.L. released under the MIT license
31
+
@@ -0,0 +1,53 @@
1
+ require 'chronic'
2
+
3
+ module Delorean
4
+ extend self
5
+
6
+ def time_travel_to(time)
7
+ mock_current_time(time)
8
+ return unless block_given?
9
+ begin
10
+ yield
11
+ ensure
12
+ restore_previous_time
13
+ end
14
+ end
15
+
16
+ def back_to_the_present
17
+ reset
18
+ end
19
+
20
+ def jump(seconds)
21
+ mock_current_time Time.now + seconds
22
+ end
23
+
24
+ def now
25
+ Time.now_without_delorean - time_travel_offsets.inject(0){ |sum, val| sum + val }
26
+ end
27
+
28
+ private
29
+
30
+ def time_travel_offsets
31
+ @@time_travel_offsets ||= []
32
+ end
33
+
34
+ def reset
35
+ @@time_travel_offsets = []
36
+ end
37
+
38
+ def mock_current_time(time)
39
+ time = Chronic.parse(time) if time.is_a?(String)
40
+ time = Time.local(time.year, time.month, time.day) if time.is_a?(Date)
41
+
42
+ time_travel_offsets.push Time.now - time
43
+ end
44
+
45
+ def restore_previous_time
46
+ time_travel_offsets.pop
47
+ end
48
+ end
49
+
50
+ class << Time
51
+ alias_method :now_without_delorean, :now
52
+ def now; Delorean.now; end
53
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delorean
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "Luismi Cavall\xC3\xA9"
8
+ - Sergio Gil
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2010-02-24 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: chronic
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ type: :development
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ version:
36
+ description:
37
+ email: ballsbreaking@bebanjo.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - README.rdoc
44
+ files:
45
+ - lib/delorean.rb
46
+ - README.rdoc
47
+ has_rdoc: true
48
+ homepage: http://github.com/bebanjo/delorean
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --main
54
+ - README.rdoc
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Time-travel in Ruby
76
+ test_files: []
77
+