@ledsun blog

Hのキーがhellで、Sのキーがslaveだ、と彼は思った。そしてYのキーがyouだ。

複数クラスの図示に対応する

~ curl -s https://gist.githubusercontent.com/ledsun/76d5485644685f19c98f88000a971d00/raw/68d4730c4e9b3fbf35d352e0a97b094f8b1cd39f/class_diagram.rb | bin/rails runner - Dictionary Association User

こういう感じで引数で複数のクラスを指定したら、次のように3クラスまとめて一つのHTMLとして出力します。

<html>
  <body>
    <script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
    <script>mermaid.initialize({startOnLoad:true});</script>

    <div class="mermaid">

classDiagram
  class Dictionary {
   integer id
    string name
      text description
   integer user_id
   integer entries_num
  datetime created_at
  datetime updated_at
   boolean public
    string license
    string license_url
      text no_term_words
      text no_begin_words
      text no_end_words
   integer tokens_len_min
   integer tokens_len_max
     float threshold
    string language
  add_entries(hentries)
  additional_entries()
  administrable?(user)
  autosave_associated_records_for_associated_managers()
  autosave_associated_records_for_associations()
  autosave_associated_records_for_entries()
  autosave_associated_records_for_jobs()
  autosave_associated_records_for_user()
  compilable?()
  compile!()
  create_a_black_entry(entry)
  editable?(user)
  empty_entries()
  narrow_entries_by_identifier(str)
  narrow_entries_by_label(str)
  narrow_entries_by_label_prefix(str)
  narrow_entries_by_label_prefix_and_substring(str)
  new_entry(label, identifier)
  num_black()
  num_gray()
  num_white()
  search_term(ssdb, term)
  sim_string_db_dir()
  sim_string_db_path()
  simstring_method()
  tmp_sim_string_db_path()
  to_param()
  undo_entry(entry)
  update_stop_words()
  validate_associated_records_for_associated_managers()
  validate_associated_records_for_associations()
  validate_associated_records_for_entries()
  validate_associated_records_for_jobs()
}

  class Association {
   integer id
   integer user_id
   integer dictionary_id
  datetime created_at
  datetime updated_at
  autosave_associated_records_for_dictionary()
  autosave_associated_records_for_user()
}

  class User {
   integer id
      text username
    string email
    string encrypted_password
    string reset_password_token
  datetime reset_password_sent_at
  datetime remember_created_at
   integer sign_in_count
  datetime current_sign_in_at
  datetime last_sign_in_at
    string current_sign_in_ip
    string last_sign_in_ip
  datetime created_at
  datetime updated_at
   boolean admin
    string confirmation_token
  datetime confirmed_at
  datetime confirmation_sent_at
    string unconfirmed_email
  autosave_associated_records_for_associated_dictionaries()
  autosave_associated_records_for_associations()
  autosave_associated_records_for_dictionaries()
  current_password()
  devise_modules()
  devise_modules?()
  password()
  password_confirmation()
  password_confirmation=()
  validate_associated_records_for_associated_dictionaries()
  validate_associated_records_for_associations()
  validate_associated_records_for_dictionaries()
}

  User --* Dictionary
  Dictionary --* Association
  Dictionary --* AssociatedManager
  Dictionary --* Entry
  Dictionary --* Job
  User --* Association
  User --* AssociatedDictionary

    </div>
  </body>
</html>

こんな見た目です。

f:id:ledsun:20210613015136p:plain
3つのクラスを描画したクラス図

リレーションが重複していると、重複して描画されるので、ユニークする必要がありました。