Project

General

Profile

Backport #5270 ยป date-once.patch

Patch that fixes the issue - ddfreyne (Denis Defreyne), 09/03/2011 05:13 PM

View differences:

lib/date.rb
1002 1002

  
1003 1003
  class << self
1004 1004

  
1005
    def once(*ids) # :nodoc:
1006
      for id in ids
1007
	module_eval <<-"end;"
1008
	  alias_method :__#{id.to_i}__, :#{id.to_s}
1009
	  private :__#{id.to_i}__
1010
	  def #{id.to_s}(*args, &block)
1011
	    (@__#{id.to_i}__ ||= [__#{id.to_i}__(*args, &block)])[0]
1012
	  end
1013
	end;
1005
    def once(*meths)
1006
      meths.each do |meth|
1007
        # Alias
1008
        orig_meth = '__nonmemoized_' + meth.to_s
1009
        alias_method orig_meth, meth
1010

  
1011
        # Redefine
1012
        define_method(meth) do |*args|
1013
          # Get cache
1014
          @__memoization_cache ||= {}
1015
          @__memoization_cache[meth] ||= {}
1016

  
1017
          # Recalculate or fetch
1018
          cache = @__memoization_cache[meth]
1019
          if !cache.has_key?(args)
1020
            cache[args] = send(orig_meth, *args)
1021
          else
1022
            result = cache[args]
1023
          end
1024
        end
1014 1025
      end
1015 1026
    end
1016 1027

  
......
1037 1048
  #
1038 1049
  # Using one of the factory methods such as Date::civil is
1039 1050
  # generally easier and safer.
1040
  def initialize(ajd=0, of=0, sg=ITALY) @ajd, @of, @sg = ajd, of, sg end
1051
  def initialize(ajd=0, of=0, sg=ITALY)
1052
    @ajd, @of, @sg = ajd, of, sg
1053
    @__memoization_cache = {}
1054
  end
1041 1055

  
1042 1056
  # Get the date as an Astronomical Julian Day Number.
1043 1057
  def ajd() @ajd end
test/ruby/test_date.rb
1
require 'test/unit'
2

  
3
require 'date'
4

  
5
class TestDate < Test::Unit::TestCase
6

  
7
  def test_freeze
8
    foo = Date.new
9
    foo.freeze
10
    foo.year
11
  end
12

  
13
end