reviews commit dcc9d38

This commit is contained in:
Xavier Noria 2010-08-14 17:04:17 +02:00
parent c7adc96186
commit 21bbcfe42c
1 changed files with 22 additions and 32 deletions

View File

@ -3,48 +3,48 @@ module ActiveSupport
# #
# module M # module M
# def self.included(base) # def self.included(base)
# base.send(:extend, ClassMethods) # base.extend, ClassMethods
# base.send(:include, InstanceMethods) # base.send(:include, InstanceMethods)
# scope :foo, :conditions => { :created_at => nil } # scope :disabled, where(:disabled => true)
# end # end
# #
# module ClassMethods # module ClassMethods
# def cm; puts 'I am a class method'; end # ...
# end # end
# #
# module InstanceMethods # module InstanceMethods
# def im; puts 'I am an instance method'; end # ...
# end # end
# end # end
# #
# By using <tt>ActiveSupport::Concern</tt> the above module could instead be written as: # By using <tt>ActiveSupport::Concern</tt> the above module could instead be written as:
# #
# require 'active_support/concern' # require 'active_support/concern'
# #
# module M # module M
# extend ActiveSupport::Concern # extend ActiveSupport::Concern
# #
# included do # included do
# scope :foo, :conditions => { :created_at => nil } # scope :disabled, where(:disabled => true)
# end # end
# #
# module ClassMethods # module ClassMethods
# def cm; puts 'I am a class method'; end # ...
# end # end
# #
# module InstanceMethods # module InstanceMethods
# def im; puts 'I am an instance method'; end # ...
# end # end
# end # end
# #
# Moreover, it gracefully handles module dependencies. Given a Foo module and a Bar module which depends on the former, we would typically write the following: # Moreover, it gracefully handles module dependencies. Given a +Foo+ module and a +Bar+
# module which depends on the former, we would typically write the following:
# #
# module Foo # module Foo
# def self.included(base) # def self.included(base)
# # Define some :enhanced_method for Host class
# base.class_eval do # base.class_eval do
# def self.enhanced_method # def self.method_injected_by_foo
# # Do enhanced stuff # ...
# end # end
# end # end
# end # end
@ -52,7 +52,7 @@ module ActiveSupport
# #
# module Bar # module Bar
# def self.included(base) # def self.included(base)
# base.send(:enhanced_method) # base.method_injected_by_foo
# end # end
# end # end
# #
@ -61,23 +61,13 @@ module ActiveSupport
# include Bar # Bar is the module that Host really needs # include Bar # Bar is the module that Host really needs
# end # end
# #
# But why should Host care about Bar's dependencies, namely Foo? We could try to hide these from Host directly including Foo in Bar: # But why should +Host+ care about +Bar+'s dependencies, namely +Foo+? We could try to hide
# # these from +Host+ directly including +Foo+ in +Bar+:
# module Foo
# def self.included(base)
# # Define some :enhanced_method for Host class
# base.class_eval do
# def self.enhanced_method
# # Do enhanced stuff
# end
# end
# end
# end
# #
# module Bar # module Bar
# include Foo # include Foo
# def self.included(base) # def self.included(base)
# base.send(:enhanced_method) # base.method_injected_by_foo
# end # end
# end # end
# #
@ -85,8 +75,8 @@ module ActiveSupport
# include Bar # include Bar
# end # end
# #
# Unfortunately this won't work, since when Foo is included, its <tt>base</tt> is Bar module, not Host class. # Unfortunately this won't work, since when +Foo+ is included, its <tt>base</tt> is the +Bar+ module,
# With <tt>ActiveSupport::Concern</tt>, module dependencies are properly resolved: # not the +Host+ class. With <tt>ActiveSupport::Concern</tt>, module dependencies are properly resolved:
# #
# require 'active_support/concern' # require 'active_support/concern'
# #
@ -94,8 +84,8 @@ module ActiveSupport
# extend ActiveSupport::Concern # extend ActiveSupport::Concern
# included do # included do
# class_eval do # class_eval do
# def self.enhanced_method # def self.method_injected_by_foo
# # Do enhanced stuff # ...
# end # end
# end # end
# end # end
@ -106,12 +96,12 @@ module ActiveSupport
# include Foo # include Foo
# #
# included do # included do
# self.send(:enhanced_method) # self.method_injected_by_foo
# end # end
# end # end
# #
# class Host # class Host
# include Bar # Host only needs to care about Bar without needing to know about its dependencies # include Bar # works, Bar takes care now of its dependencies
# end # end
# #
module Concern module Concern