Yard doc and dynamic assignment of constants, instance, and class methods
Asked Answered
I

0

7

I have a module I include to help define certain methods, constants and class methods. It is creating them all dynamically. I need help understanding how I can use Yard Doc's @macro feature to align the documentation and set the instance, class, and constant documentation all from inside an included module.

At its basic, it looks like this:

module Constantizer
  extend ActiveSupport::Concern

  # @!macro [attach] define_constant
  #   @method $1?
  #   Is this a $1?
  #   @return [Boolean]

  module ClassMethods

    def define_constant(label, value, description)
      const_set(label.to_s.upcase, value)
      define_method("#{label}?") { self.id == value }
      define_singleton_method(label) { find(value) }
    end

  end
end

class MyRecord < ActiveRecord::Base
  include Constantizer 
  # @!parse include Constantizer

  attr_accessor :label, :id

  define_constant :one, 1, 'the number one'
  define_constant :two, 2, 'the number two'
  # ... etc ...
end

This define_constant creates an instance methods, one constant, and a class method.

MyRecord::ONE #=> 1
MyRecord.one  #=> MyRecord.find(1)
MyRecord#one? #=> true || false

help with yard

I found this answer on SO as well as this post and I can get this to sort of work at the instance method level by what you see, above. However, I can't get the class method or the constant on the class to document. I tried adding the same kind of thing inside of ClassMethods but it was using label instead of the value passed to define_constant.

Kind of just copying pasting and trying to guess so I thought I would ask.

Indeliberate answered 19/12, 2019 at 19:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.