depend_lister 0.1.0 → 0.1.1

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: 28b837e8b1568b3735dda47dd12cedd738279eb87a5ef90ffb0aacaf93735a44
4
- data.tar.gz: 54d092e6f18a2555368458eed0eaf58064133724706842eea0704474706dee12
3
+ metadata.gz: 748b8e0c6661dba50f1ff1dca1302bc40f4cde0ff0169b16f0f9f349a30a1743
4
+ data.tar.gz: bd424a38f6cce4d542795b048604152a9ae12ca00026dfab0d43aad8247b6419
5
5
  SHA512:
6
- metadata.gz: e62e7dc7bc259b27844431c0485e409126e1c6b822a8e5f38fea0c341e3655b6b6819067704d3c4d192840bba691a7a8d85c329c548e855978b945c934324863
7
- data.tar.gz: caf9eb8418d5235ad4f3eb431968e65655d6fa5c8e636c21af1e7298ab529d258f086f2c2affc37008b31480555010cbc262a9dfa77a246d1d40912b93799919
6
+ metadata.gz: 68e795e7ba69f213e980e8b5ca9a0de7a223584612cd972187897d4cfdad0b4787b18c135c55c5c99fdd5a2872d7b7522168dbbfed12a369ed010d066c7ba450
7
+ data.tar.gz: 8375045722cb62eb85a0f805b5449c63e53d2892f647ca0519ed5ca67c0070a78f003dbb9885114bb69e230f9d50307dc47667e6a5e1fbe7ef7f3b222f9584a6
@@ -0,0 +1,24 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ build:
11
+
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby 2.6
17
+ uses: actions/setup-ruby@v1
18
+ with:
19
+ ruby-version: 2.6.x
20
+ - name: Build and test with Rake
21
+ run: |
22
+ gem install bundler
23
+ bundle install --jobs 4 --retry 3
24
+ bundle exec rake
data/Gemfile CHANGED
@@ -3,5 +3,13 @@ source "https://rubygems.org"
3
3
  # Specify your gem's dependencies in depend_lister.gemspec
4
4
  gemspec
5
5
 
6
- gem "rake", "~> 12.0"
7
- gem "rspec", "~> 3.0"
6
+ gem "rake", "~> 12.3.3"
7
+
8
+ group :development, :test do
9
+ gem "pry"
10
+ gem "pry-byebug"
11
+ end
12
+
13
+ group :test do
14
+ gem "rspec", "~> 3.0"
15
+ end
@@ -6,7 +6,16 @@ PATH
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ byebug (11.1.1)
10
+ coderay (1.1.2)
9
11
  diff-lcs (1.3)
12
+ method_source (1.0.0)
13
+ pry (0.13.0)
14
+ coderay (~> 1.1)
15
+ method_source (~> 1.0)
16
+ pry-byebug (3.9.0)
17
+ byebug (~> 11.0)
18
+ pry (~> 0.13.0)
10
19
  rake (12.3.3)
11
20
  rspec (3.9.0)
12
21
  rspec-core (~> 3.9.0)
@@ -28,8 +37,10 @@ PLATFORMS
28
37
  DEPENDENCIES
29
38
  bundler (~> 2.1)
30
39
  depend_lister!
31
- rake (~> 12.0)
40
+ pry
41
+ pry-byebug
42
+ rake (~> 12.3.3)
32
43
  rspec (~> 3.0)
33
44
 
34
45
  BUNDLED WITH
35
- 2.1.0
46
+ 2.1.4
data/README.md CHANGED
@@ -1,39 +1,65 @@
1
1
  # DependLister
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/depend_lister`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ DependLister analyzes belongs_to defined by a model and outputs a list of tables in order with a little dependence.
4
+ When You begin the maintenance of the existing project, it may be helpful.
6
5
 
7
6
  ## Installation
8
7
 
9
- Add this line to your application's Gemfile:
8
+ To use it, add it to your Gemfile:
10
9
 
11
10
  ```ruby
12
11
  gem 'depend_lister'
13
12
  ```
14
13
 
15
- And then execute:
14
+ and bundle:
15
+
16
+ ```shell
17
+ bundle
18
+ ```
16
19
 
17
- $ bundle install
20
+ #### Post Installation
18
21
 
19
- Or install it yourself as:
22
+ Install migrations:
20
23
 
21
- $ gem install depend_lister
24
+ ```shell
25
+ rake db:migrate
26
+ ```
22
27
 
23
28
  ## Usage
24
29
 
25
- TODO: Write usage instructions here
30
+ ```shell
31
+ rails depend_lister
32
+ ```
26
33
 
27
- ## Development
34
+ ### Example of Result
35
+
36
+ ```shell
37
+ $ rails depend_lister
38
+ Level Table BelongsTo
39
+ Lv1 accounts
40
+ Lv2 follows accounts
41
+ Lv2 statuses accounts
42
+ Lv2 users accounts
43
+ Lv3 favourites accounts, statuses
44
+ Lv3 mentions accounts, statuses
45
+ Lv3 stream_entries accounts, statuses
46
+ ```
28
47
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+ You can copy the result and paste to Excel Sheet or Google Spread Sheet.
30
49
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50
+ | Level | Table | BelongsTo |
51
+ | --- | --- | --- |
52
+ | Lv1 | accounts | |
53
+ | Lv2 | follows | accounts |
54
+ | Lv2 | statuses | accounts |
55
+ | Lv2 | users | accounts |
56
+ | Lv3 | favourites | accounts, statuses |
57
+ | Lv3 | mentions | accounts, statuses |
58
+ | Lv3 | stream_entries | accounts, statuses |
32
59
 
33
60
  ## Contributing
34
61
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/depend_lister. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/depend_lister/blob/master/CODE_OF_CONDUCT.md).
36
-
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kayamak/depend_lister. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/kayamak/depend_lister).
37
63
 
38
64
  ## License
39
65
 
@@ -41,4 +67,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
41
67
 
42
68
  ## Code of Conduct
43
69
 
44
- Everyone interacting in the DependLister project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/depend_lister/blob/master/CODE_OF_CONDUCT.md).
70
+ Everyone interacting in the DependLister project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/kayamak/depend_lister).
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.require_paths = ["lib"]
29
29
 
30
30
  spec.add_development_dependency "bundler", "~> 2.1"
31
- spec.add_development_dependency "rake", '~> 0'
31
+ spec.add_development_dependency "rake", ">= 12.3.3"
32
32
  spec.add_development_dependency "rspec", '~> 0'
33
+ spec.add_development_dependency "pry"
33
34
  end
@@ -0,0 +1,43 @@
1
+ class Belong
2
+ # Generates the hash(key: table name, value: belongs_to point table name).
3
+ def to_table_belongs_hash
4
+ # Generates the hash(key: table name, value: model).
5
+ table_model_hash = to_table_model_hash
6
+
7
+ all_tables = table_model_hash.keys
8
+ belogs = {}
9
+ table_model_hash.each do |table, model|
10
+ next unless model
11
+ # the model which does not exist is ignored.
12
+ tables = belong_tables(model)
13
+ # models of alias are exclusion.
14
+ belogs[table] = tables & all_tables
15
+ end
16
+ belogs
17
+ end
18
+
19
+ private
20
+
21
+ # Generates the hash(key: table name, value: model name).
22
+ def make_table_model_hash
23
+ tables = gain_tables
24
+ models = to_models(tables)
25
+ Hash[*tables.zip(models).flatten]
26
+ end
27
+
28
+ # Gains table names.
29
+ def gain_tables
30
+ ActiveRecord::Base.connection.tables.sort
31
+ end
32
+
33
+ def to_models(tables)
34
+ tables.map{ |table| Object.const_get(table.classify) rescue nil }
35
+ end
36
+
37
+ # Gains the table name of the belongs_to point of the model.
38
+ def belong_tables(model)
39
+ model.reflect_on_all_associations(:belongs_to).map do |belong|
40
+ belong.name.to_s.pluralize
41
+ end.sort
42
+ end
43
+ end
@@ -0,0 +1,172 @@
1
+ require 'depend_lister/belong'
2
+ class DependListerCore
3
+ LEVEL_LIMITTER = 1000
4
+
5
+ def execute
6
+ make_table_belongs_hash
7
+
8
+ level_tables_hash = to_level_tables_hash_main!
9
+
10
+ adjust_circulation_main!(level_tables_hash)
11
+
12
+ displayed = display_hash(level_tables_hash)
13
+ end
14
+
15
+ private
16
+
17
+ def make_table_belongs_hash
18
+ @table_belongs_hash = Belong.new.make_table_belongs_hash
19
+ @table_belongs_hash_bak = @table_belongs_hash.dup
20
+ end
21
+
22
+ def adjust_circulation_main!(level_tables_hash)
23
+ loop do
24
+ adjust_circulation!(level_tables_hash)
25
+ break if @table_belongs_hash.empty?
26
+ if level_tables_hash.keys.max > LEVEL_LIMITTER
27
+ puts 'over loop!'
28
+ break
29
+ end
30
+ end
31
+ end
32
+
33
+ def adjust_circulation!(level_tables_hash)
34
+ unless @table_belongs_hash.empty?
35
+ extract_no_belong!
36
+ table, blong = find_circulation
37
+ if table
38
+ @table_belongs_hash[table] = []
39
+ @table_belongs_hash[blong] = []
40
+ next_level = level_tables_hash.keys.max + 1
41
+ next_level_tables_hash = to_level_tables_hash_main!(next_level)
42
+ level_tables_hash.merge!(next_level_tables_hash)
43
+ end
44
+ end
45
+ end
46
+
47
+ def extract_belogs_to_self!
48
+ @table_belongs_hash.each do |table, belongs|
49
+
50
+ if belongs.include?(table)
51
+ @table_belongs_hash[table] = belongs - [table]
52
+ end
53
+ end
54
+ end
55
+
56
+ def to_level_tables_hash_main!(level=1)
57
+ extract_belogs_to_self!
58
+ level_tables_hash = {}
59
+ top_tables = to_top_tables!
60
+ level_tables_hash[level] = top_tables
61
+ loop do
62
+ level += 1
63
+ next_hash = to_level_tables_hash!(level, level_tables_hash)
64
+ level_tables_hash.merge!(next_hash)
65
+ break if level_tables_hash[level].empty?
66
+ if level > LEVEL_LIMITTER
67
+ puts 'over loop!!'
68
+ break
69
+ end
70
+ end
71
+ level_tables_hash
72
+ end
73
+
74
+ def extract_no_belong!
75
+ tables = @table_belongs_hash.keys
76
+ @table_belongs_hash.each do |table, belongs|
77
+ @table_belongs_hash[table] = belongs.select { |belong| tables.include?(belong) }
78
+ end
79
+ end
80
+
81
+ def find_circulation
82
+ tables = @table_belongs_hash.keys
83
+ @table_belongs_hash.each do |table, belongs|
84
+ belongs.each do |belong|
85
+ # the belong without a reference point is inapplicable.
86
+ next unless tables.include?(belong)
87
+ # belog with a reference point.
88
+ next_belongs = @table_belongs_hash[belong]
89
+ if next_belongs.include?(table)
90
+ # When former table is included in reference point belog
91
+ return [table, belong].sort
92
+ end
93
+ end
94
+ end
95
+ return [nil, nil]
96
+ end
97
+
98
+ def to_top_tables!
99
+ # Gains table names without belongs_to
100
+ top_tables = @table_belongs_hash.select do |table, belongs|
101
+ if belongs.empty?
102
+ @table_belongs_hash.delete(table)
103
+ end
104
+ belongs.empty?
105
+ end.keys
106
+ top_tables.sort
107
+ end
108
+
109
+ # Converts it into the hash(key: level, value: table name).
110
+ def to_level_tables_hash!(level, level_tables_hash)
111
+ # Gains table names of level-1
112
+ prev_levels = []
113
+ (2..level).each do |level|
114
+ prev_levels << level_tables_hash.fetch(level-1, [])
115
+ end
116
+ prev_levels.flatten!
117
+
118
+ # A table name of level-1 selects
119
+ # the level of the table name of
120
+ # the belongt_to point as level
121
+ @table_belongs_hash.each do |table, belongs|
122
+ found = false
123
+ belongs.each do |belong|
124
+ if prev_levels.include?(belong)
125
+ found = true
126
+ break
127
+ end
128
+ end
129
+ if found
130
+ level_tables_hash[level] ||= []
131
+ level_tables_hash[level] << table
132
+ end
133
+ end
134
+ # Gains the table name that belongs of
135
+ # @table_belongs_hash was included in
136
+ # the table of the previous level and
137
+ # exclude it from level_tables_hash[level]
138
+ extract_tables = []
139
+ @table_belongs_hash.each do |table, belongs|
140
+ unless belongs.to_set.subset?(prev_levels.to_set)
141
+ # When all belongs is not included in prev_levels
142
+ extract_tables << table
143
+ end
144
+ end
145
+ level_tables_hash[level] ||= []
146
+ level_tables_hash[level] -= extract_tables
147
+
148
+ level_tables_hash[level].each do |table|
149
+ @table_belongs_hash.delete(table)
150
+ end
151
+ level_tables_hash
152
+ end
153
+
154
+ def debug_hash(hash)
155
+ hash.each{|key, value| puts "#{key}:#{value}" }
156
+ end
157
+
158
+ def display_hash(level_tables_hash)
159
+ displayes = []
160
+ displayes << "Level\tTable\tBelongsTo"
161
+ level_tables_hash.each do |level, tables|
162
+ tables.each do |table|
163
+ blongs = @table_belongs_hash_bak[table].join(', ')
164
+ displayes << "Lv#{level}\t#{table}\t#{blongs}"
165
+ end
166
+ displayes << "Lv#{level}\t\t" if tables.empty?
167
+ end
168
+ result = displayes.join("\n")
169
+ puts result
170
+ result
171
+ end
172
+ end
@@ -1,3 +1,3 @@
1
1
  module DependLister
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,143 +1,7 @@
1
- desc "依存関係順のテーブル一覧"
1
+ require "depend_lister/depend_lister_core"
2
+
3
+ desc "Table list of dependence order"
2
4
  task depend_lister: :environment do
3
- DependListerMain.new.execute
5
+ DependListerCore.new.execute
4
6
  end
5
7
 
6
- class DependListerMain
7
- def execute
8
- tables = gain_tables
9
- models = to_models(tables)
10
- # テーブル名がキーで値がモデルのハッシュを生成
11
- table_model_hash = to_table_model_hash(tables, models)
12
- # テーブル名がキーで値がbelongs_to先テーブル名のハッシュを生成
13
- table_belongs_hash = to_table_belongs_hash(table_model_hash, tables)
14
- #debug_hash(table_belongs_hash)
15
- # レベルがキーで値がテーブル名のハッシュを生成
16
- level_table_hash = to_level_table_hash_main(table_belongs_hash)
17
- # レベルを整理する
18
- organized_level_table_hash = organize_level(level_table_hash, table_belongs_hash)
19
- display_hash(organized_level_table_hash, table_belongs_hash)
20
- end
21
-
22
- private
23
-
24
- def organize_level(level_table_hash, table_belongs_hash)
25
- organized_level_table_hash = {}
26
- pre_tables = []
27
- level_table_hash.each do |level, tables|
28
- if level == 1
29
- organized_level_table_hash[level] = tables
30
- pre_tables = tables
31
- next
32
- end
33
- exist_belongs = []
34
- tables.each do |table|
35
- belongs = table_belongs_hash.fetch(table, [])
36
- exist_belongs << belongs.find{ |belong| pre_tables.include?(belong) }
37
- end
38
- exist_belongs.compact!
39
- if exist_belongs.empty?
40
- level_tables = organized_level_table_hash[level-1] || []
41
- level_tables << tables
42
- organized_level_table_hash[level-1] = level_tables.flatten
43
- pre_tables << tables
44
- else
45
- organized_level_table_hash[level] = tables
46
- pre_tables = tables
47
- end
48
- end
49
- organized_level_table_hash
50
- end
51
-
52
- def to_level_table_hash_main(arg_table_belongs_hash)
53
- table_belongs_hash = arg_table_belongs_hash.dup
54
- level = 0;
55
- level_table_hash = {}
56
- loop do
57
- level += 1
58
- level_table_hash.merge!(to_level_table_hash!(table_belongs_hash, level))
59
- break if table_belongs_hash.empty?
60
- raise 'over loop' if level > 99
61
- end
62
- level_table_hash
63
- end
64
-
65
- # テーブル名を取得
66
- def gain_tables
67
- ActiveRecord::Base.connection.tables.sort
68
- end
69
-
70
- def to_models(tables)
71
- tables.map{ |table| Object.const_get(table.classify) rescue nil }
72
- end
73
-
74
- # テーブル名がキーで値がモデルのハッシュを生成
75
- def to_table_model_hash(tables, models)
76
- Hash[*tables.zip(models).flatten]
77
- end
78
-
79
- # テーブル名がキーで値がモデルのハッシュを生成
80
- def models_per_table(tables, models)
81
- Hash[*tables.zip(models).flatten]
82
- end
83
-
84
- # テーブル名がキーで値がbelongs_to先テーブルのハッシュを生成
85
- def to_table_belongs_hash(table_model_hash, all_tables)
86
- belogs = {}
87
- table_model_hash.each do |table, model|
88
- next unless model # モデルが存在しないtableは無視する
89
- tables = belong_tables(model)
90
- # 別名のmodelやparents(自身のモデルをbelongs_toのみしているものは)は除外
91
- belogs[table] = tables & all_tables
92
- end
93
- belogs
94
- end
95
-
96
- # モデルのbelongs_to先のテーブル名を取得
97
- def belong_tables(model)
98
- model.reflect_on_all_associations(:belongs_to).map do |belong|
99
- belong.name.to_s.pluralize
100
- end.sort
101
- end
102
-
103
- # ハッシュが空ではなかったら1.から繰り返す。
104
- def to_level_table_hash!(table_belongs_hash, level)
105
- level_table_hash = {}
106
- # 1. ハッシュの値が空のテーブル名のテーブル名を空テーブル名を取得する。
107
- top_tables = table_belongs_hash.select do |table, belongs|
108
- belongs.empty?
109
- end.keys
110
- if top_tables.empty?
111
- top_tables = table_belongs_hash.select do |table, belongs|
112
- belongs.size == 1
113
- end.keys
114
- end
115
- # 2. 空テーブル名をレベルiとする。
116
- level_table_hash[level] = top_tables
117
- # 3. ハッシュから空テーブル名をキーにして除去する。
118
- top_tables.each do |table|
119
- table_belongs_hash.delete(table)
120
- end
121
- # 4. ハッシュの値から空テーブル名を削除する。
122
- tables = table_belongs_hash.keys
123
- tables.each do |table|
124
- belongs = table_belongs_hash[table]
125
- table_belongs_hash[table] = belongs - top_tables
126
- end
127
- level_table_hash
128
- end
129
-
130
- def debug_hash(hash)
131
- hash.each{|key, value| puts "#{key}:#{value}" }
132
- end
133
-
134
- def display_hash(level_table_hash, table_belongs_hash)
135
- puts "Level\tTable\tBelongsTo"
136
- level_table_hash.each do |level, tables|
137
- tables.each do |table|
138
- blongs = table_belongs_hash[table].join(', ')
139
- puts "Lv#{level}\t#{table}\t#{blongs}"
140
- end
141
- end
142
- end
143
- end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: depend_lister
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keiichi Kayama
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-20 00:00:00.000000000 Z
11
+ date: 2020-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -26,6 +26,20 @@ dependencies:
26
26
  version: '2.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
@@ -39,17 +53,17 @@ dependencies:
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
- name: rspec
56
+ name: pry
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - "~>"
59
+ - - ">="
46
60
  - !ruby/object:Gem::Version
47
61
  version: '0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - "~>"
66
+ - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  description: depend_lister is a command line tool which displays a list sorted by
@@ -61,6 +75,7 @@ extensions: []
61
75
  extra_rdoc_files: []
62
76
  files:
63
77
  - ".gem/credentials"
78
+ - ".github/workflows/ruby.yml"
64
79
  - ".gitignore"
65
80
  - ".rspec"
66
81
  - ".travis.yml"
@@ -74,6 +89,8 @@ files:
74
89
  - bin/setup
75
90
  - depend_lister.gemspec
76
91
  - lib/depend_lister.rb
92
+ - lib/depend_lister/belong.rb
93
+ - lib/depend_lister/depend_lister_core.rb
77
94
  - lib/depend_lister/version.rb
78
95
  - lib/tasks/depend_lister.rake
79
96
  homepage: https://github.com/kayamak/depend_lister