Ruby 码⇒;如何基于YARD plugin中的一条DSL语句向多个方法描述添加自定义输出
我在为以下DSL编写YARD插件时遇到问题:Ruby 码⇒;如何基于YARD plugin中的一条DSL语句向多个方法描述添加自定义输出,ruby,yard,Ruby,Yard,我在为以下DSL编写YARD插件时遇到问题: class MyClass my_dsl :do_it, :do_other def do_it # do it end def do_other # do other end def do_unrelated # do unrelated end end 现在,我想向这些方法的文档片段添加注释,它们受到DSLmy_DSL的“影响”。在my_dsl处理程序中,范围完全不同(我不想添加一些新文档,我
class MyClass
my_dsl :do_it, :do_other
def do_it
# do it
end
def do_other
# do other
end
def do_unrelated
# do unrelated
end
end
现在,我想向这些方法的文档片段添加注释,它们受到DSLmy_DSL
的“影响”。在my_dsl
处理程序中,范围完全不同(我不想添加一些新文档,我想扩展已有的for方法。)
因此,我决定在MyDSLHandler\process
中使用code对象来标记延迟的未来后处理所需的方法(这将发生在def do\u it;上的内置MethodHandler
中)
class MyDSLHandler < YARD::Handlers::Ruby::DSLHandler
handles method_call(:my_dsl)
namespace_only
def process
statement.parameters.each { |astnode|
mthd = astnode.jump(:string_content).source if astnode.respond_to? :jump
obj = YARD::CodeObjects::Proxy.new(namespace, "\##{mthd}", :method)
register(obj)
obj[:my_dsl_params] << {
name: mthd,
file: statement.file, line: statement.line # I need this!
}
}
end
end
我应该如何将当前上下文中的一些值存储到代理
对象中,以便它们在对象实际实例化期间可用?好吧,我能够解决它
此处不应使用代理。CodeObject
s有一个非常独特的特性:它们是“沉默的”单例。因此,当您通过CodeObject.new
定义对象时,同一对象上所有即将调用的new
(根据Registry
)都将重新映射到现有对象
因此,在我的例子中,我只需创建MethodObject
:
m = "#{astnode.jump(:string_content).source[1..-1]}";
obj = YARD::CodeObjects::MethodObject.new(namespace, "#{m}")
然后我在新创建的对象上执行我想要的操作:
obj.my_dsl_params = { :win => true, :mthd => "#{m}", … }
方法定义解析中的对象将将这些属性与其自身属性合并。只需使用register(obj)
注册一个对象,或者从process
方法返回该对象以进行系统注册
obj.my_dsl_params = { :win => true, :mthd => "#{m}", … }