awesome_annotate 0.1.0 → 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: 5c32fa3c7c828aa41da8e575479a8d6156d06f6911890e704d605621760f8f3e
4
- data.tar.gz: 12d8857e51cf0ad0cc4f2819230236cce845eb54c41704551b81d2bec6422086
3
+ metadata.gz: b32b0b966e78664837bbf66e85c75e882f6f6ecaff193b7ba0e0d1ce7fae42bc
4
+ data.tar.gz: d1293087a0c3f44ff72aa9ff4cd6ad02effa90afd8562160c68dbc1267353edb
5
5
  SHA512:
6
- metadata.gz: 71314f7a69db717c1d689468a1045e4fe5520a24ad98c2723081716b88e46f4268c5eb2949916c5f312016f90c5f103382cdf91adc2280132dd370aa90e0925d
7
- data.tar.gz: 191e243909d986426a9b31679d247ffb8c79bbd69f65dd0543459e625e891ed1bd6acd2622c6630ec8efe5f971e6916e9b2428ea22f45046e70e89142e4df43b
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,30 +1,27 @@
1
1
  require 'awesome_annotate'
2
+ require 'awesome_annotate/model'
3
+ require 'awesome_annotate/version'
2
4
  require 'thor'
3
- require 'active_record'
4
5
 
5
6
  module AwesomeAnnotate
6
7
  class CLI < Thor
7
- desc "annotate", "annotate your code"
8
- def annotate
9
- puts "annotate your code"
10
- return 'annotate your code'
11
- # AwesomeAnnotate::Annotator.new.annotate
8
+ include Thor::Actions
9
+
10
+ map %w[--version -v] => :print_version
11
+ desc "--version, -v", "print the version"
12
+ def print_version
13
+ say AwesomeAnnotate::VERSION
12
14
  end
13
15
 
14
- desc 'model [model name]', 'annotate your model'
16
+ desc 'model [model_name]', 'annotate your model'
15
17
  def model(model_name)
16
- name = model_name.singularize.camelize
17
- klass = Object.const_get(name)
18
+ AwesomeAnnotate::Model.new.annotate(model_name)
19
+ end
18
20
 
19
- puts 'This is not a model' unless klass < ActiveRecord::Base
21
+ private
20
22
 
21
- column_names = klass.column_names
22
- model_dir = 'app/models'
23
- file_path = "#{model_dir}/#{model_name}.rb"
24
- File.open(file_path, 'r') do |file|
25
- file.puts "# Columns: #{column_names.join(', ')}"
26
- end
27
- puts 'annotate your model'
23
+ def self.exit_on_failure?
24
+ true
28
25
  end
29
26
  end
30
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.0"
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.0
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-04-30 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