Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Make Time "Stand Still" in Ruby

When writing tests, it's sometimes difficult to test results of methods that use current time. Two calls to Time.now can give different results and cause test failures. Here is some code that lets you execute a block during which Time.now will return a consistent value:

   1  
   2  class Time
   3  
   4    # this code adds a Time.freeze method to make time "stand still"
   5    # during execution of a block. This can be helpful during testing of
   6    # methods that depend on Time.new or Time.now
   7    #
   8    # Example:
   9    #
  10    #   Time.freeze do
  11    #      puts Time.new.to_f
  12    #      # ... do stuff; real time passes
  13    #      puts Time.new.to_f     # outputs same time as above
  14    #   end
  15    #   # ... time returns to normal
  16    #
  17    # An optional Time object may be passed to freeze to a specific time:
  18    #
  19    #   Time.freeze(Time.at(2007, 11, 15)) do
  20    #      # ...
  21    #   end
  22    #
  23    # While inside the block, Time.frozen? will return true
  24  
  25    class << self
  26  
  27      def now
  28        @time || orig_new
  29      end
  30  
  31      alias_method :orig_freeze, :freeze
  32      alias_method :orig_new, :new
  33      alias_method :new, :now
  34  
  35      # makes time "stand still" during execution of a block. if no time is
  36      # supplied, the current time is used. While in the block, Time.new and
  37      # Time.now will always return the "frozen" value.
  38      def freeze(time = nil)
  39        raise "A block is required" unless block_given?
  40        begin
  41          prev = @time
  42          @time = time || now
  43          yield
  44        ensure
  45          @time = prev
  46        end
  47      end
  48  
  49      def frozen?
  50        !@time.nil?
  51      end
  52  
  53    end
  54  end

Rake Freezes Rails

Stolen from: ZenSpider: http://blog.zenspider.com/archives/2006/08/upgrade_rails_n.html

If you prefer to freeze your rails checkout. I recommend stealing this rule:
   1  
   2  namespace :rails do
   3    namespace :freeze do
   4      desc "Lock to a specific rails version. Defaults to 1.1.5 or specify with RELEASE=x.y.z"
   5      task :version do
   6        rel = ENV['RELEASE'] || '1.1.5'
   7        tag = 'rel_' + rel.split(/[.-]/).join('-')
   8        rails_svn = "http://dev.rubyonrails.org/svn/rails/tags/#{tag}"
   9  
  10        puts "Freezing to #{tag} using #{rails_svn}"
  11        sh "type svn"
  12        
  13        dir = 'vendor/rails'
  14        rm_rf dir
  15        mkdir_p dir
  16        for framework in %w( railties actionpack activerecord actionmailer activesupport actionwebservice )
  17          checkout = "#{dir}/#{framework}"
  18          sh "svn export #{rails_svn}/#{framework} #{checkout}"
  19          unless test ?d, checkout then
  20            puts "ERROR: checkout missing: #{checkout}"
  21            exit 1
  22          end
  23        end
  24      end
  25    end
  26  end

and running:
   1  
   2  rake rails:freeze:version

It'll default to 1.1.5 or you can specify the tagged version you want using RELEASE=x.y.z.
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS