Skip to content

Commit

Permalink
Merge branch 'flux-capacitor' of https://github.com/nashby/delorean i…
Browse files Browse the repository at this point in the history
…nto nashby-flux-capacitor

Conflicts:
	delorean.gemspec
  • Loading branch information
cavalle committed Jul 4, 2012
2 parents 23e774d + c58a18b commit bb5e70c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 53 deletions.
6 changes: 3 additions & 3 deletions delorean.gemspec
Expand Up @@ -6,13 +6,13 @@ Gem::Specification.new do |gem|
gem.date = Date.today.to_s

gem.summary = "Delorean lets you travel in time with Ruby by mocking Time.now"

gem.authors = ["Luismi Cavallé", "Sergio Gil"]
gem.email = "ballsbreaking@bebanjo.com"
gem.homepage = "http://github.com/bebanjo/delorean"

gem.extra_rdoc_files = ["README.md"]
gem.files = ["lib/delorean.rb", "README.md", "MIT-LICENSE"]
gem.files = ["lib/delorean.rb", "lib/flux_capacitor.rb", "README.md", "MIT-LICENSE"]

gem.add_runtime_dependency 'chronic', '>= 0'
end
52 changes: 2 additions & 50 deletions lib/delorean.rb
@@ -1,57 +1,9 @@
require 'chronic'
require 'flux_capacitor'

module Delorean
include FluxCapacitor
extend self

def time_travel_to(time, options={})
mock_current_time(time, options)
return unless block_given?
begin
yield
ensure
restore_previous_time
end
end

def back_to_the_present
reset
end
alias :back_to_1985 :back_to_the_present

def jump(seconds)
mock_current_time Time.now + seconds
return unless block_given?
begin
yield
ensure
restore_previous_time
end
end

def now
Time.now_without_delorean - time_travel_offsets.inject(0){ |sum, val| sum + val }
end

private

def time_travel_offsets
@@time_travel_offsets ||= []
end

def reset
@@time_travel_offsets = []
end

def mock_current_time(time, options={})
time = Chronic.parse(time, options) if time.is_a?(String)
time = Time.local(time.year, time.month, time.day) if time.is_a?(Date) && !time.is_a?(DateTime)

time_travel_offsets.push Time.now - time
end

def restore_previous_time
time_travel_offsets.pop
end
end

class << Time
Expand Down
53 changes: 53 additions & 0 deletions lib/flux_capacitor.rb
@@ -0,0 +1,53 @@
module Delorean
module FluxCapacitor
def time_travel_to(time, options={})
mock_current_time(time, options)
return unless block_given?
begin
yield
ensure
restore_previous_time
end
end

def back_to_the_present
reset
end
alias :back_to_1985 :back_to_the_present

def jump(seconds)
mock_current_time Time.now + seconds
return unless block_given?
begin
yield
ensure
restore_previous_time
end
end

def now
Time.now_without_delorean - time_travel_offsets.inject(0){ |sum, val| sum + val }
end

private

def time_travel_offsets
@@time_travel_offsets ||= []
end

def reset
@@time_travel_offsets = []
end

def mock_current_time(time, options={})
time = Chronic.parse(time, options) if time.is_a?(String)
time = Time.local(time.year, time.month, time.day) if time.is_a?(Date) && !time.is_a?(DateTime)

time_travel_offsets.push Time.now - time
end

def restore_previous_time
time_travel_offsets.pop
end
end
end

0 comments on commit bb5e70c

Please sign in to comment.