我在为以下 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
现在我想为这些方法的文档片段添加注释,这些片段受 DSL “影响” my_dsl
。在my_dsl
处理程序中,范围完全不同(我不想添加一些新文档,我想扩展已经存在的方法。)
所以我决定在里面使用Proxy代码对象MyDSLHandler#process
来标记延迟未来后处理所需的方法(这将发生在内置MethodHandler
on 中def do_it ; … ; end
。)它看起来像:
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
现在的问题是该Proxy
对象是从 plain 派生的Object
,而不是从YARD::CodeObjects::Base
所以没有[]=
定义方法:
[warn]: Load Order / Name Resolution Problem on MyClass#do_it:
[warn]: -
[warn]: Something is trying to call [] on object MyClass#do_it before it has been recognized.
[warn]: This error usually means that you need to modify the order in which you parse files
[warn]: so that MyClass#do_it is parsed before methods or other objects attempt to access it.
[warn]: -
[warn]: YARD will recover from this error and continue to parse but you *may* have problems
[warn]: with your generated documentation. You should probably fix this.
[warn]: -
[error]: Unhandled exception in Yard::Handlers::MyDSLHandler:
[error]: in `example_mydsl.rb`:5:
5: my_dsl :do_it, :do_other
[error]: ProxyMethodError: Proxy cannot call method #[] on object 'MyClass#do_it'
我应该如何将一些值从当前上下文存储到Proxy
对象,以便它们在对象实际实例化期间可用?