Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Documenting methods created with meta-programming using YARD

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?

like image 668
Aliou Avatar asked Oct 30 '22 21:10

Aliou


1 Answers

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.

like image 164
tzzzoz Avatar answered Nov 15 '22 06:11

tzzzoz