activerecord_uml 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2cc6a981ab5f550073e505fb2192a116251d261699b0ebea985fee66644daddf
4
- data.tar.gz: e20fd64c002bb27f63877bcfb5312033b0f3976d442234f181ea84063169415a
3
+ metadata.gz: 50ec6f2cb089ba466d1fff00c4b69faf94d86a6760b7099e76afe0452c596b58
4
+ data.tar.gz: 6f79579599c6f8d19ac3a5cf3d438b746f6be552e2e76a3ca6224ada2a775df3
5
5
  SHA512:
6
- metadata.gz: 92e36b86b5fa17bf477d1f42f14d003e886fc2c9690e81d5131f3dafdc0a0e4c6ddd93c975ce23087af6ef5567e3f8267e889d57220f8def355392a566f9e446
7
- data.tar.gz: 48a54fdf7c0ba6731ed425faf868c2eddc44a1406bf305d58eabd00cebcf5bd4ef66acc1ebbfb875e15a540c7171f3fff3c2aa92ad8a8a739ebb4a372150b951
6
+ metadata.gz: cc65efc94125046d0c409ec50cbbaa2c5df6f9568309aad0b6dca01d2648d960df02fee6fb2c93cc2a5c4ece631b5714679b8db4cbd48331b9baab16e277d803
7
+ data.tar.gz: 055b92dd7b195376926167b7e9be35e074c9bf36cb342469ad8903a8846dcde2991168ad7e005354198dbe0a7f5ca1484c14e8417ebc6c216f51c79603551777
@@ -1,8 +1,8 @@
1
1
  #! /usr/bin/env ruby
2
2
 
3
3
  activerecord_uml = File.join __dir__, "../lib/activerecord_uml"
4
- ruby_script = "require \"#{activerecord_uml}\"; ActiverecordUml.draw true"
4
+ ruby_script = "require \"#{activerecord_uml}\"; ActiverecordUml.draw"
5
5
  rails = File.join Dir.pwd, "bin/rails"
6
- rails_runner_command = "echo '#{ruby_script}' | #{rails} runner - #{ARGV.join " "}"
6
+ rails_runner_command = "echo '#{ruby_script}' | #{rails} runner - --relation-only #{ARGV.join " "}"
7
7
 
8
8
  system rails_runner_command
@@ -1,38 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
  require "erb"
3
3
  require_relative "activerecord_uml/version"
4
- require_relative "activerecord_uml/diagram_drawer"
4
+ require_relative "activerecord_uml/activerecord_uml"
5
5
 
6
6
  module ActiverecordUml
7
- def self.draw(relation_only = false)
8
- html = <<EOF
9
- <html>
10
- <body>
11
- <script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
12
- <script>mermaid.initialize({startOnLoad:true});</script>
13
-
14
- <div class="mermaid">
15
-
16
- classDiagram
17
- <% class_diagrams.each do |d| %>
18
- <%= d %>
19
- <% end %>
20
- <% relations.each do |r| %>
21
- <%= r %>
22
- <% end %>
23
-
24
- </div>
25
- </body>
26
- </html>
27
- EOF
28
-
29
- html_template = ERB.new html, nil, "<>"
30
- classes = ARGV.map { |model_name| DiagramDrawer.new(model_name) }
31
- puts html_template.result_with_hash class_diagrams: relation_only ? [] : classes.map { |c| c.class_diagram },
32
- relations: classes.map { |c| c.relations }
33
- .flatten
34
- .select { |r| relation_only ? r.belongs_to?(ARGV) : true }
35
- .map(&:to_s)
36
- .uniq
7
+ def self.draw
8
+ ActiverecordUml.new(ARGV).draw
37
9
  end
38
10
  end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+ require "erb"
3
+ require_relative "class_drawer"
4
+ require_relative "relation_drawer"
5
+
6
+ module ActiverecordUml
7
+ class ActiverecordUml
8
+ def initialize(args)
9
+ @args = args
10
+ end
11
+
12
+ def draw
13
+ puts html_template.result_with_hash classes: classes,
14
+ relations: relations
15
+ end
16
+
17
+ private
18
+
19
+ def classes
20
+ options.include?(:relation_only) ? [] : target_classes.map { |klass| ClassDrawer.new(klass).diagram }
21
+ end
22
+
23
+ def relations
24
+ target_classes.map { |c| RelationDrawer.new(c).relations }
25
+ .flatten
26
+ .select { |r| options.include?(:relation_only) ? r.belongs_to?(target_classes.map(&:name)) : true }
27
+ .map(&:to_s)
28
+ .uniq
29
+ end
30
+
31
+ def target_classes
32
+ @args.select { |arg| !arg.start_with?("--") }
33
+ .map { |model_name| Object.const_get model_name }
34
+ end
35
+
36
+ def options
37
+ Set.new @args.select { |arg| arg.start_with?("--") }
38
+ .map { |arg| arg.gsub(/^--/, "").tr("-", "_").to_sym }
39
+ end
40
+
41
+ def html_template
42
+ html = <<EOF
43
+ <html>
44
+ <body>
45
+ <script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
46
+ <script>mermaid.initialize({startOnLoad:true});</script>
47
+
48
+ <div class="mermaid">
49
+
50
+ classDiagram
51
+ <% classes.each do |d| %>
52
+ <%= d %>
53
+ <% end %>
54
+ <% relations.each do |r| %>
55
+ <%= r %>
56
+ <% end %>
57
+
58
+ </div>
59
+ </body>
60
+ </html>
61
+ EOF
62
+
63
+ ERB.new html, nil, "<>"
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,45 @@
1
+ require "erb"
2
+
3
+ module ActiverecordUml
4
+ class ClassDrawer
5
+ CLASS_TEMPLATE = <<EOF
6
+ class <%= klass.name %> {
7
+ <% klass.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(klass)
17
+ @klass = klass
18
+ end
19
+
20
+ def diagram
21
+ class_template.result_with_hash klass: @klass, methods: methods
22
+ end
23
+
24
+ private
25
+
26
+ def class_template
27
+ ERB.new CLASS_TEMPLATE, nil, "<>"
28
+ end
29
+
30
+ def methods
31
+ @klass.public_instance_methods(false).sort.map do |m|
32
+ method_parameters = @klass.new.method(m)
33
+ .parameters
34
+ .filter { |a| a[0] == :req }
35
+ .map { |a| a[1] }
36
+
37
+ if method_parameters.length > 3
38
+ method_parameters = method_parameters.slice(0, 3).append("...")
39
+ end
40
+
41
+ [m.to_s, method_parameters.join(", ")]
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,29 @@
1
+ require "erb"
2
+ require_relative "./relation.rb"
3
+
4
+ module ActiverecordUml
5
+ class RelationDrawer
6
+ def initialize(klass)
7
+ @klass = klass
8
+ end
9
+
10
+ def relations
11
+ @klass.reflect_on_all_associations(:belongs_to).map do |a|
12
+ Relation.new(a.class_name, "--*", @klass, a)
13
+ end.concat(@klass.reflect_on_all_associations(:has_many).map do |a|
14
+ if a.is_a? ActiveRecord::Reflection::ThroughReflection
15
+ if a.source_reflection
16
+ # If a source association is specified, the class name of the source association is displayed.
17
+ Relation.new(@klass, "*--*", a.source_reflection.class_name, a)
18
+ else
19
+ Relation.new(@klass, "*--*", a.class_name, a)
20
+ end
21
+ else
22
+ Relation.new(@klass, "--*", a.class_name, a)
23
+ end
24
+ end).concat(@klass.reflect_on_all_associations(:has_one).map do |a|
25
+ Relation.new(@klass, "--*", a.class_name, a)
26
+ end)
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiverecordUml
4
- VERSION = "0.13.0"
4
+ VERSION = "0.14.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_uml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - shigeru.nakajima
@@ -30,8 +30,10 @@ files:
30
30
  - exe/activerecord_relations
31
31
  - exe/activerecord_uml
32
32
  - lib/activerecord_uml.rb
33
- - lib/activerecord_uml/diagram_drawer.rb
33
+ - lib/activerecord_uml/activerecord_uml.rb
34
+ - lib/activerecord_uml/class_drawer.rb
34
35
  - lib/activerecord_uml/relation.rb
36
+ - lib/activerecord_uml/relation_drawer.rb
35
37
  - lib/activerecord_uml/version.rb
36
38
  homepage: https://github.com/ledsun/activerecord_uml
37
39
  licenses: []
@@ -1,70 +0,0 @@
1
- require "erb"
2
- require_relative "./relation.rb"
3
-
4
- module ActiverecordUml
5
- class DiagramDrawer
6
- CLASS_TEMPLATE = <<EOF
7
- class <%= class_name %> {
8
- <% class_name.columns.each do |c| %>
9
- <%= sprintf("%8s %s", c.type, c.name) %>
10
- <% end %>
11
- <% methods.each do |m, parameters| %>
12
- <%= sprintf("%s(%s)", m, parameters) %>
13
- <% end %>
14
- }
15
- EOF
16
-
17
- def initialize(model_name)
18
- begin
19
- @class_name = Object.const_get model_name
20
- rescue NameError
21
- STDERR.puts "#{model_name}というクラスがみつかりません"
22
- raise
23
- end
24
- end
25
-
26
- def class_diagram
27
- class_template.result_with_hash class_name: @class_name, methods: methods
28
- end
29
-
30
- def relations
31
- @class_name.reflect_on_all_associations(:belongs_to).map do |a|
32
- Relation.new(a.class_name, "--*", @class_name, a)
33
- end.concat(@class_name.reflect_on_all_associations(:has_many).map do |a|
34
- if a.is_a? ActiveRecord::Reflection::ThroughReflection
35
- if a.source_reflection
36
- # If a source association is specified, the class name of the source association is displayed.
37
- Relation.new(@class_name, "*--*", a.source_reflection.class_name, a)
38
- else
39
- Relation.new(@class_name, "*--*", a.class_name, a)
40
- end
41
- else
42
- Relation.new(@class_name, "--*", a.class_name, a)
43
- end
44
- end).concat(@class_name.reflect_on_all_associations(:has_one).map do |a|
45
- Relation.new(@class_name, "--*", a.class_name, a)
46
- end)
47
- end
48
-
49
- private
50
-
51
- def class_template
52
- ERB.new CLASS_TEMPLATE, nil, "<>"
53
- end
54
-
55
- def methods
56
- @class_name.public_instance_methods(false).sort.map do |m|
57
- method_parameters = @class_name.new.method(m)
58
- .parameters
59
- .filter { |a| a[0] == :req }
60
- .map { |a| a[1] }
61
-
62
- if method_parameters.length > 3
63
- method_parameters = method_parameters.slice(0, 3).append("...")
64
- end
65
-
66
- [m.to_s, method_parameters.join(", ")]
67
- end
68
- end
69
- end
70
- end