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/core_ext') 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 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/core_ext') 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/core_ext') 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/core_ext') 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 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 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') 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: -- 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/core_ext') 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