mergen 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '00139ca81d6776ed67f74a55665915eeb5c342231164b0a35fdb89dca6e0f9f2'
4
+ data.tar.gz: 97379884ccde6d2d4d7ac38f46e3d99e7e236d59aa3b50b4efe3e543eab828a4
5
+ SHA512:
6
+ metadata.gz: e0b5b66167b69d7f300c720ff0cf384d2d786ad4d442b77300de75e7213c5c047142f8cf8c7d7ca1883acada8435cfcbb0c29c1e76530c57282d9b3301173d25
7
+ data.tar.gz: ae1628bd499d6c942d3f0d0018d8b5f2dffa7b431d74dfa97224b9d77576447baedc55aaa7cd275729075a17b27c5875b06895a9084669a609049a245ff6a05e
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-04-12
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Yudai Takada
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Mergen
2
+
3
+ A tool to create mermaid class diagrams from Ruby code.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ $ gem install mergen
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### From Command Line
14
+
15
+ ```bash
16
+ $ mergen path/to/ruby/files -o diagram.md
17
+ ```
18
+
19
+ ### From Ruby Code
20
+
21
+ ```ruby
22
+ require 'mergen'
23
+
24
+ generator = Mergen::Generator.new('path/to/ruby/files')
25
+ diagram = generator.generate
26
+
27
+ # Display the output
28
+ puts diagram
29
+
30
+ # Or save to a file
31
+ File.write('diagram.md', diagram)
32
+ ```
33
+
34
+ ## Output Example
35
+
36
+ The generated class diagram in Mermaid syntax will look like this:
37
+
38
+ ```
39
+ classDiagram
40
+ class Person {
41
+ + name : property
42
+ + age : property
43
+ + initialize(name, age)
44
+ + greet()
45
+ }
46
+
47
+ class Employee {
48
+ + department : property
49
+ + position : property
50
+ + work()
51
+ }
52
+
53
+ Person <|-- Employee
54
+ ```
55
+
56
+ ## License
57
+
58
+ Distributed under the [MIT License](LICENSE.txt).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ end
8
+
9
+ task default: %i[spec]
data/exe/mergen ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH << File.join(__dir__, '../lib')
5
+ require 'mergen'
6
+ require 'optparse'
7
+
8
+ options = {}
9
+ parser = OptionParser.new do |opts|
10
+ opts.banner = 'Usage: mergen [options] PATH'
11
+
12
+ opts.on('-o', '--output FILE', 'output file') do |file|
13
+ options[:output] = file
14
+ end
15
+
16
+ opts.on('-h', '--help', 'show help message') do
17
+ puts opts
18
+ exit
19
+ end
20
+ end
21
+
22
+ begin
23
+ parser.parse!
24
+
25
+ if ARGV.empty?
26
+ puts 'error: PATH not specified'
27
+ puts parser
28
+ exit(1)
29
+ end
30
+
31
+ path = ARGV[0]
32
+
33
+ generator = Mergen::Generator.new(path)
34
+ diagram = generator.generate
35
+
36
+ if options[:output]
37
+ File.write(options[:output], diagram)
38
+ puts "class diagram generated in #{options[:output]}"
39
+ else
40
+ puts diagram
41
+ end
42
+ rescue StandardError => e
43
+ puts "error: #{e.message}"
44
+ exit(1)
45
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mergen
4
+ class Diagram
5
+ attr_reader :name, :type, :parent_class, :included_modules, :methods, :attributes
6
+
7
+ def initialize(name, type = :class)
8
+ @name = name
9
+ @type = type
10
+ @parent_class = nil
11
+ @included_modules = []
12
+ @methods = []
13
+ @attributes = []
14
+ end
15
+
16
+ def set_parent_class(parent_class)
17
+ @parent_class = parent_class
18
+ end
19
+
20
+ def add_included_module(module_name)
21
+ @included_modules << module_name
22
+ end
23
+
24
+ def add_method(method_name, visibility = :public)
25
+ @methods << { name: method_name, visibility: visibility }
26
+ end
27
+
28
+ def add_attribute(attr_name, visibility = :public, type = :attr_accessor)
29
+ @attributes << { name: attr_name, visibility: visibility, type: type }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mergen
4
+ class DiagramGenerator
5
+ def initialize(diagrams)
6
+ @diagrams = diagrams
7
+ end
8
+
9
+ def generate
10
+ lines = ['classDiagram']
11
+
12
+ @diagrams.each do |info|
13
+ if info.type == :module
14
+ lines << " class #{info.name} {\n <<module>>\n }"
15
+ else
16
+ lines << " class #{info.name} {"
17
+
18
+ info.attributes.each do |attr|
19
+ visibility = case attr[:visibility]
20
+ when :private then '-'
21
+ when :protected then '#'
22
+ else '+'
23
+ end
24
+
25
+ attr_type = case attr[:type]
26
+ when :attr_reader then 'getter'
27
+ when :attr_writer then 'setter'
28
+ else 'property'
29
+ end
30
+
31
+ lines << " #{visibility} #{attr[:name]} : #{attr_type}"
32
+ end
33
+
34
+ info.methods.each do |method|
35
+ visibility = case method[:visibility]
36
+ when :private then '-'
37
+ when :protected then '#'
38
+ else '+'
39
+ end
40
+
41
+ lines << " #{visibility} #{method[:name]}()"
42
+ end
43
+
44
+ lines << ' }'
45
+ end
46
+
47
+ lines << " #{info.parent_class} <|-- #{info.name}" if info.parent_class
48
+
49
+ info.included_modules.each do |mod|
50
+ lines << " #{mod} <|.. #{info.name} : includes"
51
+ end
52
+ end
53
+
54
+ lines.join("\n")
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mergen
4
+ class Parser
5
+ def parse(content)
6
+ res = []
7
+ current_class = nil
8
+ current_visibility = :public
9
+
10
+ content.each_line do |line|
11
+ line = line.strip
12
+ if line =~ /^class\s+([A-Z][A-Za-z0-9_]*)\s*(<\s*([A-Z][A-Za-z0-9_:]*))?\s*$/
13
+ class_name = ::Regexp.last_match(1)
14
+ parent_class = ::Regexp.last_match(3)
15
+ current_class = Diagram.new(class_name)
16
+ current_class.set_parent_class(parent_class) if parent_class
17
+ res << current_class
18
+ current_visibility = :public
19
+ elsif line =~ /^module\s+([A-Z][A-Za-z0-9_]*)\s*$/
20
+ module_name = ::Regexp.last_match(1)
21
+ current_class = Diagram.new(module_name, :module)
22
+ res << current_class
23
+ current_visibility = :public
24
+ elsif current_class && line =~ /^\s*include\s+([A-Z][A-Za-z0-9_:]*)\s*$/
25
+ current_class.add_included_module(::Regexp.last_match(1))
26
+ elsif current_class && line =~ /^\s*def\s+([a-z_][a-zA-Z0-9_]*[?!=]?)/
27
+ current_class.add_method(::Regexp.last_match(1), current_visibility)
28
+ elsif current_class && line =~ /^\s*(attr_accessor|attr_reader|attr_writer)\s+(:[a-z_][a-zA-Z0-9_]*(?:\s*,\s*:[a-z_][a-zA-Z0-9_]*)*)/
29
+ attr_type = ::Regexp.last_match(1).to_sym
30
+ attr_list = ::Regexp.last_match(2)
31
+ attr_list.scan(/:[a-z_][a-zA-Z0-9_]*/).each do |attr|
32
+ attr_name = attr.sub(/^:/, '')
33
+ current_class.add_attribute(attr_name, current_visibility, attr_type)
34
+ end
35
+ elsif current_class && line =~ /^\s*(private|protected|public)\s*$/
36
+ current_visibility = ::Regexp.last_match(1).to_sym
37
+ end
38
+ end
39
+ res
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mergen
4
+ VERSION = '0.1.0'
5
+ end
data/lib/mergen.rb ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mergen/version'
4
+ require 'mergen/parser'
5
+ require 'mergen/diagram'
6
+ require 'mergen/diagram_generator'
7
+
8
+ module Mergen
9
+ class Generator
10
+ def initialize(path)
11
+ @path = path
12
+ @parser = Parser.new
13
+ end
14
+
15
+ def generate
16
+ diagrams = []
17
+ targets(@path).each do |file|
18
+ file_content = File.read(file)
19
+ diagrams.concat(@parser.parse(file_content))
20
+ end
21
+
22
+ DiagramGenerator.new(diagrams).generate
23
+ end
24
+
25
+ private
26
+
27
+ def targets(path)
28
+ if File.directory?(path)
29
+ Dir.glob(File.join(path, '**', '*.rb'))
30
+ elsif File.file?(path) && path.end_with?('.rb')
31
+ [path]
32
+ else
33
+ []
34
+ end
35
+ end
36
+ end
37
+ end
data/sig/mergen.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Mergen
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mergen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yudai Takada
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A tool to create mermaid class diagrams from Ruby code.
13
+ email:
14
+ - t.yudai92@gmail.com
15
+ executables:
16
+ - mergen
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - exe/mergen
27
+ - lib/mergen.rb
28
+ - lib/mergen/diagram.rb
29
+ - lib/mergen/diagram_generator.rb
30
+ - lib/mergen/parser.rb
31
+ - lib/mergen/version.rb
32
+ - sig/mergen.rbs
33
+ homepage: https://github.com/ydah/mergen
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ homepage_uri: https://github.com/ydah/mergen
38
+ source_code_uri: https://github.com/ydah/mergen
39
+ changelog_uri: https://github.com/ydah/mergen/CHANGELOG.md
40
+ rubygems_mfa_required: 'true'
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 3.1.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.7.0.dev
56
+ specification_version: 4
57
+ summary: A tool to create mermaid class diagrams from Ruby code.
58
+ test_files: []