public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Move some core extension methods into a module under the 
ActiveSupport::CoreExtensions namespace, instead of extending core classes 
directly. This is more friendly for API reference generators.

[#915 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Hongli Lai (Phusion) (author)
Wed Aug 27 06:48:14 -0700 2008
jeremy (committer)
Fri Aug 29 15:15:40 -0700 2008
commit  204a8cce88d986fcfafe01ce9ba44818739374c3
tree    6eb7e01a9b14784d9a06614a29982593a7fb8416
parent  7f179f8540ab92dbd9d3e650b465de5b694d93d4
...
8
9
10
 
 
 
 
 
 
 
 
 
11
12
 
13
...
8
9
10
11
12
13
14
15
16
17
18
19
20
 
21
22
0
@@ -8,6 +8,15 @@ require 'active_support/core_ext/module/loading'
0
 require 'active_support/core_ext/module/aliasing'
0
 require 'active_support/core_ext/module/model_naming'
0
 
0
+module ActiveSupport
0
+  module CoreExtensions
0
+    # Various extensions for the Ruby core Module class.
0
+    module Module
0
+      # Nothing here. Only defined for API documentation purposes.
0
+    end
0
+  end
0
+end
0
+
0
 class Module
0
-  include ActiveSupport::CoreExt::Module::ModelNaming
0
+  include ActiveSupport::CoreExtensions::Module
0
 end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
70
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
0
@@ -1,70 +1,74 @@
0
-class Module
0
-  # Encapsulates the common pattern of:
0
-  #
0
-  #   alias_method :foo_without_feature, :foo
0
-  #   alias_method :foo, :foo_with_feature
0
-  #
0
-  # With this, you simply do:
0
-  #
0
-  #   alias_method_chain :foo, :feature
0
-  #
0
-  # And both aliases are set up for you.
0
-  #
0
-  # Query and bang methods (foo?, foo!) keep the same punctuation:
0
-  #
0
-  #   alias_method_chain :foo?, :feature
0
-  #
0
-  # is equivalent to
0
-  #
0
-  #   alias_method :foo_without_feature?, :foo?
0
-  #   alias_method :foo?, :foo_with_feature?
0
-  #
0
-  # so you can safely chain foo, foo?, and foo! with the same feature.
0
-  def alias_method_chain(target, feature)
0
-    # Strip out punctuation on predicates or bang methods since
0
-    # e.g. target?_without_feature is not a valid method name.
0
-    aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
0
-    yield(aliased_target, punctuation) if block_given?
0
-    
0
-    with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
0
-    
0
-    alias_method without_method, target
0
-    alias_method target, with_method
0
-    
0
-    case
0
-      when public_method_defined?(without_method)
0
-        public target
0
-      when protected_method_defined?(without_method)
0
-        protected target
0
-      when private_method_defined?(without_method)
0
-        private target
0
-    end
0
-  end
0
+module ActiveSupport
0
+  module CoreExtensions
0
+    module Module
0
+      # Encapsulates the common pattern of:
0
+      #
0
+      #   alias_method :foo_without_feature, :foo
0
+      #   alias_method :foo, :foo_with_feature
0
+      #
0
+      # With this, you simply do:
0
+      #
0
+      #   alias_method_chain :foo, :feature
0
+      #
0
+      # And both aliases are set up for you.
0
+      #
0
+      # Query and bang methods (foo?, foo!) keep the same punctuation:
0
+      #
0
+      #   alias_method_chain :foo?, :feature
0
+      #
0
+      # is equivalent to
0
+      #
0
+      #   alias_method :foo_without_feature?, :foo?
0
+      #   alias_method :foo?, :foo_with_feature?
0
+      #
0
+      # so you can safely chain foo, foo?, and foo! with the same feature.
0
+      def alias_method_chain(target, feature)
0
+        # Strip out punctuation on predicates or bang methods since
0
+        # e.g. target?_without_feature is not a valid method name.
0
+        aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
0
+        yield(aliased_target, punctuation) if block_given?
0
+
0
+        with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
0
 
0
-  # Allows you to make aliases for attributes, which includes 
0
-  # getter, setter, and query methods.
0
-  #
0
-  # Example:
0
-  #
0
-  #   class Content < ActiveRecord::Base
0
-  #     # has a title attribute
0
-  #   end
0
-  #
0
-  #   class Email < Content
0
-  #     alias_attribute :subject, :title
0
-  #   end
0
-  #
0
-  #   e = Email.find(1)
0
-  #   e.title    # => "Superstars"
0
-  #   e.subject  # => "Superstars"
0
-  #   e.subject? # => true
0
-  #   e.subject = "Megastars"
0
-  #   e.title    # => "Megastars"
0
-  def alias_attribute(new_name, old_name)
0
-    module_eval <<-STR, __FILE__, __LINE__+1
0
-      def #{new_name}; self.#{old_name}; end
0
-      def #{new_name}?; self.#{old_name}?; end
0
-      def #{new_name}=(v); self.#{old_name} = v; end
0
-    STR
0
+        alias_method without_method, target
0
+        alias_method target, with_method
0
+
0
+        case
0
+          when public_method_defined?(without_method)
0
+            public target
0
+          when protected_method_defined?(without_method)
0
+            protected target
0
+          when private_method_defined?(without_method)
0
+            private target
0
+        end
0
+      end
0
+
0
+      # Allows you to make aliases for attributes, which includes
0
+      # getter, setter, and query methods.
0
+      #
0
+      # Example:
0
+      #
0
+      #   class Content < ActiveRecord::Base
0
+      #     # has a title attribute
0
+      #   end
0
+      #
0
+      #   class Email < Content
0
+      #     alias_attribute :subject, :title
0
+      #   end
0
+      #
0
+      #   e = Email.find(1)
0
+      #   e.title    # => "Superstars"
0
+      #   e.subject  # => "Superstars"
0
+      #   e.subject? # => true
0
+      #   e.subject = "Megastars"
0
+      #   e.title    # => "Megastars"
0
+      def alias_attribute(new_name, old_name)
0
+        module_eval <<-STR, __FILE__, __LINE__+1
0
+          def #{new_name}; self.#{old_name}; end
0
+          def #{new_name}?; self.#{old_name}?; end
0
+          def #{new_name}=(v); self.#{old_name} = v; end
0
+        STR
0
+      end
0
+    end
0
   end
0
 end
...
1
2
3
4
5
6
7
8
9
10
 
 
 
 
 
 
 
 
 
 
 
 
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
53
54
55
56
57
58
59
60
61
62
63
64
 
 
 
 
 
 
 
 
 
 
 
 
65
66
67
68
 
 
 
 
 
 
 
 
69
70
71
72
 
 
 
 
73
74
75
76
77
78
79
80
81
82
83
84
85
86
...
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 
 
 
 
59
 
 
 
 
 
 
 
60
61
62
63
64
65
66
67
68
69
70
71
72
 
 
 
73
74
75
76
77
78
79
80
81
82
 
 
83
84
85
86
87
88
 
 
 
 
 
 
 
 
 
 
89
90
0
@@ -1,86 +1,90 @@
0
-class Module
0
-  # Returns the name of the module containing this one.
0
-  #
0
-  #   p M::N.parent_name # => "M"
0
-  def parent_name
0
-    unless defined? @parent_name
0
-      @parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
0
-    end
0
-    @parent_name
0
-  end
0
+module ActiveSupport
0
+  module CoreExtensions
0
+    module Module
0
+      # Returns the name of the module containing this one.
0
+      #
0
+      #   p M::N.parent_name # => "M"
0
+      def parent_name
0
+        unless defined? @parent_name
0
+          @parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
0
+        end
0
+        @parent_name
0
+      end
0
 
0
-  # Returns the module which contains this one according to its name.
0
-  #
0
-  #   module M
0
-  #     module N
0
-  #     end
0
-  #   end
0
-  #   X = M::N
0
-  #   
0
-  #   p M::N.parent # => M
0
-  #   p X.parent    # => M
0
-  #
0
-  # The parent of top-level and anonymous modules is Object.
0
-  #
0
-  #   p M.parent          # => Object
0
-  #   p Module.new.parent # => Object
0
-  #
0
-  def parent
0
-    parent_name ? parent_name.constantize : Object
0
-  end
0
+      # Returns the module which contains this one according to its name.
0
+      #
0
+      #   module M
0
+      #     module N
0
+      #     end
0
+      #   end
0
+      #   X = M::N
0
+      #
0
+      #   p M::N.parent # => M
0
+      #   p X.parent    # => M
0
+      #
0
+      # The parent of top-level and anonymous modules is Object.
0
+      #
0
+      #   p M.parent          # => Object
0
+      #   p Module.new.parent # => Object
0
+      #
0
+      def parent
0
+        parent_name ? parent_name.constantize : Object
0
+      end
0
 
0
-  # Returns all the parents of this module according to its name, ordered from
0
-  # nested outwards. The receiver is not contained within the result.
0
-  #
0
-  #   module M
0
-  #     module N
0
-  #     end
0
-  #   end
0
-  #   X = M::N
0
-  #   
0
-  #   p M.parents    # => [Object]
0
-  #   p M::N.parents # => [M, Object]
0
-  #   p X.parents    # => [M, Object]
0
-  #
0
-  def parents
0
-    parents = []
0
-    if parent_name
0
-      parts = parent_name.split('::')
0
-      until parts.empty?
0
-        parents << (parts * '::').constantize
0
-        parts.pop
0
+      # Returns all the parents of this module according to its name, ordered from
0
+      # nested outwards. The receiver is not contained within the result.
0
+      #
0
+      #   module M
0
+      #     module N
0
+      #     end
0
+      #   end
0
+      #   X = M::N
0
+      #
0
+      #   p M.parents    # => [Object]
0
+      #   p M::N.parents # => [M, Object]
0
+      #   p X.parents    # => [M, Object]
0
+      #
0
+      def parents
0
+        parents = []
0
+        if parent_name
0
+          parts = parent_name.split('::')
0
+          until parts.empty?
0
+            parents << (parts * '::').constantize
0
+            parts.pop
0
+          end
0
+        end
0
+        parents << Object unless parents.include? Object
0
+        parents
0
       end
0
-    end
0
-    parents << Object unless parents.include? Object
0
-    parents
0
-  end
0
 
0
-  if RUBY_VERSION < '1.9'
0
-    # Returns the constants that have been defined locally by this object and
0
-    # not in an ancestor. This method is exact if running under Ruby 1.9. In
0
-    # previous versions it may miss some constants if their definition in some
0
-    # ancestor is identical to their definition in the receiver.
0
-    def local_constants
0
-      inherited = {}
0
+      if RUBY_VERSION < '1.9'
0
+        # Returns the constants that have been defined locally by this object and
0
+        # not in an ancestor. This method is exact if running under Ruby 1.9. In
0
+        # previous versions it may miss some constants if their definition in some
0
+        # ancestor is identical to their definition in the receiver.
0
+        def local_constants
0
+          inherited = {}
0
+
0
+          ancestors.each do |anc|
0
+            next if anc == self
0
+            anc.constants.each { |const| inherited[const] = anc.const_get(const) }
0
+          end
0
 
0
-      ancestors.each do |anc|
0
-        next if anc == self
0
-        anc.constants.each { |const| inherited[const] = anc.const_get(const) }
0
+          constants.select do |const|
0
+            !inherited.key?(const) || inherited[const].object_id != const_get(const).object_id
0
+          end
0
+        end
0
+      else
0
+        def local_constants #:nodoc:
0
+          constants(false)
0
+        end
0
       end
0
 
0
-      constants.select do |const|
0
-        !inherited.key?(const) || inherited[const].object_id != const_get(const).object_id
0
+      # Returns the names of the constants defined locally rather than the
0
+      # constants themselves. See <tt>local_constants</tt>.
0
+      def local_constant_names
0
+        local_constants.map { |c| c.to_s }
0
       end
0
     end
0
-  else
0
-    def local_constants #:nodoc:
0
-      constants(false)
0
-    end
0
-  end
0
-
0
-  # Returns the names of the constants defined locally rather than the
0
-  # constants themselves. See <tt>local_constants</tt>.
0
-  def local_constant_names
0
-    local_constants.map { |c| c.to_s }
0
   end
0
 end
...
11
12
13
14
 
15
16
17
18
19
 
 
 
 
20
21
22
...
11
12
13
 
14
15
 
 
 
 
16
17
18
19
20
21
22
0
@@ -11,12 +11,12 @@ module ActiveSupport
0
     end
0
   end
0
 
0
-  module CoreExt
0
+  module CoreExtensions
0
     module Module
0
-      module ModelNaming
0
-        def model_name
0
-          @model_name ||= ModelName.new(name)
0
-        end
0
+      # Returns an ActiveSupport::ModelName object for module. It can be
0
+      # used to retrieve all kinds of naming-related information.
0
+      def model_name
0
+        @model_name ||= ModelName.new(name)
0
       end
0
     end
0
   end

Comments