Jig*_*hel 3 ruby module include extend mixins
以下是代码段:
模块:ActiveSupport ::关注
module ActiveSupport
module Concern
def self.extended(base)
base.instance_variable_set("@_dependencies", [])
end
def append_features(base)
if base.instance_variable_defined?("@_dependencies")
base.instance_variable_get("@_dependencies") << self
return false
else
return false if base < self
@_dependencies.each { |dep| base.send(:include, dep) }
super
base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
base.send :include, const_get("InstanceMethods") if const_defined?("InstanceMethods")
base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block")
end
end
def included(base = nil, &block)
if base.nil?
@_included_block = block
else
super
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
自定义模块:GeneralScopes
module GeneralScopes
extend ActiveSupport::Concern
included do
scope :reverse_order_by_date, :order => "date DESC"
scope :recent, lambda { |count| reverse_order_by_date.limit(count) }
end
end
Run Code Online (Sandbox Code Playgroud)
自定义类:用户
class User < ActiveRecord::Base
include GeneralScopes
end
Run Code Online (Sandbox Code Playgroud)
用法:
User.recent(5) => Prints the recent five User instances available in database
Run Code Online (Sandbox Code Playgroud)
我在上面显示的代码片段的上下文中有以下问题:
1)包括(MODULE_NAME(s)). include是Module的私有实例方法,此方法由self隐式调用.那么自我在课堂体内代表什么呢?在类的实例方法中,self表示当前对象.在类的类方法中,self表示类的Class对象.
include <MODULE_NAME>类体的这种语法是如何工作的?
2)延长(MODULE_NAME(s))
Object.extend.This方法使指定模块或模块的实例方法成为接收者对象的单例方法.(如果接收者对象是一个Class实例,则接收者的方法成为该类的类方法)
我在上面显示的GeneralScopes模块的上下文中无法理解这一点.
3)
module GeneralScopes
extend ActiveSupport::Concern
included do
scope :reverse_order_by_date, :order => "date DESC"
scope :recent, lambda { |count| reverse_order_by_date.limit(count) }
end
end
Run Code Online (Sandbox Code Playgroud)
模块GeneralScopes我假设是调用ActiveSupport :: Concern模块的实例方法"included(base = nil,&block)".如何从模块体调用实例方法?哪个对象充当接收器时
included do
scope :reverse_order_by_date, :order => "date DESC"
scope :recent, lambda { |count| reverse_order_by_date.limit(count) }
end
Run Code Online (Sandbox Code Playgroud)
执行?
4)ActiveSupport :: Concern模块的实例方法"included(base = nil,&block)"
def included(base = nil, &block)
if base.nil?
@_included_block = block
else
super
end
end
Run Code Online (Sandbox Code Playgroud)
这里使用了super.这个超级工作在User类或ActiveSupport :: Concern模块的上下文中?执行super时控件到哪个类?
如果任何人能让我理解所执行的控制流程,或者指出任何相关资源来解释与我所遇到的问题相关的概念,那将是一个很大的帮助.
1)include使模块的方法可用作类的实例方法,extend而使模块的方法可用作类方法.所以self指的是实例或类:
module M
def foo
puts self
end
end
class A
include M
end
A.new.foo # => #<A:0x007fcefa18e270> ('self' refers to the instance)
class B
extend M
end
B.foo # => B ('self' refers to the class)
Run Code Online (Sandbox Code Playgroud)
2)extend ActiveSupport::Concern使得ActiveSupport::Concern方法可用,GeneralScopes这样你就可以调用included方法.
3)块被分配给@_included_block内部append_features(base.class_eval(&@_included_block))并在其中进行评估,当GeneralScopes用户包含该内容时调用该内容.来自文档:
当这个模块包含在另一个模块中时,Ruby
append_features在这个模块中调用,在mod中传递接收模块.
4)super指ActiveSupport::Concern的父,所以Module的included被调用.
| 归档时间: |
|
| 查看次数: |
3668 次 |
| 最近记录: |