File: active_support/core_ext/date/behavior.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: ActiveSupport#3
  module: CoreExtensions#4
  module: Date#5
  module: Behavior#6
has properties
method: acts_like_date? #9
method: freeze #28

Code

   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