# File lib/dm-core/property.rb, line 464
    def typecast(value)
      return type.typecast(value, self) if type.respond_to?(:typecast)
      return value if value.kind_of?(primitive) || value.nil?
      begin
        if    primitive == TrueClass  then %w[ true 1 t ].include?(value.to_s.downcase)
        elsif primitive == String     then value.to_s
        elsif primitive == Float      then value.to_f
        elsif primitive == Integer
          # The simplest possible implementation, i.e. value.to_i, is not
          # desirable because "junk".to_i gives "0". We want nil instead,
          # because this makes it clear that the typecast failed.
          #
          # After benchmarking, we preferred the current implementation over
          # these two alternatives:
          # * Integer(value) rescue nil
          # * Integer(value_to_s =~ /(\d+)/ ? $1 : value_to_s) rescue nil
          #
          # [YK] The previous implementation used a rescue. Why use a rescue
          # when the list of cases where a valid string other than "0" could
          # produce 0 is known?
          value_to_i = value.to_i
          if value_to_i == 0
            value.to_s =~ /^(0x|0b)?0+/ ? 0 : nil
          else
            value_to_i
          end
        elsif primitive == BigDecimal then BigDecimal(value.to_s)
        elsif primitive == DateTime   then typecast_to_datetime(value)
        elsif primitive == Date       then typecast_to_date(value)
        elsif primitive == Time       then typecast_to_time(value)
        elsif primitive == Class      then self.class.find_const(value)
        else
          value
        end
      rescue
        value
      end
    end