From 0ccd0b569ad4df7acc37a14531606ba6f74c69f4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 03:02:22 -0700 Subject: Fix doc typo. Move extend self so it's more immediately obvious. Require inflections from load path. --- activesupport/lib/active_support/inflector.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index fc88d80cdb..9d549eaae1 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -10,6 +10,8 @@ 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: # @@ -91,8 +93,6 @@ module ActiveSupport end end - extend self - # Yields a singleton instance of Inflector::Inflections so you can specify additional # inflector rules. # @@ -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' -- cgit v1.2.3 From 566d717d783f56563cd602198be2177c972c9a81 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 03:38:05 -0700 Subject: Move Class::ModelName to Active Support module core_ext --- .../lib/active_support/core_ext/module.rb | 5 +++++ .../active_support/core_ext/module/model_naming.rb | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/module/model_naming.rb (limited to 'activesupport/lib/active_support') 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 -- cgit v1.2.3 From e79d47847a75d63a66a3ed2296271fd28d41f24c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 13:03:59 -0700 Subject: Qualify Inflector in rdoc examples also. [#356 state:resolved] --- activesupport/lib/active_support/inflector.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 9d549eaae1..47bd6e1767 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -15,7 +15,7 @@ module ActiveSupport # 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' # @@ -97,7 +97,7 @@ module ActiveSupport # inflector rules. # # Example: - # Inflector.inflections do |inflect| + # ActiveSupport::Inflector.inflections do |inflect| # inflect.uncountable "rails" # end def inflections -- cgit v1.2.3 From 84fb971c2f3f26452fbc73467c13d039fe4c2024 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 21:34:36 -0700 Subject: Remove 1.9's String#chars also --- .../lib/active_support/core_ext/string/unicode.rb | 24 ++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'activesupport/lib/active_support') 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? -- cgit v1.2.3 From 67e4f16fc5303ae35bdfe9ef4ef016127751ce35 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 7 Jun 2008 18:06:45 -0700 Subject: No need to build a Set since we're iterating instead of checking for inclusion now --- activesupport/lib/active_support/core_ext/hash/slice.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'activesupport/lib/active_support') 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 -- cgit v1.2.3 From 6c970d79a064b953d3d9555a362a1ad1e0058d1c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 7 Jun 2008 17:23:25 -0700 Subject: Performance: faster Object.subclasses_of --- .../lib/active_support/core_ext/object/extending.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'activesupport/lib/active_support') 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 -- cgit v1.2.3 From 19895f087c338d8385dff9d272d30fb87cb10330 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 10 Jun 2008 10:29:25 +0100 Subject: Lazy load cache and session stores --- activesupport/lib/active_support/cache.rb | 9 +++------ activesupport/lib/active_support/cache/drb_store.rb | 1 + 2 files changed, 4 insertions(+), 6 deletions(-) (limited to 'activesupport/lib/active_support') 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/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 -- cgit v1.2.3 From f5cbad21ac09822a61d6326cbadea16bbe86b623 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 10 Jun 2008 03:42:43 -0700 Subject: Rubinious: work around h[k] ||= v returning []= result instead of v --- activesupport/lib/active_support/core_ext/enumerable.rb | 17 ++++++++++++++--- activesupport/lib/active_support/ordered_hash.rb | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index f1469aa0e3..e7f537d045 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/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) -- cgit v1.2.3 From 34c51c9e8f758a5fc892e654b6ca1ad8b86d1b3a Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 10 Jun 2008 03:46:23 -0700 Subject: Rubinious: setup/teardown override for miniunit --- .../active_support/testing/setup_and_teardown.rb | 38 ++++++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'activesupport/lib/active_support') 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) -- cgit v1.2.3 From 634e462a0b70ddae2f21dbddddd07e7b340bb69c Mon Sep 17 00:00:00 2001 From: Grant Hollingworth Date: Tue, 10 Jun 2008 11:54:58 -0400 Subject: Performance: speed up Hash#except. [#382 state:resolved] --- activesupport/lib/active_support/core_ext/hash/except.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'activesupport/lib/active_support') 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 -- cgit v1.2.3 From dd4181f47dc0f166eb5d3e47a4a0dc1594cc5669 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 12 Jun 2008 14:20:30 -0500 Subject: Ensure MemCacheStore is required when using CompressedMemCacheStore since they are lazy load now. --- activesupport/lib/active_support/cache/compressed_mem_cache_store.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activesupport/lib/active_support') 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 -- cgit v1.2.3