activerecord_uml 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 75bc29669bc6893589083ea59d6dcb6699a53165b3258a008ed69fce1f907c7d
4
+ data.tar.gz: 8fa743ad797eef029abd1d347654f51589c9b575bced44735ea62752e7b906cc
5
+ SHA512:
6
+ metadata.gz: c026ab1b8b5de2630e49ec544c73693d157cee869c0b8995109e877611a6522d5f48f703dd5d0eb08fb74cf094454a887ba2511353d67837894fa3edf8bed62c
7
+ data.tar.gz: 6c08b058547c1f8010fefb5048653ff462d47618689b722b23db679e3b1cc375525a1323d77f0ee06936f84e214c55d8f887bae19d15a31a3bd4fd9c6823dfaf
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+ /.idea
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in activerecord_uml.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # ActiverecordUml
2
+
3
+ ActiverecordUml draws class diagrams of ActiveRecord models.
4
+
5
+ We sometimes wander in the existing Ruby on Rails application.
6
+ All models are too much to see.
7
+ We want to select from three to seven models to see their attributes and relations.
8
+
9
+ ActiverecordUml draws class diagrams of selected models to help to recognize their attributes and relations.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'activerecord_uml'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install activerecord_uml
26
+
27
+ ## Usage
28
+
29
+ In your Ruby on Rails directory:
30
+
31
+ ```
32
+ activerecord_uml User
33
+ ```
34
+
35
+ Execute the activerecord_uml command with name of model classes.
36
+ Then the activerecord_uml outputs HTML text includes class diagrams of specified model classes.
37
+
38
+ For MacOs, I recommend to use the activerecord_uml with [browser](https://gist.github.com/defunkt/318247) command.
39
+ For example:
40
+
41
+ Install browser with Homebrew.
42
+
43
+ ```
44
+ $ brew install browser
45
+ ```
46
+
47
+ And then execute:
48
+
49
+ ```
50
+ activerecord_uml User | browser
51
+ ```
52
+
53
+ Open the class diagrams with the browser immediately.
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake ` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ledsun/activerecord_uml.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/activerecord_uml/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "activerecord_uml"
7
+ spec.version = ActiverecordUml::VERSION
8
+ spec.authors = ["shigeru.nakajima"]
9
+ spec.email = ["shigeru.nakajima@gmail.com"]
10
+
11
+ spec.summary = "Generate UML class diagram from ActiveRecord instances."
12
+ spec.description = "Generate UML class diagram for Mermaid.js from ActiveRecord instances by Rails runner."
13
+ spec.homepage = "https://github.com/ledsun/activerecord_uml"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/ledsun/activerecord_uml"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ # spec.add_dependency "example-gem", "~> 1.0"
32
+
33
+ # For more information and examples about making a new gem, checkout our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "activerecord_uml"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ #! /usr/bin/env ruby
2
+ require "activerecord_uml"
3
+
4
+ require(File.join(Dir.pwd, "config/application"))
5
+ Rails.application.require_environment!
6
+
7
+ ActiverecordUml.draw
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+ require_relative "activerecord_uml/version"
3
+ require_relative "activerecord_uml/diagram_drawer"
4
+
5
+ module ActiverecordUml
6
+ def self.draw
7
+ html = <<EOF
8
+ <html>
9
+ <body>
10
+ <script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
11
+ <script>mermaid.initialize({startOnLoad:true});</script>
12
+
13
+ <div class="mermaid">
14
+
15
+ classDiagram
16
+ <% class_diagrams.each do |d| %>
17
+ <%= d %>
18
+ <% end %>
19
+ <% relations.each do |r| %>
20
+ <%= r %>
21
+ <% end %>
22
+
23
+ </div>
24
+ </body>
25
+ </html>
26
+ EOF
27
+
28
+ html_template = ERB.new html, nil, "<>"
29
+ classes = ARGV.map { |model_name| DiagramDrawer.new(model_name) }
30
+ puts html_template.result_with_hash class_diagrams: classes.map { |c| c.class_diagram },
31
+ relations: classes.map { |c| c.relations }.flatten.uniq
32
+ end
33
+ end
@@ -0,0 +1,55 @@
1
+ require "erb"
2
+
3
+ module ActiverecordUml
4
+ class DiagramDrawer
5
+ CLASS_TEMPLATE = <<EOF
6
+ class <%= class_name %> {
7
+ <% class_name.columns.each do |c| %>
8
+ <%= sprintf("%8s %s", c.type, c.name) %>
9
+ <% end %>
10
+ <% methods.each do |m, parameters| %>
11
+ <%= sprintf("%s(%s)", m, parameters) %>
12
+ <% end %>
13
+ }
14
+ EOF
15
+
16
+ def initialize(model_name)
17
+ begin
18
+ @class_name = Object.const_get model_name
19
+ rescue NameError
20
+ STDERR.puts "#{model_name}というクラスがみつかりません"
21
+ raise
22
+ end
23
+ end
24
+
25
+ def class_diagram
26
+ class_template.result_with_hash class_name: @class_name, methods: methods
27
+ end
28
+
29
+ def relations
30
+ @class_name.reflect_on_all_associations(:belongs_to).map do |a|
31
+ "#{a.name.to_s.classify} --* #{@class_name}"
32
+ end.concat(@class_name.reflect_on_all_associations(:has_many).map do |a|
33
+ "#{@class_name} --* #{a.name.to_s.classify}"
34
+ end).concat(@class_name.reflect_on_all_associations(:has_one).map do |a|
35
+ "#{@class_name} --* #{a.name.to_s.classify}"
36
+ end)
37
+ end
38
+
39
+ private
40
+
41
+ def class_template
42
+ ERB.new CLASS_TEMPLATE, nil, "<>"
43
+ end
44
+
45
+ def methods
46
+ @class_name.public_instance_methods(false).sort.map do |m|
47
+ method_parameters = @class_name.new.method(m)
48
+ .parameters
49
+ .filter { |a| a[0] == :req }
50
+ .map { |a| a[1] }.join(", ")
51
+ [m.to_s, method_parameters]
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiverecordUml
4
+ VERSION = "0.5.0"
5
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_uml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - shigeru.nakajima
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-06-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Generate UML class diagram for Mermaid.js from ActiveRecord instances
14
+ by Rails runner.
15
+ email:
16
+ - shigeru.nakajima@gmail.com
17
+ executables:
18
+ - activerecord_uml
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".gitignore"
23
+ - Gemfile
24
+ - README.md
25
+ - Rakefile
26
+ - activerecord_uml.gemspec
27
+ - bin/console
28
+ - bin/setup
29
+ - exe/activerecord_uml
30
+ - lib/activerecord_uml.rb
31
+ - lib/activerecord_uml/diagram_drawer.rb
32
+ - lib/activerecord_uml/version.rb
33
+ homepage: https://github.com/ledsun/activerecord_uml
34
+ licenses: []
35
+ metadata:
36
+ allowed_push_host: https://rubygems.org
37
+ homepage_uri: https://github.com/ledsun/activerecord_uml
38
+ source_code_uri: https://github.com/ledsun/activerecord_uml
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.4.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.2.15
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Generate UML class diagram from ActiveRecord instances.
58
+ test_files: []