aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/cache.rb9
-rw-r--r--activesupport/lib/active_support/cache/compressed_mem_cache_store.rb2
-rw-r--r--activesupport/lib/active_support/cache/drb_store.rb1
-rw-r--r--activesupport/lib/active_support/core_ext/enumerable.rb17
-rw-r--r--activesupport/lib/active_support/core_ext/hash/except.rb7
-rw-r--r--activesupport/lib/active_support/core_ext/hash/slice.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/module.rb5
-rw-r--r--activesupport/lib/active_support/core_ext/module/model_naming.rb22
-rw-r--r--activesupport/lib/active_support/core_ext/object/extending.rb17
-rw-r--r--activesupport/lib/active_support/core_ext/string/unicode.rb24
-rw-r--r--activesupport/lib/active_support/inflector.rb12
-rw-r--r--activesupport/lib/active_support/ordered_hash.rb1
-rw-r--r--activesupport/lib/active_support/testing/setup_and_teardown.rb38
-rw-r--r--activesupport/test/core_ext/class_test.rb2
-rw-r--r--activesupport/test/core_ext/module/model_naming_test.rb19
-rw-r--r--activesupport/test/core_ext/time_with_zone_test.rb34
-rw-r--r--activesupport/test/test_test.rb6
17 files changed, 155 insertions, 67 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index 2f1143e610..07c83774df 100644
--- a/activesupport/lib/active_support/cache.rb
+++ b/activesupport/lib/active_support/cache.rb
@@ -7,10 +7,13 @@ module ActiveSupport
case store
when Symbol
+ require "active_support/cache/#{store.to_s}"
+
store_class_name = (store == :drb_store ? "DRbStore" : store.to_s.camelize)
store_class = ActiveSupport::Cache.const_get(store_class_name)
store_class.new(*parameters)
when nil
+ require "active_support/cache/memory_store"
ActiveSupport::Cache::MemoryStore.new
else
store
@@ -137,9 +140,3 @@ module ActiveSupport
end
end
end
-
-require 'active_support/cache/file_store'
-require 'active_support/cache/memory_store'
-require 'active_support/cache/drb_store'
-require 'active_support/cache/mem_cache_store'
-require 'active_support/cache/compressed_mem_cache_store'
diff --git a/activesupport/lib/active_support/cache/compressed_mem_cache_store.rb b/activesupport/lib/active_support/cache/compressed_mem_cache_store.rb
index 9470ac9f66..3f1f9ad179 100644
--- a/activesupport/lib/active_support/cache/compressed_mem_cache_store.rb
+++ b/activesupport/lib/active_support/cache/compressed_mem_cache_store.rb
@@ -1,3 +1,5 @@
+require "active_support/cache/mem_cache_store"
+
module ActiveSupport
module Cache
class CompressedMemCacheStore < MemCacheStore
diff --git a/activesupport/lib/active_support/cache/drb_store.rb b/activesupport/lib/active_support/cache/drb_store.rb
index b80c2ee4d5..f06f08f566 100644
--- a/activesupport/lib/active_support/cache/drb_store.rb
+++ b/activesupport/lib/active_support/cache/drb_store.rb
@@ -1,4 +1,5 @@
require 'drb'
+require 'active_support/cache/memory_store'
module ActiveSupport
module Cache
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 47ff19e963..8e148cc1b4 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -1,3 +1,5 @@
+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'
@@ -18,10 +20,19 @@ module Enumerable
# "2006-02-24 -> Transcript, Transcript"
# "2006-02-23 -> Transcript"
def group_by
- inject ActiveSupport::OrderedHash.new do |grouped, element|
- (grouped[yield(element)] ||= []) << element
- grouped
+ 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:
diff --git a/activesupport/lib/active_support/core_ext/hash/except.rb b/activesupport/lib/active_support/core_ext/hash/except.rb
index 8362cd880e..bc97fa35a6 100644
--- a/activesupport/lib/active_support/core_ext/hash/except.rb
+++ b/activesupport/lib/active_support/core_ext/hash/except.rb
@@ -10,13 +10,14 @@ module ActiveSupport #:nodoc:
module Except
# Returns a new hash without the given keys.
def except(*keys)
- rejected = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
- reject { |key,| rejected.include?(key) }
+ clone.except!(*keys)
end
# Replaces the hash without only the given keys.
def except!(*keys)
- replace(except(*keys))
+ keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
+ keys.each { |key| delete(key) }
+ self
end
end
end
diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb
index 1b2c8f63e3..be4dec6e53 100644
--- a/activesupport/lib/active_support/core_ext/hash/slice.rb
+++ b/activesupport/lib/active_support/core_ext/hash/slice.rb
@@ -1,5 +1,3 @@
-require 'set'
-
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Hash #:nodoc:
@@ -14,9 +12,9 @@ module ActiveSupport #:nodoc:
module Slice
# Returns a new hash with only the given keys.
def slice(*keys)
- allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
+ keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
hash = {}
- allowed.each { |k| hash[k] = self[k] if has_key?(k) }
+ keys.each { |k| hash[k] = self[k] if has_key?(k) }
hash
end
diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb
index da8d5b3762..34fcbd124b 100644
--- a/activesupport/lib/active_support/core_ext/module.rb
+++ b/activesupport/lib/active_support/core_ext/module.rb
@@ -6,3 +6,8 @@ require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/module/introspection'
require 'active_support/core_ext/module/loading'
require 'active_support/core_ext/module/aliasing'
+require 'active_support/core_ext/module/model_naming'
+
+class Module
+ include ActiveSupport::CoreExt::Module::ModelNaming
+end
diff --git a/activesupport/lib/active_support/core_ext/module/model_naming.rb b/activesupport/lib/active_support/core_ext/module/model_naming.rb
new file mode 100644
index 0000000000..26e76ab556
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/module/model_naming.rb
@@ -0,0 +1,22 @@
+module ActiveSupport
+ class ModelName < String
+ attr_reader :singular, :plural, :partial_path
+
+ def initialize(name)
+ super
+ @singular = underscore.tr('/', '_').freeze
+ @plural = @singular.pluralize.freeze
+ @partial_path = "#{tableize}/#{demodulize.underscore}".freeze
+ end
+ end
+
+ module CoreExt
+ module Module
+ module ModelNaming
+ def model_name
+ @model_name ||= ModelName.new(name)
+ end
+ end
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/core_ext/object/extending.rb b/activesupport/lib/active_support/core_ext/object/extending.rb
index 43a2be916e..082e98a297 100644
--- a/activesupport/lib/active_support/core_ext/object/extending.rb
+++ b/activesupport/lib/active_support/core_ext/object/extending.rb
@@ -3,17 +3,18 @@ class Object
Class.remove_class(*subclasses_of(*superclasses))
end
+ # Exclude this class unless it's a subclass of our supers and is defined.
+ # We check defined? in case we find a removed class that has yet to be
+ # garbage collected. This also fails for anonymous classes -- please
+ # submit a patch if you have a workaround.
def subclasses_of(*superclasses) #:nodoc:
subclasses = []
- # Exclude this class unless it's a subclass of our supers and is defined.
- # We check defined? in case we find a removed class that has yet to be
- # garbage collected. This also fails for anonymous classes -- please
- # submit a patch if you have a workaround.
- ObjectSpace.each_object(Class) do |k|
- if superclasses.any? { |superclass| k < superclass } &&
- (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id"))
- subclasses << k
+ superclasses.each do |sup|
+ ObjectSpace.each_object(class << sup; self; end) do |k|
+ if k != sup && (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id"))
+ subclasses << k
+ end
end
end
diff --git a/activesupport/lib/active_support/core_ext/string/unicode.rb b/activesupport/lib/active_support/core_ext/string/unicode.rb
index 5e20534d1d..666f7bcb65 100644
--- a/activesupport/lib/active_support/core_ext/string/unicode.rb
+++ b/activesupport/lib/active_support/core_ext/string/unicode.rb
@@ -1,16 +1,16 @@
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module String #:nodoc:
- unless '1.9'.respond_to?(:force_encoding)
- # Define methods for handling unicode data.
- module Unicode
- def self.append_features(base)
- if '1.8.7'.respond_to?(:chars)
- base.class_eval { remove_method :chars }
- end
- super
+ # Define methods for handling unicode data.
+ module Unicode
+ def self.append_features(base)
+ if '1.8.7 and later'.respond_to?(:chars)
+ base.class_eval { remove_method :chars }
end
+ super
+ end
+ unless '1.9'.respond_to?(:force_encoding)
# +chars+ is a Unicode safe proxy for string methods. 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. Undefined methods are forwarded to String, so all of the
@@ -44,14 +44,12 @@ module ActiveSupport #:nodoc:
def is_utf8?
ActiveSupport::Multibyte::Handlers::UTF8Handler.consumes?(self)
end
- end
- else
- module Unicode #:nodoc:
- def chars
+ else
+ def chars #:nodoc:
self
end
- def is_utf8?
+ def is_utf8? #:nodoc:
case encoding
when Encoding::UTF_8
valid_encoding?
diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb
index fc88d80cdb..47bd6e1767 100644
--- a/activesupport/lib/active_support/inflector.rb
+++ b/activesupport/lib/active_support/inflector.rb
@@ -10,10 +10,12 @@ module ActiveSupport
# If you discover an incorrect inflection and require it for your application, you'll need
# to correct it yourself (explained below).
module Inflector
+ extend self
+
# A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
# inflection rules. Examples:
#
- # Inflector.inflections do |inflect|
+ # ActiveSupport::Inflector.inflections do |inflect|
# inflect.plural /^(ox)$/i, '\1\2en'
# inflect.singular /^(ox)en/i, '\1'
#
@@ -91,13 +93,11 @@ module ActiveSupport
end
end
- extend self
-
# Yields a singleton instance of Inflector::Inflections so you can specify additional
# inflector rules.
#
# Example:
- # Inflector.inflections do |inflect|
+ # ActiveSupport::Inflector.inflections do |inflect|
# inflect.uncountable "rails"
# end
def inflections
@@ -134,7 +134,7 @@ module ActiveSupport
# "posts".singularize # => "post"
# "octopi".singularize # => "octopus"
# "sheep".singluarize # => "sheep"
- # "word".singluarize # => "word"
+ # "word".singularize # => "word"
# "the blue mailmen".singularize # => "the blue mailman"
# "CamelOctopi".singularize # => "CamelOctopus"
def singularize(word)
@@ -307,4 +307,4 @@ module ActiveSupport
end
end
-require File.dirname(__FILE__) + '/inflections'
+require 'active_support/inflections'
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb
index 59ceaec696..9757054e43 100644
--- a/activesupport/lib/active_support/ordered_hash.rb
+++ b/activesupport/lib/active_support/ordered_hash.rb
@@ -12,6 +12,7 @@ module ActiveSupport
else
self << [key, value]
end
+ value
end
def [](key)
diff --git a/activesupport/lib/active_support/testing/setup_and_teardown.rb b/activesupport/lib/active_support/testing/setup_and_teardown.rb
index ed8e34510a..21d71eb92a 100644
--- a/activesupport/lib/active_support/testing/setup_and_teardown.rb
+++ b/activesupport/lib/active_support/testing/setup_and_teardown.rb
@@ -10,19 +10,43 @@ module ActiveSupport
end
def self.included(base)
- base.send :include, ActiveSupport::Callbacks
- base.define_callbacks :setup, :teardown
+ base.class_eval do
+ include ActiveSupport::Callbacks
+ define_callbacks :setup, :teardown
+ if defined?(::Mini)
+ alias_method :run, :run_with_callbacks_and_miniunit
+ else
+ begin
+ require 'mocha'
+ alias_method :run, :run_with_callbacks_and_mocha
+ rescue LoadError
+ alias_method :run, :run_with_callbacks_and_testunit
+ end
+ end
+ end
+ end
+
+ def run_with_callbacks_and_miniunit(runner)
+ result = '.'
begin
- require 'mocha'
- base.alias_method_chain :run, :callbacks_and_mocha
- rescue LoadError
- base.alias_method_chain :run, :callbacks
+ run_callbacks :setup
+ result = super
+ rescue Exception => e
+ result = runner.puke(self.class, self.name, e)
+ ensure
+ begin
+ teardown
+ run_callbacks :teardown, :enumerator => :reverse_each
+ rescue Exception => e
+ result = runner.puke(self.class, self.name, e)
+ end
end
+ result
end
# This redefinition is unfortunate but test/unit shows us no alternative.
- def run_with_callbacks(result) #:nodoc:
+ def run_with_callbacks_and_testunit(result) #:nodoc:
return if @method_name.to_s == "default_test"
yield(Test::Unit::TestCase::STARTED, name)
diff --git a/activesupport/test/core_ext/class_test.rb b/activesupport/test/core_ext/class_test.rb
index 0346fad190..9c6071d478 100644
--- a/activesupport/test/core_ext/class_test.rb
+++ b/activesupport/test/core_ext/class_test.rb
@@ -38,9 +38,9 @@ class ClassTest < Test::Unit::TestCase
@parent = eval("class D; end; D")
@sub = eval("class E < D; end; E")
@subofsub = eval("class F < E; end; F")
- assert @parent.subclasses.all? { |i| [@sub.to_s, @subofsub.to_s].include?(i) }
assert_equal 2, @parent.subclasses.size
assert_equal [@subofsub.to_s], @sub.subclasses
assert_equal [], @subofsub.subclasses
+ assert_equal [@sub.to_s, @subofsub.to_s].sort, @parent.subclasses.sort
end
end
diff --git a/activesupport/test/core_ext/module/model_naming_test.rb b/activesupport/test/core_ext/module/model_naming_test.rb
new file mode 100644
index 0000000000..fc73fa5c36
--- /dev/null
+++ b/activesupport/test/core_ext/module/model_naming_test.rb
@@ -0,0 +1,19 @@
+require 'abstract_unit'
+
+class ModelNamingTest < Test::Unit::TestCase
+ def setup
+ @name = ActiveSupport::ModelName.new('Post::TrackBack')
+ end
+
+ def test_singular
+ assert_equal 'post_track_back', @name.singular
+ end
+
+ def test_plural
+ assert_equal 'post_track_backs', @name.plural
+ end
+
+ def test_partial_path
+ assert_equal 'post/track_backs/track_back', @name.partial_path
+ end
+end
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb
index 012ec373c9..0977cd8e50 100644
--- a/activesupport/test/core_ext/time_with_zone_test.rb
+++ b/activesupport/test/core_ext/time_with_zone_test.rb
@@ -336,24 +336,26 @@ class TimeWithZoneTest < Test::Unit::TestCase
end
end
- def test_method_missing_with_non_time_return_value
- silence_warnings do # silence warnings raised by tzinfo gem
- @twz.time.expects(:foo).returns('bar')
- assert_equal 'bar', @twz.foo
+ uses_mocha 'TestDatePartValueMethods' do
+ def test_method_missing_with_non_time_return_value
+ silence_warnings do # silence warnings raised by tzinfo gem
+ @twz.time.expects(:foo).returns('bar')
+ assert_equal 'bar', @twz.foo
+ end
end
- end
- def test_date_part_value_methods
- silence_warnings do # silence warnings raised by tzinfo gem
- twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone)
- twz.stubs(:method_missing).returns(nil) #ensure these methods are defined directly on class
- assert_equal 1999, twz.year
- assert_equal 12, twz.month
- assert_equal 31, twz.day
- assert_equal 14, twz.hour
- assert_equal 18, twz.min
- assert_equal 17, twz.sec
- assert_equal 500, twz.usec
+ def test_date_part_value_methods
+ silence_warnings do # silence warnings raised by tzinfo gem
+ twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone)
+ twz.stubs(:method_missing).returns(nil) #ensure these methods are defined directly on class
+ assert_equal 1999, twz.year
+ assert_equal 12, twz.month
+ assert_equal 31, twz.day
+ assert_equal 14, twz.hour
+ assert_equal 18, twz.min
+ assert_equal 17, twz.sec
+ assert_equal 500, twz.usec
+ end
end
end
diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb
index 1e75e18602..26a45af255 100644
--- a/activesupport/test/test_test.rb
+++ b/activesupport/test/test_test.rb
@@ -84,6 +84,12 @@ class SetupAndTeardownTest < Test::Unit::TestCase
assert_equal [:foo, :sentinel, :foo], self.class.teardown_callback_chain.map(&:method)
end
+ def setup
+ end
+
+ def teardown
+ end
+
protected
def reset_callback_record
@called_back = []