diff options
Diffstat (limited to 'activesupport/lib/active_support')
10 files changed, 77 insertions, 73 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 818983fdd6..b91ae65e9f 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -2,12 +2,7 @@ require 'benchmark' require 'active_support/core_ext/benchmark' require 'active_support/core_ext/exception' require 'active_support/core_ext/class/attribute_accessors' - -%w(hash nil string time date date_time array big_decimal range object boolean).each do |library| - require "active_support/core_ext/#{library}/conversions" -end - -# require 'active_support/core_ext' # FIXME: pulling in all to_param extensions +require 'active_support/core_ext/object/to_param' module ActiveSupport # See ActiveSupport::Cache::Store for documentation. diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index c53cf3f530..db140225e8 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -40,22 +40,6 @@ class Array end end - - # Calls <tt>to_param</tt> on all its elements and joins the result with - # slashes. This is used by <tt>url_for</tt> in Action Pack. - def to_param - collect { |e| e.to_param }.join '/' - end - - # Converts an array into a string suitable for use as a URL query string, - # using the given +key+ as the param name. - # - # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding" - def to_query(key) - prefix = "#{key}[]" - collect { |value| value.to_query(prefix) }.join '&' - end - # Converts a collection of elements into a formatted string by calling # <tt>to_s</tt> on all elements and joining them: # diff --git a/activesupport/lib/active_support/core_ext/boolean.rb b/activesupport/lib/active_support/core_ext/boolean.rb deleted file mode 100644 index be834288f2..0000000000 --- a/activesupport/lib/active_support/core_ext/boolean.rb +++ /dev/null @@ -1 +0,0 @@ -require 'active_support/core_ext/boolean/conversions'
\ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/boolean/conversions.rb b/activesupport/lib/active_support/core_ext/boolean/conversions.rb deleted file mode 100644 index 534ebb7118..0000000000 --- a/activesupport/lib/active_support/core_ext/boolean/conversions.rb +++ /dev/null @@ -1,11 +0,0 @@ -class TrueClass - def to_param - self - end -end - -class FalseClass - def to_param - self - end -end diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index bd9419e1a2..190173f8a0 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -1,6 +1,4 @@ require 'active_support/time' -require 'active_support/core_ext/object/conversions' -require 'active_support/core_ext/array/conversions' require 'active_support/core_ext/hash/reverse_merge' class Hash @@ -68,21 +66,6 @@ class Hash ) end - # Converts a hash into a string suitable for use as a URL query string. An optional <tt>namespace</tt> can be - # passed to enclose the param names (see example below). - # - # ==== Examples - # { :name => 'David', :nationality => 'Danish' }.to_query # => "name=David&nationality=Danish" - # - # { :name => 'David', :nationality => 'Danish' }.to_query('user') # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish" - def to_query(namespace = nil) - collect do |key, value| - value.to_query(namespace ? "#{namespace}[#{key}]" : key) - end.sort * '&' - end - - alias_method :to_param, :to_query - def to_xml(options = {}) require 'builder' unless defined?(Builder) diff --git a/activesupport/lib/active_support/core_ext/nil.rb b/activesupport/lib/active_support/core_ext/nil.rb deleted file mode 100644 index e9f63c4802..0000000000 --- a/activesupport/lib/active_support/core_ext/nil.rb +++ /dev/null @@ -1 +0,0 @@ -require 'active_support/core_ext/nil/conversions'
\ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/nil/conversions.rb b/activesupport/lib/active_support/core_ext/nil/conversions.rb deleted file mode 100644 index 6ceb500a2a..0000000000 --- a/activesupport/lib/active_support/core_ext/nil/conversions.rb +++ /dev/null @@ -1,5 +0,0 @@ -class NilClass - def to_param - self - end -end diff --git a/activesupport/lib/active_support/core_ext/object/conversions.rb b/activesupport/lib/active_support/core_ext/object/conversions.rb index 638f0decc1..540f7aadb0 100644 --- a/activesupport/lib/active_support/core_ext/object/conversions.rb +++ b/activesupport/lib/active_support/core_ext/object/conversions.rb @@ -1,18 +1,4 @@ +require 'active_support/core_ext/object/to_param' +require 'active_support/core_ext/object/to_query' require 'active_support/core_ext/array/conversions' require 'active_support/core_ext/hash/conversions' - -class Object - # Alias of <tt>to_s</tt>. - def to_param - to_s - end - - # Converts an object into a string suitable for use as a URL query string, using the given <tt>key</tt> as the - # param name. - # - # 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 diff --git a/activesupport/lib/active_support/core_ext/object/to_param.rb b/activesupport/lib/active_support/core_ext/object/to_param.rb new file mode 100644 index 0000000000..a5e2260791 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/to_param.rb @@ -0,0 +1,47 @@ +class Object + # Alias of <tt>to_s</tt>. + def to_param + to_s + end +end + +class NilClass + def to_param + self + end +end + +class TrueClass + def to_param + self + end +end + +class FalseClass + def to_param + self + end +end + +class Array + # Calls <tt>to_param</tt> on all its elements and joins the result with + # slashes. This is used by <tt>url_for</tt> in Action Pack. + def to_param + collect { |e| e.to_param }.join '/' + end +end + +class Hash + # Converts a hash into a string suitable for use as a URL query string. An optional <tt>namespace</tt> can be + # passed to enclose the param names (see example below). + # + # ==== Examples + # { :name => 'David', :nationality => 'Danish' }.to_query # => "name=David&nationality=Danish" + # + # { :name => 'David', :nationality => 'Danish' }.to_query('user') # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish" + def to_param(namespace = nil) + collect do |key, value| + value.to_query(namespace ? "#{namespace}[#{key}]" : key) + end.sort * '&' + end +end diff --git a/activesupport/lib/active_support/core_ext/object/to_query.rb b/activesupport/lib/active_support/core_ext/object/to_query.rb new file mode 100644 index 0000000000..3f1540f685 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/to_query.rb @@ -0,0 +1,27 @@ +require 'active_support/core_ext/object/to_param' + +class Object + # Converts an object into a string suitable for use as a URL query string, using the given <tt>key</tt> as the + # param name. + # + # 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 + +class Array + # Converts an array into a string suitable for use as a URL query string, + # using the given +key+ as the param name. + # + # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding" + def to_query(key) + prefix = "#{key}[]" + collect { |value| value.to_query(prefix) }.join '&' + end +end + +class Hash + alias_method :to_query, :to_param +end |