Techioz Blog

YARD を使用した Ruby on Rails モデルの文書化

概要

Ruby on Rails アプリケーションのドキュメントに YARD を使用しています。 次のような単純なモデルがあります。

# frozen_string_literal: true

# {Patient} is model responsible for storing patient personal informations.
#
# @!attribute id
#   @return [UUID] ID of the {Patient} in UUID format.
#
# @!attribute name
#   @return [String] Name of the {Patient}.
#
# @!attribute gender
#   @return [String] Gender of the {Patient}.
#
# @!attribute date_of_birth
#   @return [Date] Date of birth of the {Patient}.
#
# @!attribute created_at
#   @return [DateTime] Time when {Patient} was created.
#
# @!attribute updated_at
#   @return [DateTime] Time when {Patient} was updated.

class Patient < ApplicationRecord
  # == Relationships ========================================================
  belongs_to :organization

  # == Validations ==========================================================
  validates :name,          presence: true
  validates :date_of_birth, presence: true
  validates :gender,        inclusion: { in: Constants::GENDERS }
end

YARD との関係と検証をどのように文書化できますか? gem https://github.com/theodorton/yard-activerecord があるのを見ましたが、6年間更新されていませんでした。

解決策

Ruby gem annotate は、データベースに基づいて関数を自動文書化する方法です