Permalink
Browse files

Version 0.1.0

  • Loading branch information...
0 parents commit 406e2d48ee0d10019fcf6ae010158d0cdace65e9 @cavalle cavalle committed Feb 24, 2010
Showing with 271 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +20 −0 MIT-LICENSE
  3. +31 −0 README.rdoc
  4. +66 −0 Rakefile
  5. +53 −0 lib/delorean.rb
  6. +99 −0 spec/delorean_spec.rb
@@ -0,0 +1,2 @@
+delorean.gemspec
+pkg/*
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Luismi Cavallé & Sergio Gil
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
+= Delorean
+
+Delorean let you time-travel with Ruby by mocking <code>Time.now</code>
+
+== Install
+
+ $ [sudo] gem install delorean
+
+== Usage
+
+ # Time.now => Wed Feb 24 00:33:09 +0100 2010
+ Delorean.time_travel_to "1 month ago"
+ # Time.now => Sun Jan 24 00:33:09 +0100 2010
+ Delorean.back_to_the_present
+ # Time.now => Wed Feb 24 00:33:09 +0100 2010
+
+ Delorean.time_travel_to("1 month ago") do
+ # Inside the block:
+ # Time.now => Sun Jan 24 00:34:32 +0100 2010
+ sleep(5)
+ # And the time still goes on... Time.now => Sun Jan 24 00:34:37 +0100 2010
+ end
+ # Outside the block:
+ # Time.now => Wed Feb 24 00:34:35 +0100 2010
+
+ # Time.now => Wed Feb 24 00:34:04 +0100 2010
+ Delorean.jump 30
+ # Time.now => Wed Feb 24 00:34:34 +0100 2010
+
+Copyright (c) 2010 Luismi Cavallé, Sergio Gil and BeBanjo S.L. released under the MIT license
+
@@ -0,0 +1,66 @@
+require "rubygems"
+require "rake/gempackagetask"
+require "rake/rdoctask"
+
+require "spec"
+require "spec/rake/spectask"
+Spec::Rake::SpecTask.new do |t|
+ t.spec_opts = %w(--format specdoc --colour)
+ t.libs = ["spec"]
+end
+
+
+task :default => ["spec"]
+
+# This builds the actual gem. For details of what all these options
+# mean, and other ones you can add, check the documentation here:
+#
+# http://rubygems.org/read/chapter/20
+#
+spec = Gem::Specification.new do |s|
+
+ s.name = "delorean"
+ s.version = "0.1.0"
+ s.summary = "Time-travel in Ruby"
+ s.authors = ["Luismi Cavallé", "Sergio Gil"]
+ s.email = "ballsbreaking@bebanjo.com"
+ s.homepage = "http://github.com/bebanjo/delorean"
+
+ s.has_rdoc = true
+ s.extra_rdoc_files = %w(README.rdoc)
+ s.rdoc_options = %w(--main README.rdoc)
+
+ s.files = %w() + Dir.glob("{spec,lib/**/*}")
+ s.require_paths = ["lib"]
+
+ s.add_dependency("chronic")
+
+ s.add_development_dependency("rspec")
+end
+
+# This task actually builds the gem. We also regenerate a static
+# .gemspec file, which is useful if something (i.e. GitHub) will
+# be automatically building a gem for this project. If you're not
+# using GitHub, edit as appropriate.
+#
+# To publish your gem online, install the 'gemcutter' gem; Read more
+# about that here: http://gemcutter.org/pages/gem_docs
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.gem_spec = spec
+
+ # Generate the gemspec file for github.
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
+ File.open(file, "w") {|f| f << spec.to_ruby }
+end
+
+# Generate documentation
+Rake::RDocTask.new do |rd|
+ rd.options << '--charset' << 'utf-8'
+ rd.rdoc_files.include("lib/**/*.rb")
+ rd.rdoc_dir = "rdoc"
+end
+
+desc 'Clear out RDoc and generated packages'
+task :clean => [:clobber_rdoc, :clobber_package] do
+ rm "#{spec.name}.gemspec"
+end
@@ -0,0 +1,53 @@
+require 'chronic'
+
+module Delorean
+ extend self
+
+ def time_travel_to(time)
+ mock_current_time(time)
+ return unless block_given?
+ begin
+ yield
+ ensure
+ restore_previous_time
+ end
+ end
+
+ def back_to_the_present
+ reset
+ end
+
+ def jump(seconds)
+ mock_current_time Time.now + seconds
+ 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)
+ time = Chronic.parse(time) if time.is_a?(String)
+ time = Time.local(time.year, time.month, time.day) if time.is_a?(Date)
+
+ time_travel_offsets.push Time.now - time
+ end
+
+ def restore_previous_time
+ time_travel_offsets.pop
+ end
+end
+
+class << Time
+ alias_method :now_without_delorean, :now
+ def now; Delorean.now; end
+end
@@ -0,0 +1,99 @@
+require 'rubygems'
+require File.dirname(__FILE__) + "/../lib/delorean"
+
+describe Delorean do
+
+ after(:each) do
+ Delorean.back_to_the_present
+ end
+
+ describe "time_travel_to" do
+ it "should travel through time" do
+ past_date = Time.utc(2009,1,1,10,30)
+ Delorean.time_travel_to past_date
+ Time.now.should be_close(past_date, 1)
+ end
+
+ it "should travel through time several times" do
+ past_date = Date.new(2009,1,1)
+ Delorean.time_travel_to Date.new(2009,2,20)
+ Delorean.time_travel_to past_date
+ Date.today.should == past_date
+ end
+
+ it "should travel to string times" do
+ two_minutes_from_now = Time.now + 120
+ Delorean.time_travel_to "2 minutes from now"
+ Time.now.should be_close(two_minutes_from_now, 1)
+ end
+ end
+
+ describe "back_to_the_present" do
+ it "should stay in the present if in the present" do
+ today = Date.today
+ Delorean.back_to_the_present
+ Date.today.should == today
+ end
+
+ it "should go back to the present if not in the present" do
+ today = Date.today
+ Delorean.time_travel_to Time.local(2009,2,2,1,1)
+ Delorean.back_to_the_present
+ Date.today.should == today
+ end
+
+ it "should go back to the original present if travelled through time several times" do
+ today = Date.today
+ 2.times { Delorean.time_travel_to Time.local(2009,2,2,1,1) }
+ Delorean.back_to_the_present
+ Date.today.should == today
+ end
+ end
+
+ describe "time_travel_to with block" do
+ it "should travel through time" do
+ past_date = Time.utc(2009,1,1,10,30)
+ Delorean.time_travel_to(past_date) do
+ Time.now.should be_close(past_date, 1)
+ end
+ end
+
+ it "should return to the future" do
+ today = Date.today
+ Delorean.time_travel_to(Time.utc(2009,2,2,10,30)) {}
+ Date.today.should == today
+ end
+
+ it "should travel through time several times" do
+ Delorean.time_travel_to(Time.utc(2009,2,2,10,40)) do
+ Delorean.time_travel_to(Time.utc(2009,1,1,22,45)) do
+ Date.today.should == Date.new(2009,1,1)
+ end
+ end
+ end
+
+ it "should still return to the future" do
+ today = Date.today
+ Delorean.time_travel_to(Time.utc(2009,2,2,10,40)) do
+ Delorean.time_travel_to(Time.utc(2009,1,1,10,40)) {}
+ Date.today.should == Date.new(2009,2,2)
+ end
+ Date.today.should == today
+ end
+
+ it "should travel to string times" do
+ two_minutes_ago = Time.now - 120
+ Delorean.time_travel_to("2 minutes ago") do
+ Time.now.should be_close(two_minutes_ago, 1)
+ end
+ end
+ end
+
+ describe "jump" do
+ it "should jump the given number of seconds to the future" do
+ expected = Time.now + 60
+ Delorean.jump 60
+ Time.now.should be_close(expected, 1)
+ end
+ end
+end

0 comments on commit 406e2d4

Please sign in to comment.