aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
diff options
context:
space:
mode:
authorHongli Lai (Phusion) <hongli@phusion.nl>2008-12-03 19:30:35 +0100
committerHongli Lai (Phusion) <hongli@phusion.nl>2008-12-03 19:30:35 +0100
commitccb96f2297e8783165cba764e9b5d51e1a15ff87 (patch)
tree3229e6fdddc42054615514d843c555e341003033 /activesupport/lib/active_support/core_ext
parentfb2325e35855d62abd2c76ce03feaa3ca7992e4f (diff)
parent761a633a9c0a45d76ef3ed10da97e3696c3ded79 (diff)
downloadrails-ccb96f2297e8783165cba764e9b5d51e1a15ff87.tar.gz
rails-ccb96f2297e8783165cba764e9b5d51e1a15ff87.tar.bz2
rails-ccb96f2297e8783165cba764e9b5d51e1a15ff87.zip
Merge commit 'origin/master' into savepoints
Conflicts: activerecord/lib/active_record/fixtures.rb activerecord/test/cases/defaults_test.rb
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r--activesupport/lib/active_support/core_ext/array/access.rb26
-rw-r--r--activesupport/lib/active_support/core_ext/array/conversions.rb3
-rw-r--r--activesupport/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb19
-rw-r--r--activesupport/lib/active_support/core_ext/enumerable.rb9
-rw-r--r--activesupport/lib/active_support/core_ext/exception.rb1
-rw-r--r--activesupport/lib/active_support/core_ext/file/atomic.rb4
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb61
-rw-r--r--activesupport/lib/active_support/core_ext/module/delegation.rb4
-rw-r--r--activesupport/lib/active_support/core_ext/object/conversions.rb3
-rw-r--r--activesupport/lib/active_support/core_ext/object/misc.rb18
-rw-r--r--activesupport/lib/active_support/core_ext/rexml.rb51
-rw-r--r--activesupport/lib/active_support/core_ext/string/iterators.rb4
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb2
13 files changed, 103 insertions, 102 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/access.rb b/activesupport/lib/active_support/core_ext/array/access.rb
index a3b2a54c7d..6de338bfcc 100644
--- a/activesupport/lib/active_support/core_ext/array/access.rb
+++ b/activesupport/lib/active_support/core_ext/array/access.rb
@@ -43,29 +43,9 @@ module ActiveSupport #:nodoc:
self[4]
end
- # Equal to <tt>self[5]</tt>.
- def sixth
- self[5]
- end
-
- # Equal to <tt>self[6]</tt>.
- def seventh
- self[6]
- end
-
- # Equal to <tt>self[7]</tt>.
- def eighth
- self[7]
- end
-
- # Equal to <tt>self[8]</tt>.
- def ninth
- self[8]
- end
-
- # Equal to <tt>self[9]</tt>.
- def tenth
- self[9]
+ # Equal to <tt>self[41]</tt>. Also known as accessing "the reddit".
+ def forty_two
+ self[41]
end
end
end
diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb
index cf3e03f62c..f0d6591135 100644
--- a/activesupport/lib/active_support/core_ext/array/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/array/conversions.rb
@@ -1,5 +1,3 @@
-require 'builder'
-
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Array #:nodoc:
@@ -149,6 +147,7 @@ module ActiveSupport #:nodoc:
#
def to_xml(options = {})
raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml }
+ require 'builder' unless defined?(Builder)
options[:root] ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records"
options[:children] ||= options[:root].singularize
diff --git a/activesupport/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb b/activesupport/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb
index a21e98fa80..1edb3771a2 100644
--- a/activesupport/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb
+++ b/activesupport/lib/active_support/core_ext/cgi/escape_skipping_slashes.rb
@@ -2,11 +2,20 @@ module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module CGI #:nodoc:
module EscapeSkippingSlashes #:nodoc:
- def escape_skipping_slashes(str)
- str = str.join('/') if str.respond_to? :join
- str.gsub(/([^ \/a-zA-Z0-9_.-])/n) do
- "%#{$1.unpack('H2').first.upcase}"
- end.tr(' ', '+')
+ if RUBY_VERSION >= '1.9'
+ def escape_skipping_slashes(str)
+ str = str.join('/') if str.respond_to? :join
+ str.gsub(/([^ \/a-zA-Z0-9_.-])/n) do
+ "%#{$1.unpack('H2' * $1.bytesize).join('%').upcase}"
+ end.tr(' ', '+')
+ end
+ else
+ def escape_skipping_slashes(str)
+ str = str.join('/') if str.respond_to? :join
+ str.gsub(/([^ \/a-zA-Z0-9_.-])/n) do
+ "%#{$1.unpack('H2').first.upcase}"
+ end.tr(' ', '+')
+ end
end
end
end
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 788f3a7e9e..a7eaccfed7 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -104,4 +104,13 @@ module Enumerable
size = block_given? ? select(&block).size : self.size
size > 1
end
+
+ # Returns true if none of the elements match the given block.
+ #
+ # success = responses.none? {|r| r.status / 100 == 5 }
+ #
+ # This is a builtin method in Ruby 1.8.7 and later.
+ def none?(&block)
+ !any?(&block)
+ end unless [].respond_to?(:none?)
end
diff --git a/activesupport/lib/active_support/core_ext/exception.rb b/activesupport/lib/active_support/core_ext/exception.rb
index 57c8568334..73470cbe05 100644
--- a/activesupport/lib/active_support/core_ext/exception.rb
+++ b/activesupport/lib/active_support/core_ext/exception.rb
@@ -6,6 +6,7 @@ module ActiveSupport
end
end
+# TODO: Turn all this into using the BacktraceCleaner.
class Exception # :nodoc:
def clean_message
Pathname.clean_within message
diff --git a/activesupport/lib/active_support/core_ext/file/atomic.rb b/activesupport/lib/active_support/core_ext/file/atomic.rb
index f988eff3d9..976d462e8e 100644
--- a/activesupport/lib/active_support/core_ext/file/atomic.rb
+++ b/activesupport/lib/active_support/core_ext/file/atomic.rb
@@ -1,5 +1,3 @@
-require 'tempfile'
-
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module File #:nodoc:
@@ -18,6 +16,8 @@ module ActiveSupport #:nodoc:
# file.write("hello")
# end
def atomic_write(file_name, temp_dir = Dir.tmpdir)
+ require 'tempfile' unless defined?(Tempfile)
+
temp_file = Tempfile.new(basename(file_name), temp_dir)
yield temp_file
temp_file.close
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
index 50dc7c61fc..437b44c51c 100644
--- a/activesupport/lib/active_support/core_ext/hash/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -1,47 +1,22 @@
require 'date'
-require 'cgi'
-require 'builder'
-require 'xmlsimple'
-
-# Locked down XmlSimple#xml_in_string
-class XmlSimple
- # Same as xml_in but doesn't try to smartly shoot itself in the foot.
- def xml_in_string(string, options = nil)
- handle_options('in', options)
-
- @doc = parse(string)
- result = collapse(@doc.root)
-
- if @options['keeproot']
- merge({}, @doc.root.name, result)
- else
- result
- end
- end
-
- def self.xml_in_string(string, options = nil)
- new.xml_in_string(string, options)
- end
-end
-
-# This module exists to decorate files deserialized using Hash.from_xml with
-# the <tt>original_filename</tt> and <tt>content_type</tt> methods.
-module FileLike #:nodoc:
- attr_writer :original_filename, :content_type
-
- def original_filename
- @original_filename || 'untitled'
- end
-
- def content_type
- @content_type || 'application/octet-stream'
- end
-end
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Hash #:nodoc:
module Conversions
+ # This module exists to decorate files deserialized using Hash.from_xml with
+ # the <tt>original_filename</tt> and <tt>content_type</tt> methods.
+ module FileLike #:nodoc:
+ attr_writer :original_filename, :content_type
+
+ def original_filename
+ @original_filename || 'untitled'
+ end
+
+ def content_type
+ @content_type || 'application/octet-stream'
+ end
+ end
XML_TYPE_NAMES = {
"Symbol" => "symbol",
@@ -113,6 +88,8 @@ module ActiveSupport #:nodoc:
alias_method :to_param, :to_query
def to_xml(options = {})
+ require 'builder' unless defined?(Builder)
+
options[:indent] ||= 2
options.reverse_merge!({ :builder => Builder::XmlMarkup.new(:indent => options[:indent]),
:root => "hash" })
@@ -167,13 +144,7 @@ module ActiveSupport #:nodoc:
module ClassMethods
def from_xml(xml)
- # TODO: Refactor this into something much cleaner that doesn't rely on XmlSimple
- typecast_xml_value(undasherize_keys(XmlSimple.xml_in_string(xml,
- 'forcearray' => false,
- 'forcecontent' => true,
- 'keeproot' => true,
- 'contentkey' => '__content__')
- ))
+ typecast_xml_value(undasherize_keys(XmlMini.parse(xml)))
end
private
diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb
index 1ad18402e5..2905eebc85 100644
--- a/activesupport/lib/active_support/core_ext/module/delegation.rb
+++ b/activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -78,6 +78,10 @@ class Module
raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
end
+ if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
+ raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
+ end
+
prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_"
methods.each do |method|
diff --git a/activesupport/lib/active_support/core_ext/object/conversions.rb b/activesupport/lib/active_support/core_ext/object/conversions.rb
index ad752f0fc5..278b856c45 100644
--- a/activesupport/lib/active_support/core_ext/object/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/object/conversions.rb
@@ -9,6 +9,7 @@ class Object
#
# Note: This method is defined as a default implementation for all Objects for Hash#to_query to work.
def to_query(key)
+ require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
"#{CGI.escape(key.to_s)}=#{CGI.escape(to_param.to_s)}"
end
-end \ No newline at end of file
+end
diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb
index cd0a04d32a..46f9c7d676 100644
--- a/activesupport/lib/active_support/core_ext/object/misc.rb
+++ b/activesupport/lib/active_support/core_ext/object/misc.rb
@@ -71,4 +71,22 @@ class Object
def acts_like?(duck)
respond_to? "acts_like_#{duck}?"
end
+
+ # Tries to send the method only if object responds to it. Return +nil+ otherwise.
+ # It will also forward any arguments and/or block like Object#send does.
+ #
+ # ==== Example :
+ #
+ # # Without try
+ # @person ? @person.name : nil
+ #
+ # With try
+ # @person.try(:name)
+ #
+ # # try also accepts arguments/blocks for the method it is trying
+ # Person.try(:find, 1)
+ # @people.try(:map) {|p| p.name}
+ def try(method, *args, &block)
+ send(method, *args, &block) if respond_to?(method, true)
+ end
end
diff --git a/activesupport/lib/active_support/core_ext/rexml.rb b/activesupport/lib/active_support/core_ext/rexml.rb
index 187f3e0f5e..b4891a9153 100644
--- a/activesupport/lib/active_support/core_ext/rexml.rb
+++ b/activesupport/lib/active_support/core_ext/rexml.rb
@@ -1,34 +1,39 @@
-require 'rexml/document'
-require 'rexml/entity'
-
# Fixes the rexml vulnerability disclosed at:
# http://www.ruby-lang.org/en/news/2008/08/23/dos-vulnerability-in-rexml/
# This fix is identical to rexml-expansion-fix version 1.0.1
+require 'rexml/rexml'
# Earlier versions of rexml defined REXML::Version, newer ones REXML::VERSION
unless (defined?(REXML::VERSION) ? REXML::VERSION : REXML::Version) > "3.1.7.2"
- module REXML
- class Entity < Child
- undef_method :unnormalized
- def unnormalized
- document.record_entity_expansion! if document
- v = value()
- return nil if v.nil?
- @unnormalized = Text::unnormalize(v, parent)
- @unnormalized
- end
- end
- class Document < Element
- @@entity_expansion_limit = 10_000
- def self.entity_expansion_limit= val
- @@entity_expansion_limit = val
+ require 'rexml/document'
+
+ # REXML in 1.8.7 has the patch but didn't update Version from 3.1.7.2.
+ unless REXML::Document.respond_to?(:entity_expansion_limit=)
+ require 'rexml/entity'
+
+ module REXML
+ class Entity < Child
+ undef_method :unnormalized
+ def unnormalized
+ document.record_entity_expansion! if document
+ v = value()
+ return nil if v.nil?
+ @unnormalized = Text::unnormalize(v, parent)
+ @unnormalized
+ end
end
+ class Document < Element
+ @@entity_expansion_limit = 10_000
+ def self.entity_expansion_limit= val
+ @@entity_expansion_limit = val
+ end
- def record_entity_expansion!
- @number_of_expansions ||= 0
- @number_of_expansions += 1
- if @number_of_expansions > @@entity_expansion_limit
- raise "Number of entity expansions exceeded, processing aborted."
+ def record_entity_expansion!
+ @number_of_expansions ||= 0
+ @number_of_expansions += 1
+ if @number_of_expansions > @@entity_expansion_limit
+ raise "Number of entity expansions exceeded, processing aborted."
+ end
end
end
end
diff --git a/activesupport/lib/active_support/core_ext/string/iterators.rb b/activesupport/lib/active_support/core_ext/string/iterators.rb
index 66a08a5cd0..fe17d140ca 100644
--- a/activesupport/lib/active_support/core_ext/string/iterators.rb
+++ b/activesupport/lib/active_support/core_ext/string/iterators.rb
@@ -13,7 +13,9 @@ module ActiveSupport #:nodoc:
# When $KCODE = 'UTF8', multi-byte characters are yielded appropriately.
def each_char
scanner, char = StringScanner.new(self), /./mu
- loop { yield(scanner.scan(char) || break) }
+ while c = scanner.scan(char)
+ yield c
+ end
end
end
end
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index 00078de692..5ed750afcc 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -1,3 +1,5 @@
+require 'active_support/duration'
+
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Time #:nodoc: