From 2e37effd7203cad84459661e11db2be44586cb4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 17 Oct 2009 12:54:03 -0300 Subject: Unify class_inheritable_accessor and extlib_inheritable_accessor and allow responder to be set in the class level. --- activesupport/lib/active_support/core_ext/object/duplicable.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'activesupport/lib/active_support/core_ext/object') diff --git a/activesupport/lib/active_support/core_ext/object/duplicable.rb b/activesupport/lib/active_support/core_ext/object/duplicable.rb index 1722726ca2..a2d4d50076 100644 --- a/activesupport/lib/active_support/core_ext/object/duplicable.rb +++ b/activesupport/lib/active_support/core_ext/object/duplicable.rb @@ -1,6 +1,6 @@ class Object # Can you safely .dup this object? - # False for nil, false, true, symbols, numbers, and class objects; true otherwise. + # False for nil, false, true, symbols, numbers, class and module objects; true otherwise. def duplicable? true end @@ -41,3 +41,9 @@ class Class #:nodoc: false end end + +class Module #:nodoc: + def duplicable? + false + end +end -- cgit v1.2.3 From b540eca5889d7a28fac39c9ec0df715aa89487ce Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 1 Nov 2009 11:06:47 +0100 Subject: Consolidate Object#to_param and #to_query core extensions --- .../active_support/core_ext/object/conversions.rb | 18 +-------- .../lib/active_support/core_ext/object/to_param.rb | 47 ++++++++++++++++++++++ .../lib/active_support/core_ext/object/to_query.rb | 27 +++++++++++++ 3 files changed, 76 insertions(+), 16 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/object/to_param.rb create mode 100644 activesupport/lib/active_support/core_ext/object/to_query.rb (limited to 'activesupport/lib/active_support/core_ext/object') 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 to_s. - def to_param - to_s - end - - # Converts an object into a string suitable for use as a URL query string, using the given key 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 to_s. + 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 to_param on all its elements and joins the result with + # slashes. This is used by url_for 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 namespace 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 key 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 -- cgit v1.2.3 From 103b29831e6d6a6c4363fd0e59ffcc6c343a14aa Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sat, 7 Nov 2009 11:23:34 -0800 Subject: Since we require 1.8.7 we don't need to shim instance_exec anymore --- .../active_support/core_ext/object/extending.rb | 27 ---------------------- 1 file changed, 27 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/object') diff --git a/activesupport/lib/active_support/core_ext/object/extending.rb b/activesupport/lib/active_support/core_ext/object/extending.rb index bbf6f8563b..0cc74c8298 100644 --- a/activesupport/lib/active_support/core_ext/object/extending.rb +++ b/activesupport/lib/active_support/core_ext/object/extending.rb @@ -50,31 +50,4 @@ class Object def extend_with_included_modules_from(object) #:nodoc: object.extended_by.each { |mod| extend mod } end - - unless defined? instance_exec # 1.9 - module InstanceExecMethods #:nodoc: - end - include InstanceExecMethods - - # Evaluate the block with the given arguments within the context of - # this object, so self is set to the method receiver. - # - # From Mauricio's http://eigenclass.org/hiki/bounded+space+instance_exec - def instance_exec(*args, &block) - begin - old_critical, Thread.critical = Thread.critical, true - n = 0 - n += 1 while respond_to?(method_name = "__instance_exec#{n}") - InstanceExecMethods.module_eval { define_method(method_name, &block) } - ensure - Thread.critical = old_critical - end - - begin - send(method_name, *args) - ensure - InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil - end - end - end end -- cgit v1.2.3 From d4513ac69958063de3cad9aa655fe9d63e82ec76 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 9 Nov 2009 20:26:46 +0100 Subject: Object#instance_variable_defined? is not needed for Ruby >= 1.8.7 --- .../lib/active_support/core_ext/object/instance_variables.rb | 7 ------- 1 file changed, 7 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/object') diff --git a/activesupport/lib/active_support/core_ext/object/instance_variables.rb b/activesupport/lib/active_support/core_ext/object/instance_variables.rb index 4ecaab3bbb..866317b17a 100644 --- a/activesupport/lib/active_support/core_ext/object/instance_variables.rb +++ b/activesupport/lib/active_support/core_ext/object/instance_variables.rb @@ -1,11 +1,4 @@ class Object - # Available in 1.8.6 and later. - unless respond_to?(:instance_variable_defined?) - def instance_variable_defined?(variable) - instance_variables.include?(variable.to_s) - end - end - # Returns a hash that maps instance variable names without "@" to their # corresponding values. Keys are strings both in Ruby 1.8 and 1.9. # -- cgit v1.2.3 From f8e713f488bba264ba73251b56ad56385b8ed824 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 9 Nov 2009 21:23:19 +0100 Subject: Object#tap is not needed for Ruby >= 1.8.7 --- activesupport/lib/active_support/core_ext/object/misc.rb | 1 - activesupport/lib/active_support/core_ext/object/tap.rb | 16 ---------------- 2 files changed, 17 deletions(-) delete mode 100644 activesupport/lib/active_support/core_ext/object/tap.rb (limited to 'activesupport/lib/active_support/core_ext/object') diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb index 80011dfbed..3e3af03cc5 100644 --- a/activesupport/lib/active_support/core_ext/object/misc.rb +++ b/activesupport/lib/active_support/core_ext/object/misc.rb @@ -1,3 +1,2 @@ require 'active_support/core_ext/object/returning' -require 'active_support/core_ext/object/tap' require 'active_support/core_ext/object/with_options' diff --git a/activesupport/lib/active_support/core_ext/object/tap.rb b/activesupport/lib/active_support/core_ext/object/tap.rb deleted file mode 100644 index db7e715e2d..0000000000 --- a/activesupport/lib/active_support/core_ext/object/tap.rb +++ /dev/null @@ -1,16 +0,0 @@ -class Object - # Yields x to the block, and then returns x. - # The primary purpose of this method is to "tap into" a method chain, - # in order to perform operations on intermediate results within the chain. - # - # (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a. - # tap { |x| puts "array: #{x.inspect}" }. - # select { |x| x%2 == 0 }. - # tap { |x| puts "evens: #{x.inspect}" }. - # map { |x| x*x }. - # tap { |x| puts "squares: #{x.inspect}" } - def tap - yield self - self - end unless Object.respond_to?(:tap) -end -- cgit v1.2.3