awesome_annotate 0.1.1 → 0.1.2

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: b32b0b966e78664837bbf66e85c75e882f6f6ecaff193b7ba0e0d1ce7fae42bc
4
+ data.tar.gz: d1293087a0c3f44ff72aa9ff4cd6ad02effa90afd8562160c68dbc1267353edb
5
5
  SHA512:
6
- metadata.gz: d92baaa7dee1ebde3c1f5713e235a5f78ab4c3ea3f546e03f0de0fdacff286e493ddc7f94cbc18031341a98c009740300930004fd7eca0aa39fe87721be1f29d
7
- data.tar.gz: ff3d7e4fc5b3e448de8e2b08feff317f20c7e4e7fc920e86dae9859fb7c0843df8d7c2f945decb886ac48c364ec85d46cc250155553cf491a8c93f22f432bf6e
6
+ metadata.gz: 28d077b539bcef973f6576576084c809f090f132401cdd8ca0f36eeebe7fd7d4d99d68cd1b7412c470e658f2e80a56959ff886c8543283758ef44ec8296a94c2
7
+ data.tar.gz: 1a18894e57ef95c194fe94b2c0aedaa5ad183ed64251857328875a274ca23bb76aa7e1419578c6d00b52e3f7e65b5b608d40bd85a6287cea125b909029bb95d8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2024-03-31
3
+ ## [0.1.0] - 2024-04-29
4
4
 
5
5
  - Initial release
6
+
7
+ ## [0.1.1] - 2024-05-02
8
+
9
+ - Fixed a bug
10
+
11
+ ## [0.1.2] - 2024-05-03
12
+
13
+ - Add a new feature
14
+ - --version flag
@@ -1,34 +1,21 @@
1
- require 'active_record'
2
1
  require 'awesome_annotate'
2
+ require 'awesome_annotate/model'
3
+ require 'awesome_annotate/version'
3
4
  require 'thor'
4
5
 
5
6
  module AwesomeAnnotate
6
7
  class CLI < Thor
7
8
  include Thor::Actions
8
9
 
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)
26
-
27
- insert_into_file file_path, :before => / class #{klass} \n|class #{klass} .*\n / do
28
- "# Columns: #{column_names.join(', ')}\n"
29
- end
10
+ map %w[--version -v] => :print_version
11
+ desc "--version, -v", "print the version"
12
+ def print_version
13
+ say AwesomeAnnotate::VERSION
14
+ end
30
15
 
31
- puts "annotate #{model_name.pluralize} table columns in #{file_path}"
16
+ desc 'model [model_name]', 'annotate your model'
17
+ def model(model_name)
18
+ AwesomeAnnotate::Model.new.annotate(model_name)
32
19
  end
33
20
 
34
21
  private
@@ -36,9 +23,5 @@ module AwesomeAnnotate
36
23
  def self.exit_on_failure?
37
24
  true
38
25
  end
39
-
40
- def self.source_root
41
- Dir.pwd
42
- end
43
26
  end
44
27
  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
@@ -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.2"
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.2
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-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -56,6 +56,7 @@ 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
59
60
  - lib/awesome_annotate/version.rb
60
61
  - sig/awesome_annotate.rbs
61
62
  homepage: https://github.com/wisdom-plus/awesome_annotate