您的位置:首页 > 编程语言 > Ruby

关于ActiveSupport::Concern

2015-07-07 17:58 381 查看
在不使用ActiveSupport::Concern,通常module的写法如下:

module Foo
module ClassMethods
#...
end

module InstanceMethods
#...
end

def self.included(base)
base.extend         ClassMethods
base.send :include, InstanceMethods
end
end


上述代码可以看出,include有一个钩子方法included,通过这个钩子方法可以实现我们想要的效果。

通过extend为base定义ClassMethods中的类方法,通过include为base定义InstanceMethods中的实例方法. (还有class_eval)

我们可以使用ActiveSupport::Concern这样来改写代码

require "active_support/concern"

module Concern
extend Active_Support::Concern

module ClassMethods

end

module InstanceMethods

end

included do
include InstanceMethods
end
end


文档中给出了一个使用ActiveSupport::Concern来处理module dependencies的例子,文档rails-3-2-21

module Foo
def self.included(base)
base.class_eval do
def self.method_injected_by_foo
#...
end
end
end
end

module Bar
def self.included(base)
base.method_injected_by_foo
end
end


上述文档中给出的module,想要使用Bar时必须先include Foo,也就是说我们在include module时不得不把目标module的dependencies一一include…

使用ActiveSupport::Concern可以很好的handle这种dependencies,详见上述文档。

查看源码可以更好的帮助理解其实现,源码https://github.com/rails/…

instance_eval

instance_eval的调用者是一个instance,在block中self即是instance本身

,常用来定义单态函数(singleton_methods)。

class_eval

class_eval的调用者是一个类,在block中self即是receiver类本身

,常用来定义实例方法(instance_method)。

Rails4.1 之后,Module还加入了Concerning方法

http://api.rubyonrails.org/v4.1.0/classes/Module/Concerning.html

PS:

今天看到了一句话:代码过多加 module, 职责过多加 scope

好有道理…

可以这样使用scope:

class A < ActiveRecord::Base
scope :as_m do
#...
end
scope :as_l do
def bar
#...
end
end
scope :as_n do
def bar
#...
end
end
end

#scope 里面的方法不会污染别的 scope
A.as_l.bar
A.as_n.bar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ruby-on-rails