From 4163ccec2343ee66e2488f067eab2a15260e1219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 22 Apr 2010 12:00:13 +0200 Subject: Clean up the config object in ActionPack. Create config_accessor which just delegates to the config object, reducing the number of deprecations and add specific tests. --- activesupport/lib/active_support/configurable.rb | 37 ++++++++++++------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/configurable.rb b/activesupport/lib/active_support/configurable.rb index 890f465ce1..f562e17c75 100644 --- a/activesupport/lib/active_support/configurable.rb +++ b/activesupport/lib/active_support/configurable.rb @@ -1,35 +1,36 @@ -require "active_support/concern" +require 'active_support/concern' +require 'active_support/ordered_options' +require 'active_support/core_ext/kernel/singleton_class' +require 'active_support/core_ext/module/delegation' module ActiveSupport module Configurable extend ActiveSupport::Concern module ClassMethods - def get_config - module_parts = name.split("::") - modules = [Object] - module_parts.each {|name| modules.push modules.last.const_get(name) } - modules.reverse_each do |mod| - return mod.const_get(:DEFAULT_CONFIG) if const_defined?(:DEFAULT_CONFIG) - end - {} - end - def config - self.config = get_config unless @config - @config + @config ||= ActiveSupport::InheritableOptions.new(superclass.respond_to?(:config) ? superclass.config : {}) end - def config=(hash) - @config = ActiveSupport::OrderedOptions.new - hash.each do |key, value| - @config[key] = value + def configure + yield config + end + + def config_accessor(*names) + names.each do |name| + code, line = <<-RUBY, __LINE__ + 1 + def #{name}; config.#{name}; end + def #{name}=(value); config.#{name} = value; end + RUBY + + singleton_class.class_eval code, __FILE__, line + class_eval code, __FILE__, line end end end def config - self.class.config + @config ||= ActiveSupport::InheritableOptions.new(self.class.config) end end end \ No newline at end of file -- cgit v1.2.3 From 2472f1026a7d3b3309937568204a5b46b60c2c3c Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 22 Apr 2010 13:02:55 -0300 Subject: HWIA delegates to to_hash symbolize_keys and stringify_keys and bang methods are not in the api Signed-off-by: Jeremy Kemper --- activesupport/lib/active_support/hash_with_indifferent_access.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 8241b69c8b..4de399a21a 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -111,8 +111,10 @@ module ActiveSupport super(convert_key(key)) end - def stringify_keys!; self end - def symbolize_keys!; self end + undef :stringify_keys! + def stringify_keys; to_hash.stringify_keys end + undef :symbolize_keys! + def symbolize_keys; to_hash.symbolize_keys end def to_options!; self end # Convert to a Hash with String keys. -- cgit v1.2.3 From d692e6be3091dd114afa0cce2778787d3af93e83 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 22 Apr 2010 09:47:40 -0700 Subject: Restore HWIA#stringify_keys! and update changelog --- activesupport/lib/active_support/hash_with_indifferent_access.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 4de399a21a..21407327cd 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -111,8 +111,8 @@ module ActiveSupport super(convert_key(key)) end - undef :stringify_keys! - def stringify_keys; to_hash.stringify_keys end + def stringify_keys!; self end + def stringify_keys; to_hash end undef :symbolize_keys! def symbolize_keys; to_hash.symbolize_keys end def to_options!; self end -- cgit v1.2.3 From c976784381d694df8090ad8a16c2648e93a2bd12 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 22 Apr 2010 10:07:33 -0700 Subject: Change HWIA#stringify_keys to return a HWIA not a Hash --- activesupport/lib/active_support/hash_with_indifferent_access.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 21407327cd..6b7a897cf2 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -112,7 +112,7 @@ module ActiveSupport end def stringify_keys!; self end - def stringify_keys; to_hash end + def stringify_keys; dup end undef :symbolize_keys! def symbolize_keys; to_hash.symbolize_keys end def to_options!; self end -- cgit v1.2.3 From 19cecc907f2c97458519f103cbb967cf8dda5716 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 22 Apr 2010 14:50:46 -0300 Subject: HWIA relies on Hash#symbolize_keys and #stringify_keys extensions. Signed-off-by: Jeremy Kemper --- activesupport/lib/active_support/hash_with_indifferent_access.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 6b7a897cf2..f64f0f44cc 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/hash/keys' + # This class has dubious semantics and we only have it so that # people can write params[:key] instead of params['key'] # and they get the same value for both keys. -- cgit v1.2.3 From 864bd9c21fdc8ce265c48fdfa2d4d7917032e717 Mon Sep 17 00:00:00 2001 From: David Chelimsky Date: Sat, 24 Apr 2010 21:12:02 -0500 Subject: allow unsubscribe by name or subscription [#4433 state:resolved] Signed-off-by: Jeremy Kemper --- .../lib/active_support/notifications/fanout.rb | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/notifications/fanout.rb b/activesupport/lib/active_support/notifications/fanout.rb index a3ddc7705a..300ec842a9 100644 --- a/activesupport/lib/active_support/notifications/fanout.rb +++ b/activesupport/lib/active_support/notifications/fanout.rb @@ -19,8 +19,8 @@ module ActiveSupport end def unsubscribe(subscriber) - @subscribers.delete(subscriber) @listeners_for.clear + @subscribers.reject! {|s| s.matches?(subscriber)} end def publish(name, *args) @@ -60,7 +60,7 @@ module ActiveSupport end def publish(*args) - return unless matches?(args.first) + return unless subscribed_to?(args.first) push(*args) true end @@ -69,10 +69,20 @@ module ActiveSupport true end - private - def matches?(name) - !@pattern || @pattern =~ name.to_s + def subscribed_to?(name) + !@pattern || @pattern =~ name.to_s + end + + def matches?(subscriber_or_name) + case subscriber_or_name + when String + @pattern && @pattern =~ subscriber_or_name + when self + true end + end + + private def push(*args) @block.call(*args) -- cgit v1.2.3 From 490a3335d56c124c8113aac0b63ad367f81bb450 Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Sun, 25 Apr 2010 19:48:40 +0200 Subject: Action Pack: fix tests with -K*, work around Ruby 1.9.1 constant lookup. [#4473 state:committed] Signed-off-by: Jeremy Kemper --- activesupport/lib/active_support/multibyte/chars.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 38007fd4e7..4ade1158fd 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -72,8 +72,8 @@ module ActiveSupport #:nodoc: def self.codepoints_to_pattern(array_of_codepoints) #:nodoc: array_of_codepoints.collect{ |e| [e].pack 'U*' }.join('|') end - UNICODE_TRAILERS_PAT = /(#{codepoints_to_pattern(UNICODE_LEADERS_AND_TRAILERS)})+\Z/ - UNICODE_LEADERS_PAT = /\A(#{codepoints_to_pattern(UNICODE_LEADERS_AND_TRAILERS)})+/ + UNICODE_TRAILERS_PAT = /(#{codepoints_to_pattern(UNICODE_LEADERS_AND_TRAILERS)})+\Z/u + UNICODE_LEADERS_PAT = /\A(#{codepoints_to_pattern(UNICODE_LEADERS_AND_TRAILERS)})+/u UTF8_PAT = ActiveSupport::Multibyte::VALID_CHARACTER['UTF-8'] -- cgit v1.2.3 From 53c13f1acaa2eb05e7f418b53f6156a4f5a773e0 Mon Sep 17 00:00:00 2001 From: Anil Wadghule Date: Mon, 26 Apr 2010 20:55:07 +0530 Subject: Use Config::CONFIG['host_os'] instead of RUBY_PLATFORM [#4477 state:resolved] Signed-off-by: Jeremy Kemper --- activesupport/lib/active_support/core_ext/kernel/reporting.rb | 3 ++- activesupport/lib/active_support/testing/isolation.rb | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/kernel/reporting.rb b/activesupport/lib/active_support/core_ext/kernel/reporting.rb index ac35db6ab6..7c455f66d5 100644 --- a/activesupport/lib/active_support/core_ext/kernel/reporting.rb +++ b/activesupport/lib/active_support/core_ext/kernel/reporting.rb @@ -1,3 +1,4 @@ +require 'rbconfig' module Kernel # Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards. # @@ -37,7 +38,7 @@ module Kernel # puts 'But this will' def silence_stream(stream) old_stream = stream.dup - stream.reopen(RUBY_PLATFORM =~ /mswin|mingw/ ? 'NUL:' : '/dev/null') + stream.reopen(Config::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null') stream.sync = true yield ensure diff --git a/activesupport/lib/active_support/testing/isolation.rb b/activesupport/lib/active_support/testing/isolation.rb index 9507dbf473..69df399cde 100644 --- a/activesupport/lib/active_support/testing/isolation.rb +++ b/activesupport/lib/active_support/testing/isolation.rb @@ -1,3 +1,4 @@ +require 'rbconfig' module ActiveSupport module Testing class RemoteError < StandardError @@ -33,7 +34,7 @@ module ActiveSupport module Isolation def self.forking_env? - !ENV["NO_FORK"] && RUBY_PLATFORM !~ /mswin|mingw|java/ + !ENV["NO_FORK"] && ((Config::CONFIG['host_os'] !~ /mswin|mingw/) && (RUBY_PLATFORM !~ /java/)) end def self.included(base) -- cgit v1.2.3 From 76d6a993647f3bbe39f31faa0b8ed8767a228301 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 26 Apr 2010 11:50:18 -0300 Subject: Use explicit source encoding rather than forced UTF-8 from US-ASCII. Signed-off-by: Jeremy Kemper --- activesupport/lib/active_support/core_ext/uri.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/uri.rb b/activesupport/lib/active_support/core_ext/uri.rb index ca1be8b7e9..28eabd2111 100644 --- a/activesupport/lib/active_support/core_ext/uri.rb +++ b/activesupport/lib/active_support/core_ext/uri.rb @@ -1,8 +1,9 @@ +# encoding: utf-8 + if RUBY_VERSION >= '1.9' require 'uri' str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese. - str.force_encoding(Encoding::UTF_8) if str.respond_to?(:force_encoding) parser = URI::Parser.new unless str == parser.unescape(parser.escape(str)) -- cgit v1.2.3 From c1d73270717f30498f8f4d55d6695509107c2834 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 26 Apr 2010 19:32:23 -0700 Subject: JSON: encode objects that don't have a native JSON representation using to_hash, if available, instead of instance_values (the old fallback) or to_s (other encoders' default). Encode BigDecimal and Regexp encode as strings to conform with other encoders. Try to transcode non-UTF-8 strings. --- activesupport/lib/active_support/json/encoding.rb | 24 ++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb index 8ba45f7ea2..0f38fd0e89 100644 --- a/activesupport/lib/active_support/json/encoding.rb +++ b/activesupport/lib/active_support/json/encoding.rb @@ -1,4 +1,5 @@ # encoding: utf-8 +require 'bigdecimal' require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/hash/except' require 'active_support/core_ext/hash/slice' @@ -102,7 +103,9 @@ module ActiveSupport end def escape(string) - string = string.dup.force_encoding(::Encoding::BINARY) if string.respond_to?(:force_encoding) + if string.respond_to?(:force_encoding) + string = string.encode(::Encoding::UTF_8, undef: :replace).force_encoding(::Encoding::BINARY) + end json = string. gsub(escape_regex) { |s| ESCAPED_CHARS[s] }. gsub(/([\xC0-\xDF][\x80-\xBF]| @@ -110,7 +113,9 @@ module ActiveSupport [\xF0-\xF7][\x80-\xBF]{3})+/nx) { |s| s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/n, '\\\\u\&') } - %("#{json}") + json = %("#{json}") + json.force_encoding(::Encoding::UTF_8) if json.respond_to?(:force_encoding) + json end end @@ -128,7 +133,13 @@ class Object ActiveSupport::JSON.encode(self, options) end - def as_json(options = nil) instance_values end #:nodoc: + def as_json(options = nil) #:nodoc: + if respond_to?(:to_hash) + to_hash + else + instance_values + end + end end # A string that returns itself as its JSON-encoded form. @@ -166,9 +177,12 @@ class Numeric def encode_json(encoder) to_s end #:nodoc: end +class BigDecimal + def as_json(options = nil) to_s end #:nodoc: +end + class Regexp - def as_json(options = nil) self end #:nodoc: - def encode_json(encoder) inspect end #:nodoc: + def as_json(options = nil) to_s end #:nodoc: end module Enumerable -- cgit v1.2.3