~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activesupport/lib/active_support/core_ext/date/behavior.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'date'
2
 
 
3
 
module ActiveSupport #:nodoc:
4
 
  module CoreExtensions #:nodoc:
5
 
    module Date #:nodoc:
6
 
      module Behavior
7
 
        # Enable more predictable duck-typing on Date-like classes. See
8
 
        # Object#acts_like?.
9
 
        def acts_like_date?
10
 
          true
11
 
        end
12
 
 
13
 
        # Date memoizes some instance methods using metaprogramming to wrap
14
 
        # the methods with one that caches the result in an instance variable.
15
 
        #
16
 
        # If a Date is frozen but the memoized method hasn't been called, the
17
 
        # first call will result in a frozen object error since the memo
18
 
        # instance variable is uninitialized.
19
 
        #
20
 
        # Work around by eagerly memoizing before freezing.
21
 
        #
22
 
        # Ruby 1.9 uses a preinitialized instance variable so it's unaffected.
23
 
        # This hack is as close as we can get to feature detection:
24
 
        begin
25
 
          ::Date.today.freeze.jd
26
 
        rescue => frozen_object_error
27
 
          if frozen_object_error.message =~ /frozen/
28
 
            def freeze #:nodoc:
29
 
              self.class.private_instance_methods(false).each do |m|
30
 
                if m.to_s =~ /\A__\d+__\Z/
31
 
                  instance_variable_set(:"@#{m}", [send(m)])
32
 
                end
33
 
              end
34
 
 
35
 
              super
36
 
            end
37
 
          end
38
 
        end
39
 
      end
40
 
    end
41
 
  end
42
 
end