annotate_model 0.1.2 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f530a132985bc423c3f6a32005be46df58acddc550bdd68dce0658010407d4e7
4
- data.tar.gz: d57060bbee598d5914359717d716a80ff3b60463993e62fb15ee66d3843cd723
3
+ metadata.gz: acaa898d77121490f69c9253e67f0da9788299581ca7baca9a91a02e7dde69d8
4
+ data.tar.gz: d600bb6a190d68a3aa29b6e3159956b3da07d80bb521ba9feaf65b7e70fb875b
5
5
  SHA512:
6
- metadata.gz: 6a0f926c606fbfdf0346a9e7e5f2161cbc1315cddd6c03ba3a2b2b6d2e5962564bd9a7cffb905b9e3a25c56a65bdc35dd45e0442687a481f3e950d861792fd1d
7
- data.tar.gz: 44a4d4409f9605ce19972c9a111cded649ccdc7c0c6b0c23c8faf66a7e22d9ad79e6e1fcb1cfcacdd7934d22075046d32aee194746ffaeb18ce5c1790eca951b
6
+ metadata.gz: 8c7757e023d793050a2e4b0f99b0fbca36e3a8f157213c85a1d3b10d81fe751a8876751a7b851af0347bc007b10909301a1b8d3de7927ebab159fad3842fb236
7
+ data.tar.gz: 7d38492f6fcdfc8ce390c595eb2c6caf398c501d249eb873df835a57342e5fa0fd462a6b4c6087d2f1544b2139c4bf8c7cceee0e413eb792a925233af57ae478
data/README.md CHANGED
@@ -1,7 +1,10 @@
1
- # AnnotateModel [![Gem Version](https://badge.fury.io/rb/annotate_model.svg)](https://badge.fury.io/rb/annotate_model)
1
+ # AnnotateModel [![Gem Version][]][gem-version]
2
2
 
3
3
  AnnotateModel is a lightweight gem to annotate Rails models with database schema information.
4
4
 
5
+ [Gem Version]: https://badge.fury.io/rb/annotate_model.svg?v=2
6
+ [gem-version]: https://rubygems.org/gems/annotate_model
7
+
5
8
  ## Example
6
9
 
7
10
  Here's an example of the schema information in your model file after annotation:
@@ -12,10 +15,26 @@ Here's an example of the schema information in your model file after annotation:
12
15
  # name :string
13
16
  # created_at :datetime not null
14
17
  # updated_at :datetime not null
18
+ # ==
15
19
  class Product < ApplicationRecord
16
20
  end
17
21
  ```
18
22
 
23
+ ```ruby
24
+ # == Schema Information
25
+ #
26
+ # id :integer not null
27
+ # name :string
28
+ # email :string
29
+ # created_at :datetime not null
30
+ # updated_at :datetime not null
31
+ # ==
32
+ class Admin::User < ApplicationRecord
33
+ end
34
+ ```
35
+
36
+ The annotation block is re-written each time the command runs. The starting line `# == Schema Information` and the ending line `# ==` should never be changed as they are important for the regex match used to remove existing annotations.
37
+
19
38
  ## Installation
20
39
 
21
40
  Add this line to the application's Gemfile:
@@ -36,11 +55,19 @@ gem install annotate_model
36
55
 
37
56
  To annotate all models, run:
38
57
 
39
- `bundle exec annotate_model --all`
58
+ `bundle exec annotate_model`
40
59
 
41
60
  To annotate specific models, run:
42
61
 
43
- `bundle exec annotate_model [model_name1] [model_name2] ...`
62
+ `bundle exec annotate_model Product Admin::User`
63
+
64
+ ## Skipping Annotation
65
+
66
+ If you want to skip annotation for a specific model, add the following skip flag to the model file:
67
+
68
+ ```ruby
69
+ # == annotate_model:skip
70
+ ```
44
71
 
45
72
  ## Development
46
73
 
@@ -2,11 +2,15 @@
2
2
 
3
3
  module AnnotateModel
4
4
  class AnnotationDecider
5
+ SKIP_FLAG = "# == annotate_model:skip"
6
+
5
7
  def initialize(file)
6
8
  @file = file
7
9
  end
8
10
 
9
11
  def annotate?
12
+ return false if skip_flag_present?
13
+
10
14
  begin
11
15
  klass = @file.model_name.constantize
12
16
  rescue NameError
@@ -18,6 +22,10 @@ module AnnotateModel
18
22
 
19
23
  private
20
24
 
25
+ def skip_flag_present?
26
+ File.foreach(@file.to_s).any? { |line| line.include?(SKIP_FLAG) }
27
+ end
28
+
21
29
  def annotate_conditions(klass)
22
30
  [
23
31
  klass.is_a?(Class),
@@ -30,22 +30,26 @@ module AnnotateModel
30
30
  def self.annotate_file(file)
31
31
  return :skipped unless AnnotationDecider.new(file).annotate?
32
32
 
33
- content = File.read(file.to_s)
34
- return :skipped if content.include?("# == Schema Information")
35
-
36
33
  schema_info = fetch_schema_info(file)
37
34
  return :failed unless schema_info
38
35
 
36
+ content = File.read(file.to_s)
37
+ content = remove_existing_annotation(content)
39
38
  annotated_content = <<~ANNOTATION + content
40
39
  # == Schema Information
41
40
  #
42
41
  #{schema_info}
42
+ # ==
43
43
  ANNOTATION
44
44
 
45
45
  File.write(file.to_s, annotated_content)
46
46
  :run
47
47
  end
48
48
 
49
+ def self.remove_existing_annotation(content)
50
+ content.sub(/^# == Schema Information\n(#.*\n)*# ==\n/m, '')
51
+ end
52
+
49
53
  def self.fetch_schema_info(file)
50
54
  begin
51
55
  columns = ActiveRecord::Base.connection.columns(file.table_name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AnnotateModel
4
- VERSION = "0.1.2"
4
+ VERSION = "1.0.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: annotate_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taeyang Lee
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-01-21 00:00:00.000000000 Z
11
+ date: 2025-01-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A lightweight gem to annotate Rails models based on the database schema.
14
14
  email: