Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7474

Show
Ignore:
Timestamp:
09/14/07 00:34:43 (1 year ago)
Author:
bitsweat
Message:

Some 1.9 forward compatibility

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/core_ext/array/conversions.rb

    r7074 r7474  
    2626          join '/' 
    2727        end 
    28          
    29         def self.included(klass) #:nodoc: 
    30           klass.send(:alias_method, :to_default_s, :to_s) 
    31           klass.send(:alias_method, :to_s, :to_formatted_s) 
     28 
     29        def self.included(base) #:nodoc: 
     30          base.class_eval do 
     31            alias_method :to_default_s, :to_s 
     32            alias_method :to_s, :to_formatted_s 
     33          end 
    3234        end 
    33          
     35 
    3436        def to_formatted_s(format = :default) 
    3537          case format 
  • trunk/activesupport/lib/active_support/core_ext/class/removal.rb

    r3668 r7474  
    1818      # Skip this class if it does not match the current one bound to this name 
    1919      next unless parent.const_defined?(basename) && klass = parent.const_get(basename) 
    20        
    21       parent.send :remove_const, basename unless parent == klass 
     20 
     21      parent.instance_eval { remove_const basename } unless parent == klass 
    2222    end 
    2323  end 
  • trunk/activesupport/lib/active_support/core_ext/date_time.rb

    r6303 r7474  
    44require "#{File.dirname(__FILE__)}/date_time/conversions" 
    55 
    6 DateTime.send(:include, ActiveSupport::CoreExtensions::Time::Behavior) 
    7 DateTime.send(:include, ActiveSupport::CoreExtensions::DateTime::Calculations) 
    8 DateTime.send(:include, ActiveSupport::CoreExtensions::DateTime::Conversions) 
     6class DateTime 
     7  include ActiveSupport::CoreExtensions::Time::Behavior 
     8  include ActiveSupport::CoreExtensions::DateTime::Calculations 
     9  include ActiveSupport::CoreExtensions::DateTime::Conversions 
     10end 
  • trunk/activesupport/lib/active_support/core_ext/date_time/conversions.rb

    r6933 r7474  
    44      # Getting datetimes in different convenient string representations and other objects 
    55      module Conversions 
    6         def self.included(klass) 
    7           klass.send(:alias_method, :to_datetime_default_s, :to_s) 
    8           klass.send(:alias_method, :to_s, :to_formatted_s) 
    9           klass.send(:alias_method, :default_inspect, :inspect) 
    10           klass.send(:alias_method, :inspect, :readable_inspect) 
     6        def self.included(base) 
     7          base.class_eval do 
     8            alias_method :to_datetime_default_s, :to_s 
     9            alias_method :to_s, :to_formatted_s 
     10            alias_method :default_inspect, :inspect 
     11            alias_method :inspect, :readable_inspect 
     12          end 
    1113        end 
    1214 
  • trunk/activesupport/lib/active_support/core_ext/date/calculations.rb

    r7262 r7474  
    55      module Calculations 
    66        def self.included(base) #:nodoc: 
    7           base.send(:extend, ClassMethods) 
    8            
    9           base.send(:alias_method, :plus_without_duration, :+) 
    10           base.send(:alias_method, :+, :plus_with_duration) 
    11            
    12           base.send(:alias_method, :minus_without_duration, :-) 
    13           base.send(:alias_method, :-, :minus_with_duration) 
     7          base.extend ClassMethods 
     8 
     9          base.instance_eval do 
     10            alias_method :plus_without_duration, :+ 
     11            alias_method :+, :plus_with_duration 
     12 
     13            alias_method :minus_without_duration, :- 
     14            alias_method :-, :minus_with_duration 
     15          end 
    1416        end 
    1517 
  • trunk/activesupport/lib/active_support/core_ext/date/conversions.rb

    r6933 r7474  
    1212        } 
    1313 
    14         def self.included(klass) #:nodoc: 
    15           klass.send(:alias_method, :to_default_s, :to_s) 
    16           klass.send(:alias_method, :to_s, :to_formatted_s) 
    17           klass.send(:alias_method, :default_inspect, :inspect) 
    18           klass.send(:alias_method, :inspect, :readable_inspect)           
     14        def self.included(base) #:nodoc: 
     15          base.instance_eval do 
     16            alias_method :to_default_s, :to_s 
     17            alias_method :to_s, :to_formatted_s 
     18            alias_method :default_inspect, :inspect 
     19            alias_method :inspect, :readable_inspect 
     20          end 
    1921        end 
    2022 
  • trunk/activesupport/lib/active_support/core_ext/enumerable.rb

    r5334 r7474  
    3939  def sum(identity = 0, &block) 
    4040    return identity unless size > 0 
     41 
    4142    if block_given? 
    4243      map(&block).sum 
  • trunk/activesupport/lib/active_support/core_ext/exception.rb

    r5546 r7474  
    99  def clean_backtrace 
    1010    backtrace.collect do |line| 
    11       Pathname.clean_within(TraceSubstitutions.inject(line) do |line, (regexp, sub)| 
    12         line.gsub regexp, sub 
     11      Pathname.clean_within(TraceSubstitutions.inject(line) do |result, (regexp, sub)| 
     12        result.gsub regexp, sub 
    1313      end) 
    1414    end 
  • trunk/activesupport/lib/active_support/core_ext/float/rounding.rb

    r7401 r7474  
    44      module Rounding 
    55        def self.included(base) #:nodoc: 
    6           base.send(:alias_method, :round_without_precision, :round) 
    7           base.send(:alias_method, :round, :round_with_precision) 
     6          base.class_eval do 
     7            alias_method :round_without_precision, :round 
     8            alias_method :round, :round_with_precision 
     9          end 
    810        end 
    911 
  • trunk/activesupport/lib/active_support/core_ext/name_error.rb

    r6044 r7474  
    11# Add a +missing_name+ method to NameError instances. 
    2 class NameError < StandardError #:nodoc:   
     2class NameError #:nodoc:   
    33  # Add a method to obtain the missing name from a NameError. 
    44  def missing_name 
  • trunk/activesupport/lib/active_support/core_ext/range/conversions.rb

    r2506 r7474  
    77          :db => Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" } 
    88        } 
    9          
    10         def self.included(klass) #:nodoc: 
    11           klass.send(:alias_method, :to_default_s, :to_s) 
    12           klass.send(:alias_method, :to_s, :to_formatted_s) 
     9 
     10        def self.included(base) #:nodoc: 
     11          base.class_eval do 
     12            alias_method :to_default_s, :to_s 
     13            alias_method :to_s, :to_formatted_s 
     14          end 
    1315        end 
    14          
     16 
    1517        def to_formatted_s(format = :default) 
    1618          DATE_FORMATS[format] ? DATE_FORMATS[format].call(first, last) : to_default_s    
  • trunk/activesupport/lib/active_support/core_ext/string.rb

    r5223 r7474  
    33require File.dirname(__FILE__) + '/string/access' 
    44require File.dirname(__FILE__) + '/string/starts_ends_with' 
    5 require File.dirname(__FILE__) + '/string/iterators' 
     5require File.dirname(__FILE__) + '/string/iterators' unless 'test'.respond_to?(:each_char) 
    66require File.dirname(__FILE__) + '/string/unicode' 
    77 
     
    1111  include ActiveSupport::CoreExtensions::String::Inflections 
    1212  include ActiveSupport::CoreExtensions::String::StartsEndsWith 
    13   include ActiveSupport::CoreExtensions::String::Iterators 
     13  if defined? ActiveSupport::CoreExtensions::String::Iterators 
     14    include ActiveSupport::CoreExtensions::String::Iterators 
     15  end 
    1416  include ActiveSupport::CoreExtensions::String::Unicode 
    1517end 
  • trunk/activesupport/lib/active_support/core_ext/symbol.rb

    r4455 r7474  
    1 class Symbol 
    2   # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples: 
    3   # 
    4   #   # The same as people.collect { |p| p.name } 
    5   #   people.collect(&:name) 
    6   # 
    7   #   # The same as people.select { |p| p.manager? }.collect { |p| p.salary } 
    8   #   people.select(&:manager?).collect(&:salary) 
    9   def to_proc 
    10     Proc.new { |*args| args.shift.__send__(self, *args) } 
     1unless :test.respond_to?(:to_proc) 
     2  class Symbol 
     3    # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples: 
     4    # 
     5    #   # The same as people.collect { |p| p.name } 
     6    #   people.collect(&:name) 
     7    # 
     8    #   # The same as people.select { |p| p.manager? }.collect { |p| p.salary } 
     9    #   people.select(&:manager?).collect(&:salary) 
     10    def to_proc 
     11      Proc.new { |*args| args.shift.__send__(self, *args) } 
     12    end 
    1113  end 
    1214end 
  • trunk/activesupport/lib/active_support/core_ext/time/calculations.rb

    r7262 r7474  
    55      module Calculations 
    66        def self.included(base) #:nodoc: 
    7           base.extend(ClassMethods) 
    8  
    9           base.send(:alias_method, :plus_without_duration, :+) 
    10           base.send(:alias_method, :+, :plus_with_duration) 
    11           base.send(:alias_method, :minus_without_duration, :-) 
    12           base.send(:alias_method, :-, :minus_with_duration) 
     7          base.extend ClassMethods 
     8 
     9          base.class_eval do 
     10            alias_method :plus_without_duration, :+ 
     11            alias_method :+, :plus_with_duration 
     12            alias_method :minus_without_duration, :- 
     13            alias_method :-, :minus_with_duration 
     14          end 
    1315        end 
    1416 
  • trunk/activesupport/lib/active_support/core_ext/time/conversions.rb

    r6902 r7474  
    1313        } 
    1414 
    15         def self.included(klass) 
    16           klass.send(:alias_method, :to_default_s, :to_s) 
    17           klass.send(:alias_method, :to_s, :to_formatted_s) 
     15        def self.included(base) 
     16          base.class_eval do 
     17            alias_method :to_default_s, :to_s 
     18            alias_method :to_s, :to_formatted_s 
     19          end 
    1820        end 
    1921 
  • trunk/activesupport/lib/active_support/dependencies.rb

    r6443 r7474  
    115115      /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ path 
    116116     
    117     names = path.split('::') 
     117    names = path.to_s.split('::') 
    118118    names.shift if names.first.empty? 
    119119     
     
    412412     
    413413    const = $1 if /\A::(.*)\Z/ =~ const.to_s 
    414     names = const.split('::') 
     414    names = const.to_s.split('::') 
    415415    if names.size == 1 # It's under Object 
    416416      parent = Object 
     
    420420     
    421421    log "removing constant #{const}" 
    422     parent.send :remove_const, names.last 
     422    parent.instance_eval { remove_const names.last } 
    423423    return true 
    424424  end 
     
    439439end 
    440440 
    441 Object.send(:define_method, :require_or_load)     { |file_name| Dependencies.require_or_load(file_name) } unless Object.respond_to?(:require_or_load) 
    442 Object.send(:define_method, :require_dependency)  { |file_name| Dependencies.depend_on(file_name) }       unless Object.respond_to?(:require_dependency) 
    443 Object.send(:define_method, :require_association) { |file_name| Dependencies.associate_with(file_name) }  unless Object.respond_to?(:require_association) 
     441Object.instance_eval do 
     442  define_method(:require_or_load)     { |file_name| Dependencies.require_or_load(file_name) } unless Object.respond_to?(:require_or_load) 
     443  define_method(:require_dependency)  { |file_name| Dependencies.depend_on(file_name) }       unless Object.respond_to?(:require_dependency) 
     444  define_method(:require_association) { |file_name| Dependencies.associate_with(file_name) }  unless Object.respond_to?(:require_association) 
     445end 
    444446 
    445447class Module #:nodoc: 
  • trunk/activesupport/lib/active_support/deprecation.rb

    r7220 r7474  
    148148    # warnings on any method call (except #inspect). 
    149149    class DeprecatedInstanceVariableProxy #:nodoc: 
    150       instance_methods.each { |m| undef_method m unless m =~ /^__/ } 
     150      silence_warnings do 
     151        instance_methods.each { |m| undef_method m unless m =~ /^__/ } 
     152      end 
    151153 
    152154      def initialize(instance, method, var = "@#{method}") 
  • trunk/activesupport/lib/active_support/inflector.rb

    r7092 r7474  
    271271    else 
    272272      case number.to_i % 10 
    273         when 1: "#{number}st" 
    274         when 2: "#{number}nd" 
    275         when 3: "#{number}rd" 
     273        when 1; "#{number}st" 
     274        when 2; "#{number}nd" 
     275        when 3; "#{number}rd" 
    276276        else    "#{number}th" 
    277277      end 
  • trunk/activesupport/lib/active_support/option_merger.rb

    r5197 r7474  
    22  class OptionMerger #:nodoc: 
    33    instance_methods.each do |method|  
    4       undef_method(method) if method !~ /^(__|instance_eval|class)/ 
     4      undef_method(method) if method !~ /^(__|instance_eval|class|object_id)/ 
    55    end 
    66     
  • trunk/activesupport/lib/active_support/vendor/builder/xmlbase.rb

    r4839 r7474  
    123123 
    124124    def _capture_outer_self(block) 
    125       @self = eval("self", block
     125      @self = eval('self', block.instance_eval { binding }
    126126    end 
    127      
     127 
    128128    def _newline 
    129129      return if @indent == 0 
    130130      text! "\n" 
    131131    end 
    132      
     132 
    133133    def _indent 
    134134      return if @indent == 0 || @level == 0 
    135135      text!(" " * (@level * @indent)) 
    136136    end 
    137      
     137 
    138138    def _nested_structures(block) 
    139139      @level += 1 
  • trunk/activesupport/test/core_ext/array_ext_test.rb

    r7217 r7474  
    180180    assert_equal 0, xml.rindex(/<\?xml /) 
    181181  end 
    182    
     182 
    183183  def test_to_xml_with_block 
    184184    xml = [ 
    185185      { :name => "David", :age => 26, :age_in_millis => 820497600000 }, 
    186186      { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') } 
    187     ].to_xml(:skip_instruct => true, :indent => 0) do |xml
    188       xml.count 2 
    189     end 
    190      
     187    ].to_xml(:skip_instruct => true, :indent => 0) do |builder
     188      builder.count 2 
     189    end 
     190 
    191191    assert xml.include?(%(<count>2</count>)), xml 
    192192  end 
  • trunk/activesupport/test/core_ext/enumerable_test.rb

    r4599 r7474  
    99  def test_group_by 
    1010    names = %w(marcel sam david jeremy) 
    11     klass = Class.new 
    12     klass.send(:attr_accessor, :name) 
     11    klass = Struct.new(:name) 
    1312    objects = (1..50).inject([]) do |people,| 
    1413      p = klass.new 
     
    3938 
    4039  def test_nil_sums 
    41     assert_raise(TypeError) { [5, 15, nil].sum } 
     40    expected_raise = RUBY_VERSION < '1.9.0' ? TypeError : NoMethodError 
     41 
     42    assert_raise(expected_raise) { [5, 15, nil].sum } 
    4243 
    4344    payments = [ Payment.new(5), Payment.new(15), Payment.new(10), Payment.new(nil) ] 
    44     assert_raise(TypeError) { payments.sum(&:price) } 
     45    assert_raise(expected_raise) { payments.sum(&:price) } 
     46 
    4547    assert_equal 60, payments.sum { |p| p.price.to_i * 2 } 
    4648  end 
  • trunk/activesupport/test/core_ext/file_test.rb

    r7412 r7474  
    77    File.atomic_write(file_name, Dir.pwd) do |file| 
    88      file.write(contents) 
    9       assert !File.exists?(file_name) 
     9      assert !File.exist?(file_name) 
    1010    end 
    11     assert File.exists?(file_name) 
     11    assert File.exist?(file_name) 
    1212    assert_equal contents, File.read(file_name) 
    1313  ensure 
     
    2121    end 
    2222  rescue 
    23     assert !File.exists?(file_name) 
     23    assert !File.exist?(file_name) 
    2424  end 
    2525