Documenting methods created with meta-programming using YARD
Asked Answered
J

1

6

I'm currently working on a gem and writing documentation for it. I currently have a class that has several method defined using defined_method as follows:

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

I'm trying to document these methods using YARD, but when generating the documentation of the project, theses methods do not appear in the class documentation.

Does anyone know how I could document these? Am I missing something?

Johnnajohnnie answered 1/9, 2016 at 8:48 Comment(2)
not a yard expert, but having looked at their docs i dont think there's a way to do this other than writing a freeform comment. Besides, if the methods are truly dynamic, you won't be able to document them accurately. If you want to document one and two methods, make them their own methods. Save define_method for when it's needed.Romp
The actual methods are not exactly as the example above but I see your point about freeform comment.Johnnajohnnie
G
2

Instead of iterating an arbitrary list, you would generally use macros to define the methods by wrapping the dynamic behaviour into a class method that can be documented as DSL-style calls in your class:

  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

Hope it works for you.

Gama answered 1/9, 2016 at 9:6 Comment(1)
Will try this. Thank youJohnnajohnnie

© 2022 - 2024 — McMap. All rights reserved.