annotate_model 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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +33 -0
- data/exe/annotate_model +7 -0
- data/lib/annotate_model/annotation_decider.rb +25 -0
- data/lib/annotate_model/annotator.rb +79 -0
- data/lib/annotate_model/cli.rb +25 -0
- data/lib/annotate_model/finder.rb +22 -0
- data/lib/annotate_model/model_class_getter.rb +23 -0
- data/lib/annotate_model/model_file_not_found_error.rb +7 -0
- data/lib/annotate_model/version.rb +5 -0
- data/lib/annotate_model.rb +7 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7cbc5209bfbb4f497c00889bc95d34144d8e60e628f2495ffa04e36b14cebccc
|
4
|
+
data.tar.gz: bd791e489c057c52be7057bee4b4e0d8fdb7cd28fac10de187f980daf125a7f7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 15bd5a3e57f5c48671fad01351acf4b70a20f03ebe4d2d6df70c38b97709f41b11a5060bce9bd9a11daf3f6544429fbda8e9da376de3508c6f1abae9a2d03b05
|
7
|
+
data.tar.gz: 31c6a6bfdad46c5614f0793afd4e1a02e52b913f9a253ccd6b32081e55476cb58997bd39ac63b586529fa850ce1294e7eb357f158a541b27a65abe91d51f85da
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Taeyang Lee
|
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,33 @@
|
|
1
|
+
# AnnotateModel
|
2
|
+
|
3
|
+
AnnotateModel is a lightweight gem to annotate Rails models with database schema information.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
`bundle add annotate_model`
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
`gem install annotate_model`
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
To annotate all models, run:
|
18
|
+
|
19
|
+
`annotate_model --all`
|
20
|
+
|
21
|
+
To annotate specific models, run:
|
22
|
+
|
23
|
+
`annotate_model [model_name1] [model_name2] ...`
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
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).
|
30
|
+
|
31
|
+
## License
|
32
|
+
|
33
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/exe/annotate_model
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AnnotateModel
|
4
|
+
class AnnotationDecider
|
5
|
+
def initialize(file)
|
6
|
+
@file = file
|
7
|
+
end
|
8
|
+
|
9
|
+
def annotate?
|
10
|
+
klass = ModelClassGetter.call(@file)
|
11
|
+
return false unless klass
|
12
|
+
|
13
|
+
annotate_conditions(klass).all?
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def annotate_conditions(klass)
|
19
|
+
[
|
20
|
+
klass.is_a?(Class),
|
21
|
+
klass.respond_to?(:table_exists?) && klass.table_exists?
|
22
|
+
]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module AnnotateModel
|
2
|
+
class Annotator
|
3
|
+
def self.annotate(model_names)
|
4
|
+
process_files(AnnotateModel::Finder.model_files!(model_names))
|
5
|
+
rescue ModelFileNotFoundError => e
|
6
|
+
warn e.message
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.annotate_all
|
10
|
+
process_files(AnnotateModel::Finder.all_model_files)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.process_files(files)
|
14
|
+
run_files = []
|
15
|
+
skipped_files = []
|
16
|
+
failed_files = []
|
17
|
+
|
18
|
+
files.each do |file|
|
19
|
+
result = annotate_file(file)
|
20
|
+
case result
|
21
|
+
when :run
|
22
|
+
run_files << file
|
23
|
+
when :skipped
|
24
|
+
skipped_files << file
|
25
|
+
when :failed
|
26
|
+
failed_files << file
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
log_results(run_files, skipped_files, failed_files)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.annotate_file(file)
|
34
|
+
return :failed unless AnnotationDecider.new(file).annotate?
|
35
|
+
|
36
|
+
content = File.read(file)
|
37
|
+
return :skipped if content.include?("# == Schema Information")
|
38
|
+
|
39
|
+
schema_info = fetch_schema_info(file)
|
40
|
+
return :failed unless schema_info
|
41
|
+
|
42
|
+
annotated_content = <<~ANNOTATION + content
|
43
|
+
# == Schema Information
|
44
|
+
#
|
45
|
+
#{schema_info}
|
46
|
+
ANNOTATION
|
47
|
+
|
48
|
+
File.write(file, annotated_content)
|
49
|
+
:run
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.fetch_schema_info(file)
|
53
|
+
model_name = File.basename(file, ".rb").camelize
|
54
|
+
table_name = model_name.tableize
|
55
|
+
|
56
|
+
begin
|
57
|
+
columns = ActiveRecord::Base.connection.columns(table_name)
|
58
|
+
rescue ActiveRecord::StatementInvalid
|
59
|
+
warn "Could not find table '#{table_name}'"
|
60
|
+
return
|
61
|
+
end
|
62
|
+
|
63
|
+
columns.map do |column|
|
64
|
+
"# #{column.name.ljust(10)} :#{column.type}#{' ' * (15 - column.type.to_s.length)}#{column.null ? '' : ' not null'}"
|
65
|
+
end.join("\n")
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.log_results(run_files, skipped_files, failed_files)
|
69
|
+
puts "Annotated files (#{run_files.size}):"
|
70
|
+
run_files.each { |file| puts " - #{file}" }
|
71
|
+
|
72
|
+
puts "Skipped files (#{skipped_files.size}):"
|
73
|
+
skipped_files.each { |file| puts " - #{file}" }
|
74
|
+
|
75
|
+
puts "Failed files (#{failed_files.size}):"
|
76
|
+
failed_files.each { |file| puts " - #{file}" }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module AnnotateModel
|
2
|
+
class Cli
|
3
|
+
def self.start(args)
|
4
|
+
options = {}
|
5
|
+
OptionParser.new do |opts|
|
6
|
+
opts.banner = "Usage: annotate models [options] [model_name]"
|
7
|
+
|
8
|
+
opts.on("-a", "--all", "Annotate all models") do |a|
|
9
|
+
options[:all] = a
|
10
|
+
end
|
11
|
+
|
12
|
+
opts.on("-h", "--help", "Prints this help") do
|
13
|
+
puts opts
|
14
|
+
exit
|
15
|
+
end
|
16
|
+
end.parse!(args)
|
17
|
+
|
18
|
+
if options[:all] || args.empty?
|
19
|
+
AnnotateModel::Annotator.annotate_all
|
20
|
+
else
|
21
|
+
AnnotateModel::Annotator.annotate(args)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module AnnotateModel
|
2
|
+
class Finder
|
3
|
+
def self.model_files!(model_names)
|
4
|
+
model_names.map do |model_name|
|
5
|
+
find_model_file!(model_name)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.all_model_files
|
10
|
+
Dir.glob(Rails.root.join("app", "models", "**", "*.rb"))
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.find_model_file(model_name)
|
14
|
+
model_path = Rails.root.join("app", "models", "#{model_name.underscore}.rb")
|
15
|
+
model_path if File.exist?(model_path)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.find_model_file!(model_name)
|
19
|
+
find_model_file(model_name) || raise(ModelFileNotFoundError, "#{model_name}")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AnnotateModel
|
4
|
+
class ModelClassGetter
|
5
|
+
class << self
|
6
|
+
def call(file)
|
7
|
+
model_name = file_to_model_name(file)
|
8
|
+
begin
|
9
|
+
model_name.constantize
|
10
|
+
rescue NameError
|
11
|
+
nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def file_to_model_name(file)
|
18
|
+
relative_path = file.sub(Rails.root.join('app', 'models').to_s + '/', '')
|
19
|
+
relative_path.to_s.sub('.rb', '').camelize
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: annotate_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Taeyang Lee
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-01-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A lightweight gem to annotate Rails models based on the database schema.
|
14
|
+
email:
|
15
|
+
- taeyang91@gmail.com
|
16
|
+
executables:
|
17
|
+
- annotate_model
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- LICENSE.txt
|
22
|
+
- README.md
|
23
|
+
- exe/annotate_model
|
24
|
+
- lib/annotate_model.rb
|
25
|
+
- lib/annotate_model/annotation_decider.rb
|
26
|
+
- lib/annotate_model/annotator.rb
|
27
|
+
- lib/annotate_model/cli.rb
|
28
|
+
- lib/annotate_model/finder.rb
|
29
|
+
- lib/annotate_model/model_class_getter.rb
|
30
|
+
- lib/annotate_model/model_file_not_found_error.rb
|
31
|
+
- lib/annotate_model/version.rb
|
32
|
+
homepage: https://github.com/taeyang91/annotate_model
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata:
|
36
|
+
homepage_uri: https://github.com/taeyang91/annotate_model
|
37
|
+
source_code_uri: https://github.com/taeyang91/annotate_model
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 3.0.0
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubygems_version: 3.3.7
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: Annotates Rails models with database schema information.
|
57
|
+
test_files: []
|