使用 YARD 记录元编程创建的方法

Ali*_*iou 5 ruby documentation metaprogramming yard

我目前正在研究 gem 并为其编写文档。我目前有一个类,其中定义了几种方法defined_method,如下所示:

class Client
  ['one', 'two'].each do |method_name|
    # Sets the value
    # @param argument the argument to set.
    define_method("set_#{method_name}") do |argument|
      # Method content
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 YARD 记录这些方法,但是在生成项目的文档时,这些方法不会出现在类文档中。

有谁知道我如何记录这些?我错过了什么吗?

tzz*_*zoz 2

您通常不会迭代任意列表,而是使用宏来定义方法,方法是将动态行为包装到类方法中,该方法可以在类中记录为 DSL 样式调用:

  class << self
    private
    # @macro [attach] container.increment
    #   @method $1()
    #   Increment the $1 container.
    def make(name)
      define_method(name) { container.send(name).increment }
    end
  end

  make :lion
  make :pigeon
end
Run Code Online (Sandbox Code Playgroud)

希望对你有帮助。