Skip to content

Commit

Permalink
Initial pass at removing dead 1.8.x code from Active Support.
Browse files Browse the repository at this point in the history
There are a bunch of other implicit branches that adds
1.8.x specific code that still needs to be removed. Pull
requests for those cases are welcome.
  • Loading branch information
josevalim committed Dec 20, 2011
1 parent 51095be commit 7ab4775
Show file tree
Hide file tree
Showing 27 changed files with 118 additions and 760 deletions.
18 changes: 0 additions & 18 deletions activesupport/lib/active_support/core_ext/date/calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,6 @@
class Date
DAYS_INTO_WEEK = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 }

if RUBY_VERSION < '1.9'
undef :>>

# Backported from 1.9. The one in 1.8 leads to incorrect next_month and
# friends for dates where the calendar reform is involved. It additionally
# prevents an infinite loop fixed in r27013.
def >>(n)
y, m = (year * 12 + (mon - 1) + n).divmod(12)
m, = (m + 1) .divmod(1)
d = mday
until jd2 = self.class.valid_civil?(y, m, d, start)
d -= 1
raise ArgumentError, 'invalid date' unless d > 0
end
self + (jd2 - jd)
end
end

class << self
# Returns a new Date representing the date 1 day ago (i.e. yesterday's date).
def yesterday
Expand Down
23 changes: 0 additions & 23 deletions activesupport/lib/active_support/core_ext/date/conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ def readable_inspect
alias_method :default_inspect, :inspect
alias_method :inspect, :readable_inspect

# A method to keep Time, Date and DateTime instances interchangeable on conversions.
# In this case, it simply returns +self+.
def to_date
self
end if RUBY_VERSION < '1.9'

# Converts a Date instance to a Time, where the time is set to the beginning of the day.
# The timezone can be either :local or :utc (default :local).
#
Expand All @@ -83,23 +77,6 @@ def to_time(form = :local)
::Time.send("#{form}_time", year, month, day)
end

# Converts a Date instance to a DateTime, where the time is set to the beginning of the day
# and UTC offset is set to 0.
#
# ==== Examples
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
#
# date.to_datetime # => Sat, 10 Nov 2007 00:00:00 0000
def to_datetime
::DateTime.civil(year, month, day, 0, 0, 0, 0)
end if RUBY_VERSION < '1.9'

def iso8601
strftime('%F')
end if RUBY_VERSION < '1.9'

alias_method :rfc3339, :iso8601 if RUBY_VERSION < '1.9'

def xmlschema
to_time_in_current_zone.xmlschema
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'rational' unless RUBY_VERSION >= '1.9.2'

class DateTime
class << self
# DateTimes aren't aware of DST rules, so use a consistent non-DST offset when creating a DateTime with an offset in the local zone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def to_date
# Attempts to convert self to a Ruby Time object; returns self if out of range of Ruby Time class.
# If self has an offset other than 0, self will just be returned unaltered, since there's no clean way to map it to a Time.
def to_time
self.offset == 0 ? ::Time.utc_time(year, month, day, hour, min, sec, sec_fraction * (RUBY_VERSION < '1.9' ? 86400000000 : 1000000)) : self
self.offset == 0 ? ::Time.utc_time(year, month, day, hour, min, sec, sec_fraction * 1000000) : self
end

# To be able to keep Times, Dates and DateTimes interchangeable on conversions.
Expand Down
37 changes: 0 additions & 37 deletions activesupport/lib/active_support/core_ext/enumerable.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,4 @@
require 'active_support/ordered_hash'

module Enumerable
# Ruby 1.8.7 introduces group_by, but the result isn't ordered. Override it.
remove_method(:group_by) if [].respond_to?(:group_by) && RUBY_VERSION < '1.9'

# Collect an enumerable into sets, grouped by the result of a block. Useful,
# for example, for grouping records by date.
#
# Example:
#
# latest_transcripts.group_by(&:day).each do |day, transcripts|
# p "#{day} -> #{transcripts.map(&:class).join(', ')}"
# end
# "2006-03-01 -> Transcript"
# "2006-02-28 -> Transcript"
# "2006-02-27 -> Transcript, Transcript"
# "2006-02-26 -> Transcript, Transcript"
# "2006-02-25 -> Transcript"
# "2006-02-24 -> Transcript, Transcript"
# "2006-02-23 -> Transcript"
def group_by
return to_enum :group_by unless block_given?
assoc = ActiveSupport::OrderedHash.new

each do |element|
key = yield(element)

if assoc.has_key?(key)
assoc[key] << element
else
assoc[key] = [element]
end
end

assoc
end unless [].respond_to?(:group_by)

# Calculates a sum from the elements. Examples:
#
# payments.sum { |p| p.price * p.tax_rate }
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/core_ext/exception.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ActiveSupport
FrozenObjectError = RUBY_VERSION < '1.9' ? TypeError : RuntimeError
FrozenObjectError = RuntimeError
end
23 changes: 2 additions & 21 deletions activesupport/lib/active_support/core_ext/module/introspection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,8 @@ def parents
parents
end

if RUBY_VERSION < '1.9'
# Returns the constants that have been defined locally by this object and
# not in an ancestor. This method is exact if running under Ruby 1.9. In
# previous versions it may miss some constants if their definition in some
# ancestor is identical to their definition in the receiver.
def local_constants
inherited = {}

ancestors.each do |anc|
next if anc == self
anc.constants.each { |const| inherited[const] = anc.const_get(const) }
end

constants.select do |const|
!inherited.key?(const) || inherited[const].object_id != const_get(const).object_id
end
end
else
def local_constants #:nodoc:
constants(false)
end
def local_constants #:nodoc:
constants(false)
end

# Returns the names of the constants defined locally rather than the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ def instance_values #:nodoc:
# end
#
# C.new(0, 1).instance_variable_names # => ["@y", "@x"]
if RUBY_VERSION >= '1.9'
def instance_variable_names
instance_variables.map { |var| var.to_s }
end
else
alias_method :instance_variable_names, :instance_variables
def instance_variable_names
instance_variables.map { |var| var.to_s }
end
end
1 change: 0 additions & 1 deletion activesupport/lib/active_support/core_ext/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
require 'active_support/core_ext/range/conversions'
require 'active_support/core_ext/range/include_range'
require 'active_support/core_ext/range/overlaps'
require 'active_support/core_ext/range/cover'
3 changes: 0 additions & 3 deletions activesupport/lib/active_support/core_ext/range/cover.rb

This file was deleted.

10 changes: 2 additions & 8 deletions activesupport/lib/active_support/core_ext/string/encoding.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
class String
if defined?(Encoding) && "".respond_to?(:encode)
def encoding_aware?
true
end
else
def encoding_aware?
false
end
def encoding_aware?
true
end
end
110 changes: 47 additions & 63 deletions activesupport/lib/active_support/core_ext/string/multibyte.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,55 @@
require 'active_support/multibyte'

class String
if RUBY_VERSION >= "1.9"
# == Multibyte proxy
#
# +mb_chars+ is a multibyte safe proxy for string methods.
#
# In Ruby 1.8 and older it creates and returns an instance of the ActiveSupport::Multibyte::Chars class which
# encapsulates the original string. A Unicode safe version of all the String methods are defined on this proxy
# class. If the proxy class doesn't respond to a certain method, it's forwarded to the encapsulated string.
#
# name = 'Claus Müller'
# name.reverse # => "rell??M sualC"
# name.length # => 13
#
# name.mb_chars.reverse.to_s # => "rellüM sualC"
# name.mb_chars.length # => 12
#
# In Ruby 1.9 and newer +mb_chars+ returns +self+ because String is (mostly) encoding aware. This means that
# it becomes easy to run one version of your code on multiple Ruby versions.
#
# == Method chaining
#
# All the methods on the Chars proxy which normally return a string will return a Chars object. This allows
# method chaining on the result of any of these methods.
#
# name.mb_chars.reverse.length # => 12
#
# == Interoperability and configuration
#
# The Chars object tries to be as interchangeable with String objects as possible: sorting and comparing between
# String and Char work like expected. The bang! methods change the internal string representation in the Chars
# object. Interoperability problems can be resolved easily with a +to_s+ call.
#
# For more information about the methods defined on the Chars proxy see ActiveSupport::Multibyte::Chars. For
# information about how to change the default Multibyte behavior see ActiveSupport::Multibyte.
def mb_chars
if ActiveSupport::Multibyte.proxy_class.consumes?(self)
ActiveSupport::Multibyte.proxy_class.new(self)
else
self
end
end

def is_utf8?
case encoding
when Encoding::UTF_8
valid_encoding?
when Encoding::ASCII_8BIT, Encoding::US_ASCII
dup.force_encoding(Encoding::UTF_8).valid_encoding?
else
false
end
end
else
def mb_chars
if ActiveSupport::Multibyte.proxy_class.wants?(self)
ActiveSupport::Multibyte.proxy_class.new(self)
else
self
end
# == Multibyte proxy
#
# +mb_chars+ is a multibyte safe proxy for string methods.
#
# In Ruby 1.8 and older it creates and returns an instance of the ActiveSupport::Multibyte::Chars class which
# encapsulates the original string. A Unicode safe version of all the String methods are defined on this proxy
# class. If the proxy class doesn't respond to a certain method, it's forwarded to the encapsulated string.
#
# name = 'Claus Müller'
# name.reverse # => "rell??M sualC"
# name.length # => 13
#
# name.mb_chars.reverse.to_s # => "rellüM sualC"
# name.mb_chars.length # => 12
#
# In Ruby 1.9 and newer +mb_chars+ returns +self+ because String is (mostly) encoding aware. This means that
# it becomes easy to run one version of your code on multiple Ruby versions.
#
# == Method chaining
#
# All the methods on the Chars proxy which normally return a string will return a Chars object. This allows
# method chaining on the result of any of these methods.
#
# name.mb_chars.reverse.length # => 12
#
# == Interoperability and configuration
#
# The Chars object tries to be as interchangeable with String objects as possible: sorting and comparing between
# String and Char work like expected. The bang! methods change the internal string representation in the Chars
# object. Interoperability problems can be resolved easily with a +to_s+ call.
#
# For more information about the methods defined on the Chars proxy see ActiveSupport::Multibyte::Chars. For
# information about how to change the default Multibyte behavior see ActiveSupport::Multibyte.
def mb_chars
if ActiveSupport::Multibyte.proxy_class.consumes?(self)
ActiveSupport::Multibyte.proxy_class.new(self)
else
self
end
end

# Returns true if the string has UTF-8 semantics (a String used for purely byte resources is unlikely to have
# them), returns false otherwise.
def is_utf8?
ActiveSupport::Multibyte::Chars.consumes?(self)
def is_utf8?
case encoding
when Encoding::UTF_8
valid_encoding?
when Encoding::ASCII_8BIT, Encoding::US_ASCII
dup.force_encoding(Encoding::UTF_8).valid_encoding?
else
false
end
end
end
28 changes: 12 additions & 16 deletions activesupport/lib/active_support/core_ext/uri.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
# encoding: utf-8

if RUBY_VERSION >= '1.9'
require 'uri'
require 'uri'
str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese.
parser = URI::Parser.new

str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese.

parser = URI::Parser.new

unless str == parser.unescape(parser.escape(str))
URI::Parser.class_eval do
remove_method :unescape
def unescape(str, escaped = /%[a-fA-F\d]{2}/)
# TODO: Are we actually sure that ASCII == UTF-8?
# YK: My initial experiments say yes, but let's be sure please
enc = str.encoding
enc = Encoding::UTF_8 if enc == Encoding::US_ASCII
str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(enc)
end
unless str == parser.unescape(parser.escape(str))
URI::Parser.class_eval do
remove_method :unescape
def unescape(str, escaped = /%[a-fA-F\d]{2}/)
# TODO: Are we actually sure that ASCII == UTF-8?
# YK: My initial experiments say yes, but let's be sure please
enc = str.encoding
enc = Encoding::UTF_8 if enc == Encoding::US_ASCII
str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(enc)
end
end
end
Expand Down
Loading

0 comments on commit 7ab4775

Please sign in to comment.