awesome_annotate 0.1.1 → 0.1.3

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: 8f47508ed24d0bf2374190e00d9f23032797e1fac1740d91283d392eb970ed8c
4
- data.tar.gz: c76db1996bb2cee959f473c063c30b280077fc7b3fe64cd7c592b99eca4e7dd9
3
+ metadata.gz: 14fd05f02d2e589431ee8ca8b52911aa1365e2daebcb835e78b9cbfaf2198e69
4
+ data.tar.gz: 8a5de3f58b1c8f0cb94a8737b9ca01c9988868c8a797c8382f53dd6cf060cb8c
5
5
  SHA512:
6
- metadata.gz: d92baaa7dee1ebde3c1f5713e235a5f78ab4c3ea3f546e03f0de0fdacff286e493ddc7f94cbc18031341a98c009740300930004fd7eca0aa39fe87721be1f29d
7
- data.tar.gz: ff3d7e4fc5b3e448de8e2b08feff317f20c7e4e7fc920e86dae9859fb7c0843df8d7c2f945decb886ac48c364ec85d46cc250155553cf491a8c93f22f432bf6e
6
+ metadata.gz: 7584be12529764fc8a172dd29672331842eb2cb00371bafad1691c67206386452ccc8a8e28c42e0f55bca49522a5b944cd1b56cc55c74cb608c60ec194dba6ca
7
+ data.tar.gz: 51d1c40aeba22f1f941da7539e6ee489dbce49d60b7af457ea08650e933a36c276943b522fd58c412caced26699b547aefdfea68d0747b6e56886811898ada76
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
- ## [Unreleased]
1
+ # Changelog
2
2
 
3
- ## [0.1.0] - 2024-03-31
3
+ ## [0.1.3] - 2024-05-05
4
+
5
+ - Add a new feature
6
+ - annotate routes command
7
+
8
+ ## [0.1.2] - 2024-05-03
9
+
10
+ - Add a new feature
11
+ - --version flag
12
+
13
+ ## [0.1.1] - 2024-05-02
14
+
15
+ - Fixed a bug
16
+
17
+ ## [0.1.0] - 2024-04-29
4
18
 
5
19
  - Initial release
@@ -1,34 +1,27 @@
1
- require 'active_record'
2
1
  require 'awesome_annotate'
2
+ require 'awesome_annotate/model'
3
+ require 'awesome_annotate/route'
4
+ require 'awesome_annotate/version'
3
5
  require 'thor'
4
6
 
5
7
  module AwesomeAnnotate
6
8
  class CLI < Thor
7
9
  include Thor::Actions
8
10
 
9
- desc 'model [model name]', 'annotate your model'
10
- def model(model_name)
11
- rails_env_file = Pathname.new('./config/environment.rb')
12
- abort "Rails application path is required" unless rails_env_file.exist?
13
-
14
- apply rails_env_file.to_s
15
-
16
- name = model_name.singularize.camelize
17
- klass = Object.const_get(name)
18
-
19
- return puts 'This model does not inherit activerecord' unless klass < ActiveRecord::Base
20
-
21
- column_names = klass.column_names
22
- model_dir = Pathname.new('app/models')
23
- file_path = "#{model_dir.to_s}/#{model_name}.rb"
24
-
25
- return puts "Model file not found" unless File.exist?(file_path)
11
+ map %w[--version -v] => :print_version
12
+ desc "--version, -v", "print the version"
13
+ def print_version
14
+ say AwesomeAnnotate::VERSION
15
+ end
26
16
 
27
- insert_into_file file_path, :before => / class #{klass} \n|class #{klass} .*\n / do
28
- "# Columns: #{column_names.join(', ')}\n"
29
- end
17
+ desc 'model [model_name]', 'annotate your model'
18
+ def model(model_name)
19
+ AwesomeAnnotate::Model.new.annotate(model_name)
20
+ end
30
21
 
31
- puts "annotate #{model_name.pluralize} table columns in #{file_path}"
22
+ desc 'routes', 'annotate your route'
23
+ def routes
24
+ AwesomeAnnotate::Route.new.annotate
32
25
  end
33
26
 
34
27
  private
@@ -36,9 +29,5 @@ module AwesomeAnnotate
36
29
  def self.exit_on_failure?
37
30
  true
38
31
  end
39
-
40
- def self.source_root
41
- Dir.pwd
42
- end
43
32
  end
44
33
  end
@@ -0,0 +1,68 @@
1
+ require 'active_record'
2
+ require 'thor'
3
+
4
+ module AwesomeAnnotate
5
+ class Model < Thor
6
+ include Thor::Actions
7
+
8
+ desc 'model [model name]', 'annotate your model'
9
+ def annotate(model_name)
10
+ abort "Rails application path is required" unless env_file_path.exist?
11
+
12
+ apply env_file_path.to_s
13
+
14
+ klass = klass_name(model_name)
15
+
16
+ return say 'This model does not inherit activerecord' unless klass < ActiveRecord::Base
17
+
18
+ column_names = column_names(klass)
19
+ file_path = model_file_path(model_name)
20
+
21
+ insert_file_before_class(file_path, klass, "# Columns: #{column_names.join(', ')}\n")
22
+
23
+ say "annotate #{model_name.pluralize} table columns in #{file_path}"
24
+ end
25
+
26
+ private
27
+
28
+ def env_file_path
29
+ Pathname.new('config/environment.rb')
30
+ end
31
+
32
+ def model_dir
33
+ Pathname.new('app/models')
34
+ end
35
+
36
+ def insert_file_before_class(file_path, klass, message)
37
+ insert_into_file file_path, :before => / class #{klass} \n|class #{klass} .*\n / do
38
+ message
39
+ end
40
+ end
41
+
42
+ def column_names(klass)
43
+ klass.column_names
44
+ end
45
+
46
+ def model_file_path(model_name)
47
+ file_path = "#{model_dir}/#{model_name}.rb"
48
+
49
+ unless File.exist?(file_path)
50
+ return say "Model file not found"
51
+ end
52
+
53
+ return file_path
54
+ end
55
+
56
+ def klass_name(model_name)
57
+ name = model_name.singularize.camelize
58
+ klass = Object.const_get(name)
59
+
60
+ rescue NameError
61
+ say "Model not found"
62
+ end
63
+
64
+ def self.source_root
65
+ Dir.pwd
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,55 @@
1
+ require 'active_record'
2
+ require 'thor'
3
+
4
+ module AwesomeAnnotate
5
+ class Route < Thor
6
+ include Thor::Actions
7
+
8
+ desc 'annotate all routes', 'annotate your routes'
9
+ def annotate
10
+ abort "Rails application path is required" unless env_file_path.exist?
11
+
12
+ apply env_file_path.to_s
13
+
14
+ inspector = ActionDispatch::Routing::RoutesInspector.new(Rails.application.routes.routes)
15
+ formatter = ActionDispatch::Routing::ConsoleFormatter::Sheet.new
16
+
17
+ routes = inspector.format(formatter, {})
18
+ route_message = parse_routes(routes)
19
+
20
+ insert_file_before_class(route_file_path, route_message)
21
+
22
+ say "annotate routes in #{route_file_path}"
23
+ end
24
+
25
+ private
26
+
27
+ def env_file_path
28
+ Pathname.new('config/environment.rb')
29
+ end
30
+
31
+ def route_file_path
32
+ Pathname.new('config/routes.rb')
33
+ end
34
+
35
+ def parse_routes(routes)
36
+ split_routes = routes.split(/\r\n|\r|\n/)
37
+ parse_routes = split_routes.map do |route|
38
+ "# #{route}\n"
39
+ end
40
+ parse_routes.push("\n")
41
+ parse_routes.unshift("#---This is route annotate---\n#\n")
42
+ parse_routes.join
43
+ end
44
+
45
+ def insert_file_before_class(file_path, message)
46
+ insert_into_file file_path, :before => "Rails.application.routes.draw do\n" do
47
+ message
48
+ end
49
+ end
50
+
51
+ def self.source_root
52
+ Dir.pwd
53
+ end
54
+ end
55
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AwesomeAnnotate
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awesome_annotate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - wisdom-plus
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-01 00:00:00.000000000 Z
11
+ date: 2024-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -56,6 +56,8 @@ files:
56
56
  - exe/awesome_annotate
57
57
  - lib/awesome_annotate.rb
58
58
  - lib/awesome_annotate/cli.rb
59
+ - lib/awesome_annotate/model.rb
60
+ - lib/awesome_annotate/route.rb
59
61
  - lib/awesome_annotate/version.rb
60
62
  - sig/awesome_annotate.rbs
61
63
  homepage: https://github.com/wisdom-plus/awesome_annotate