mixin_comment 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a3ba1d04a4df226d3003ee545eff150313db134be536c2d9947222400c0b4713
4
+ data.tar.gz: 6c925dcf01c961cbc2232070d47bc8eac9c4ddba0c6f211c3ec6190c430d962e
5
+ SHA512:
6
+ metadata.gz: ffd54ff8b9a130c673f07820d9a0afb2fe2ee97ddcc508de4d0ea43ba2b8532f3d7b658976e8e8714ab447045caaa2af8d24cbe3b3dcb64e98790030cfce720a
7
+ data.tar.gz: a056874441c344682264bc6f4c1d6b123af42db0c46fb556416d5d5c4f003e0a68afc360f16f18772e358772720a688334a7fbe9c2cf0bfe5936d7195efdd510
@@ -0,0 +1,39 @@
1
+ name: CI
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ runs-on: ubuntu-18.04
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ ruby: [2.6, 2.7, 3.0]
12
+ gemfile: ['rails50', 'rails51', 'rails52', 'rails60', 'rails61']
13
+ exclude:
14
+ - ruby: 3.0
15
+ gemfile: rails50
16
+ - ruby: 3.0
17
+ gemfile: rails51
18
+ - ruby: 3.0
19
+ gemfile: rails52
20
+
21
+ name: ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}
22
+
23
+ env:
24
+ BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
25
+
26
+ steps:
27
+ - uses: actions/checkout@v2
28
+ - uses: ruby/setup-ruby@v1
29
+ with:
30
+ ruby-version: ${{ matrix.ruby }}
31
+ bundler-cache: true
32
+ - name: Prepare test
33
+ run: |
34
+ cd spec/dummy
35
+ BUNDLE_GEMFILE=../../${{ env.BUNDLE_GEMFILE }} RAILS_ENV=test bundle exec rake db:create db:migrate
36
+ cd ../..
37
+ - name: Run test
38
+ run: |
39
+ bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ .bundle/
2
+ .project
3
+ Gemfile.lock
4
+ coverage/
5
+ gemfiles/*.lock
6
+ pkg/
7
+ tmp/
8
+ spec/dummy/db/*.sqlite3
9
+ spec/dummy/db/schema_*.rb
10
+ spec/dummy/log/*.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.1.0
4
+
5
+ * First release.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Yoshikazu Kaneta
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,95 @@
1
+ # MixinComment
2
+
3
+ Verification of comments for roughly designed mixin module.
4
+
5
+ ## Dependencies
6
+
7
+ * ruby 2.6+
8
+ * activesupport 5.0+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'mixin_comment'
16
+ ```
17
+
18
+ Then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Usage
23
+
24
+ Add comments with specific labels preceding to the module definition.
25
+ For example:
26
+
27
+ ```ruby
28
+ # [required methods] method, method2
29
+ module A
30
+ def call_method
31
+ method
32
+ end
33
+
34
+ def call_method2
35
+ method2
36
+ end
37
+ end
38
+
39
+ class Item
40
+ include A
41
+
42
+ def method
43
+ end
44
+ end
45
+ ```
46
+
47
+ The label `[required methods]` means that the class `Item` which includes the module `A` should have two methods named `method` and `method2`.
48
+ You can verify validity of classes as follows:
49
+
50
+ ```ruby
51
+ # speficy your directory which contains *.rb files
52
+ results = MixinComment.verify('app')
53
+
54
+ results[0].module_name
55
+ #=> "A"
56
+ results[0].label
57
+ #=> "required methods"
58
+ results[0].errors
59
+ #=> [[Item, "method2"]]
60
+ ```
61
+
62
+ In this example the class `Item` does not have `method2`,
63
+ so `[[Item, "method2"]]` is returned as an error.
64
+
65
+ ### Supported labels
66
+
67
+ Following labels are available:
68
+
69
+ * `required methods`: verify existence of instance methods in classes.
70
+ * `required class methods`: verify existence of class methods in classes.
71
+ * `required override methods`: verify existence of override methods in classes.
72
+
73
+ Special labels for ActiveRecord models are also available:
74
+
75
+ * `required columns`: verify existence of columns in ActiveRecord models.
76
+ * `required associations`: verify existence of associations in ActiveRecord models.
77
+
78
+ ### For Rails
79
+
80
+ In case you develops rails application, rake task is available:
81
+
82
+ ```shell-session
83
+ $ bundle exec rake mixin_comment:verify
84
+ Item#method2 is required method by A
85
+ ```
86
+
87
+ Note that database schema will be loaded to define attribute methods for ActiveRecord model.
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome at https://github.com/kanety/mixin_comment.
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mixin_comment"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.0.0"
4
+ gem "sqlite3", "~> 1.3.6"
5
+
6
+ gemspec path: "../"
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.1.0"
4
+ gem "sqlite3", "~> 1.3.6"
5
+
6
+ gemspec path: "../"
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.2.0"
4
+ gem "sqlite3", "~> 1.3.6"
5
+
6
+ gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 6.0.0"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 6.1.0"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,24 @@
1
+ require 'rdoc'
2
+ require 'active_support/all'
3
+
4
+ require 'mixin_comment/version'
5
+ require 'mixin_comment/part'
6
+ require 'mixin_comment/loader'
7
+ require 'mixin_comment/parser'
8
+ require 'mixin_comment/verifier'
9
+ require 'mixin_comment/railtie' if defined?(Rails)
10
+
11
+ module MixinComment
12
+ class << self
13
+ def verify(dirs)
14
+ parts = Parser.new(dirs).call
15
+
16
+ Loader.new(dirs).call
17
+
18
+ parts.each do |part|
19
+ Verifier.new(part).call
20
+ end
21
+ parts
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MixinComment
4
+ class Loader
5
+ def initialize(dirs)
6
+ @dirs = Array(dirs)
7
+ end
8
+
9
+ def call
10
+ load
11
+ load_rails if defined? Rails
12
+ end
13
+
14
+ private
15
+
16
+ def load
17
+ Dir[@dirs.map { |dir| "#{dir}/**/*.rb" }.join(',')].each do |path|
18
+ full_path = path
19
+ full_path = File.join(Dir.pwd, full_path) unless path.start_with?('/')
20
+ require full_path
21
+ end
22
+ end
23
+
24
+ def load_rails
25
+ Rails.application.eager_load!
26
+ ActiveRecord::Base.descendants.each(&:define_attribute_methods)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MixinComment
4
+ class Parser
5
+ def initialize(dirs)
6
+ @dirs = Array(dirs)
7
+ @parts = []
8
+ end
9
+
10
+ def call
11
+ rdoc = RDoc::RDoc.new
12
+ rdoc.document(['--dry-run'] + @dirs)
13
+
14
+ rdoc.generator.classes.each do |mod|
15
+ next unless mod.module?
16
+ mod.comment_location.each do |comments|
17
+ comments.each do |comment|
18
+ if comment.is_a?(RDoc::Comment)
19
+ parse_module_comment(mod, comment)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ @parts
26
+ end
27
+
28
+ private
29
+
30
+ def parse_module_comment(mod, comment)
31
+ comment.parse.parts.each do |part|
32
+ if part.type == :LABEL
33
+ part.items.each do |item|
34
+ if item.label[0].in?(MixinComment::Part::LABELS)
35
+ parse_label_parts(mod, item.label[0], item.parts)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ def parse_label_parts(mod, label, parts)
43
+ attributes = parts.map do |part|
44
+ part.parts.map do |text|
45
+ text.strip.split(/\s*,\s*/)
46
+ end
47
+ end.flatten.compact
48
+
49
+ @parts << Part.new(mod.full_name, label, attributes)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MixinComment
4
+ class Part
5
+ LABELS = [
6
+ 'required methods',
7
+ 'required class methods',
8
+ 'required override methods',
9
+ 'required columns',
10
+ 'required associations'
11
+ ]
12
+
13
+ attr_accessor :module_name, :label, :attributes
14
+ attr_accessor :errors
15
+
16
+ def initialize(module_name, label, attributes)
17
+ @module_name = module_name
18
+ @label = label
19
+ @attributes = attributes
20
+ @errors = []
21
+ end
22
+
23
+ def has_errors?
24
+ @errors.present?
25
+ end
26
+
27
+ def show_errors
28
+ errors.each do |klass, attr|
29
+ puts "#{klass}##{attr} is #{label.singularize} by #{module_name}"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MixinComment
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load 'mixin_comment/tasks/verify.rake'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ namespace :mixin_comment do
2
+ desc 'Verify mixin comments'
3
+ task :verify => :environment do
4
+ dirs = ENV.key?('DIRS') ? ENV['DIRS'].split(',') : [Rails.root.join('app').to_s]
5
+ results = MixinComment.verify(dirs)
6
+ results.each(&:show_errors)
7
+ end
8
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MixinComment
4
+ class Verifier
5
+ def initialize(part)
6
+ @part = part
7
+ end
8
+
9
+ def call
10
+ included_classes(@part.module_name).each do |klass|
11
+ @part.attributes.each do |attr|
12
+ send("verify_#{@part.label.gsub(' ', '_')}", klass, attr)
13
+ end
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def included_classes(module_name)
20
+ mod = module_name.constantize
21
+ klasses = ObjectSpace.each_object(Class).to_a
22
+ klasses.select { |klass| klass.included_modules.include?(mod) }.sort_by(&:name)
23
+ end
24
+
25
+ def verify_required_methods(klass, attr)
26
+ unless klass.method_defined?(attr)
27
+ @part.errors << [klass, attr]
28
+ end
29
+ end
30
+
31
+ def verify_required_class_methods(klass, attr)
32
+ unless klass.singleton_class.method_defined?(attr)
33
+ @part.errors << [klass, attr]
34
+ end
35
+ end
36
+
37
+ def verify_required_override_methods(klass, attr)
38
+ unless klass.method_defined?(attr, false)
39
+ @part.errors << [klass, attr]
40
+ end
41
+ end
42
+
43
+ def verify_required_columns(klass, attr)
44
+ unless klass.column_names.include?(attr)
45
+ @part.errors << [klass, attr]
46
+ end
47
+ end
48
+
49
+ def verify_required_associations(klass, attr)
50
+ unless klass.reflect_on_association(attr)
51
+ @part.errors << [klass, attr]
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MixinComment
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mixin_comment/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mixin_comment"
8
+ spec.version = MixinComment::VERSION
9
+ spec.authors = ["Yoshikazu Kaneta"]
10
+ spec.email = ["kaneta@sitebridge.co.jp"]
11
+ spec.summary = %q{Verification of comments for roughly designed mixin module}
12
+ spec.description = %q{Verification of comments for roughly designed mixin module}
13
+ spec.homepage = "https://github.com/kanety/mixin_comment"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activesupport", ">= 5.0"
21
+ spec.add_dependency "rdoc"
22
+
23
+ spec.add_development_dependency "rails", ">= 5.0"
24
+ spec.add_development_dependency "sqlite3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec-rails"
27
+ spec.add_development_dependency "simplecov"
28
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mixin_comment
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoshikazu Kaneta
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Verification of comments for roughly designed mixin module
112
+ email:
113
+ - kaneta@sitebridge.co.jp
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".github/workflows/ci.yml"
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - CHANGELOG.md
122
+ - Gemfile
123
+ - LICENSE
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - gemfiles/rails50.gemfile
129
+ - gemfiles/rails51.gemfile
130
+ - gemfiles/rails52.gemfile
131
+ - gemfiles/rails60.gemfile
132
+ - gemfiles/rails61.gemfile
133
+ - lib/mixin_comment.rb
134
+ - lib/mixin_comment/loader.rb
135
+ - lib/mixin_comment/parser.rb
136
+ - lib/mixin_comment/part.rb
137
+ - lib/mixin_comment/railtie.rb
138
+ - lib/mixin_comment/tasks/verify.rake
139
+ - lib/mixin_comment/verifier.rb
140
+ - lib/mixin_comment/version.rb
141
+ - mixin_comment.gemspec
142
+ homepage: https://github.com/kanety/mixin_comment
143
+ licenses: []
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubygems_version: 3.1.2
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: Verification of comments for roughly designed mixin module
164
+ test_files: []