arpry 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -0
- data/README.md +2 -0
- data/Rakefile +8 -1
- data/arpry.gemspec +6 -2
- data/lib/arpry.rb +2 -5
- data/lib/arpry/class_factory.rb +93 -0
- data/lib/arpry/cli.rb +4 -61
- data/lib/arpry/version.rb +1 -1
- metadata +52 -10
- data/lib/arpry/application_record.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a10e37ba15471d56dfb1847fae73470610997d3f5210a766456ab2f717ac6ebf
|
4
|
+
data.tar.gz: eabcd01f8365e9562844cba4b893d17c72770928c41d8a1fc1b56ce9684a7493
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06ea25b67324d0e7f7290841ebbdb48067d7ef60058149d9fc155ab120a173c70708b0ae790f9f3cf2f33e32c190d55be3c10b00a8b1ee289dc28a930fd9807d
|
7
|
+
data.tar.gz: b1e58271d44691ebb9906e662e17717efd9258a22d880937c495413a89eee793490f62c7f33d1d63cb5b8c6becd6f9c8d0b7db72406ae45b8841bf61b858ccd7
|
data/.travis.yml
ADDED
data/README.md
CHANGED
data/Rakefile
CHANGED
data/arpry.gemspec
CHANGED
@@ -22,6 +22,8 @@ Gem::Specification.new do |spec|
|
|
22
22
|
"public gem pushes."
|
23
23
|
end
|
24
24
|
|
25
|
+
spec.required_ruby_version = '>= 2.4'
|
26
|
+
|
25
27
|
# Specify which files should be added to the gem when it is released.
|
26
28
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
27
29
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
@@ -34,6 +36,8 @@ Gem::Specification.new do |spec|
|
|
34
36
|
spec.add_runtime_dependency 'activerecord', '< 6', '>= 5'
|
35
37
|
spec.add_runtime_dependency 'pry'
|
36
38
|
|
37
|
-
spec.add_development_dependency "bundler", "
|
38
|
-
spec.add_development_dependency "rake", "~>
|
39
|
+
spec.add_development_dependency "bundler", ">= 1", "<= 2"
|
40
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
41
|
+
spec.add_development_dependency "minitest", ">= 5", '< 6'
|
42
|
+
spec.add_development_dependency "sqlite3"
|
39
43
|
end
|
data/lib/arpry.rb
CHANGED
@@ -2,16 +2,13 @@ require 'active_record'
|
|
2
2
|
require 'pry'
|
3
3
|
require 'logger'
|
4
4
|
require 'optparse'
|
5
|
+
require 'securerandom'
|
5
6
|
|
6
7
|
require "arpry/version"
|
7
8
|
require 'arpry/cli'
|
8
9
|
require 'arpry/logger'
|
9
|
-
require 'arpry/
|
10
|
+
require 'arpry/class_factory'
|
10
11
|
|
11
12
|
module Arpry
|
12
13
|
class Error < StandardError; end
|
13
|
-
|
14
|
-
# Namespace for automatically generated classes
|
15
|
-
module Namespace
|
16
|
-
end
|
17
14
|
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module Arpry
|
2
|
+
class ClassFactory
|
3
|
+
def self.create(conn_option)
|
4
|
+
self.new.run(conn_option)
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
end
|
9
|
+
|
10
|
+
def run(conn_option)
|
11
|
+
base = generate_base_class(conn_option)
|
12
|
+
|
13
|
+
namespace = generate_namespace
|
14
|
+
classes = generate_classes(base, namespace)
|
15
|
+
define_foreign_keys(classes)
|
16
|
+
|
17
|
+
namespace
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def generate_namespace
|
23
|
+
# HACK: the namespace must have a name.
|
24
|
+
mod_name = 'Namespace' + SecureRandom.hex(4)
|
25
|
+
Module.new.tap do |mod|
|
26
|
+
self.class.const_set(mod_name, mod)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def generate_base_class(conn_option)
|
31
|
+
# HACK: the base class must have a name.
|
32
|
+
class_name = 'BaseClass' + SecureRandom.hex(4)
|
33
|
+
Class.new(ActiveRecord::Base).tap do |klass|
|
34
|
+
klass.logger = Arpry::Logger.logger
|
35
|
+
self.class.const_set(class_name, klass)
|
36
|
+
klass.establish_connection(conn_option.presence)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def generate_classes(base_class, namespace)
|
41
|
+
base_class.connection.tables.map do |table|
|
42
|
+
namespace.const_set(table.classify, Class.new(base_class) do
|
43
|
+
self.table_name = table
|
44
|
+
end)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def define_foreign_keys(classes)
|
49
|
+
relations = Array.new(classes.size) do
|
50
|
+
Array.new(classes.size)
|
51
|
+
end
|
52
|
+
|
53
|
+
classes.each.with_index do |klass, idx|
|
54
|
+
klass.columns.each do |col|
|
55
|
+
ref_name = col.name[/\A(.+)(_id|ID)\z/, 1]
|
56
|
+
next unless ref_name
|
57
|
+
ref_klass_idx = classes.find_index {|c| c.table_name == ref_name.singularize || c.table_name == ref_name.pluralize}
|
58
|
+
next unless ref_klass_idx
|
59
|
+
|
60
|
+
relations[idx][ref_klass_idx] = col.name
|
61
|
+
end
|
62
|
+
|
63
|
+
klass.connection.foreign_keys(klass.table_name).each do |fk|
|
64
|
+
ref_klass_idx = classes.find_index {|c| c.table_name == fk.to_table }
|
65
|
+
next unless ref_klass_idx
|
66
|
+
relations[idx][ref_klass_idx] = fk.options[:column]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
relations.each.with_index do |row, idx|
|
71
|
+
klass = classes[idx]
|
72
|
+
exists = row.map.with_index.select {|fk, _idx| fk}
|
73
|
+
|
74
|
+
exists.each do |fk, ref_klass_idx|
|
75
|
+
next unless fk
|
76
|
+
|
77
|
+
ref_klass = classes[ref_klass_idx]
|
78
|
+
|
79
|
+
klass.belongs_to ref_klass.table_name.singularize.to_sym, foreign_key: fk
|
80
|
+
ref_klass.has_many klass.table_name.pluralize.to_sym, foreign_key: fk
|
81
|
+
end
|
82
|
+
|
83
|
+
# from -> klass <- to
|
84
|
+
exists.permutation(2).each do |from, to|
|
85
|
+
from_klass = classes[from[1]]
|
86
|
+
to_fk = to[0]
|
87
|
+
to_klass = classes[to[1]]
|
88
|
+
from_klass.has_many to_klass.table_name.pluralize.to_sym, foreign_key: to_fk, through: klass.table_name.pluralize.to_sym
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/lib/arpry/cli.rb
CHANGED
@@ -9,12 +9,8 @@ module Arpry
|
|
9
9
|
def run
|
10
10
|
parse_options
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
classes = generate_classes
|
15
|
-
define_foreign_keys(classes)
|
16
|
-
|
17
|
-
binding.pry(Namespace)
|
12
|
+
namespace = ClassFactory.create(@params)
|
13
|
+
binding.pry(namespace)
|
18
14
|
|
19
15
|
return 0
|
20
16
|
end
|
@@ -31,63 +27,10 @@ module Arpry
|
|
31
27
|
@params = {}
|
32
28
|
args = opt.parse(@argv, into: @params)
|
33
29
|
|
34
|
-
@params[:database] ||= args[0]
|
35
|
-
if File.exist?(@params[:database])
|
30
|
+
@params[:database] ||= args[0] if args.present?
|
31
|
+
if @params[:database] && File.exist?(@params[:database])
|
36
32
|
@params[:adapter] ||= 'sqlite3'
|
37
33
|
end
|
38
34
|
end
|
39
|
-
|
40
|
-
def generate_classes
|
41
|
-
ApplicationRecord.connection.tables.map do |table|
|
42
|
-
Namespace.const_set(table.classify, Class.new(ApplicationRecord) do
|
43
|
-
self.table_name = table
|
44
|
-
end)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def define_foreign_keys(classes)
|
49
|
-
relations = Array.new(classes.size) do
|
50
|
-
Array.new(classes.size)
|
51
|
-
end
|
52
|
-
|
53
|
-
classes.each.with_index do |klass, idx|
|
54
|
-
klass.columns.each do |col|
|
55
|
-
ref_name = col.name[/\A(.+)(_id|ID)\z/, 1]
|
56
|
-
next unless ref_name
|
57
|
-
ref_klass_idx = classes.find_index {|c| c.table_name == ref_name.singularize || c.table_name == ref_name.pluralize}
|
58
|
-
next unless ref_klass_idx
|
59
|
-
|
60
|
-
relations[idx][ref_klass_idx] = col.name
|
61
|
-
end
|
62
|
-
|
63
|
-
klass.connection.foreign_keys(klass.table_name).each do |fk|
|
64
|
-
ref_klass_idx = classes.find_index {|c| c.table_name == fk.to_table }
|
65
|
-
next unless ref_klass_idx
|
66
|
-
relations[idx][ref_klass_idx] = fk.options[:column]
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
relations.each.with_index do |row, idx|
|
71
|
-
klass = classes[idx]
|
72
|
-
exists = row.map.with_index.select {|fk, _idx| fk}
|
73
|
-
|
74
|
-
exists.each do |fk, ref_klass_idx|
|
75
|
-
next unless fk
|
76
|
-
|
77
|
-
ref_klass = classes[ref_klass_idx]
|
78
|
-
|
79
|
-
klass.belongs_to ref_klass.table_name.singularize.to_sym, foreign_key: fk
|
80
|
-
ref_klass.has_many klass.table_name.pluralize.to_sym, foreign_key: fk
|
81
|
-
end
|
82
|
-
|
83
|
-
# from -> klass <- to
|
84
|
-
exists.permutation(2).each do |from, to|
|
85
|
-
from_klass = classes[from[1]]
|
86
|
-
to_fk = to[0]
|
87
|
-
to_klass = classes[to[1]]
|
88
|
-
from_klass.has_many to_klass.table_name.pluralize.to_sym, foreign_key: to_fk, through: klass.table_name.pluralize.to_sym
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
35
|
end
|
93
36
|
end
|
data/lib/arpry/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arpry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masataka Pocke Kuwabara
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01-
|
11
|
+
date: 2019-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -48,30 +48,70 @@ dependencies:
|
|
48
48
|
name: bundler
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- - "
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1'
|
54
|
+
- - "<="
|
52
55
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
56
|
+
version: '2'
|
54
57
|
type: :development
|
55
58
|
prerelease: false
|
56
59
|
version_requirements: !ruby/object:Gem::Requirement
|
57
60
|
requirements:
|
58
|
-
- - "
|
61
|
+
- - ">="
|
59
62
|
- !ruby/object:Gem::Version
|
60
|
-
version: '1
|
63
|
+
version: '1'
|
64
|
+
- - "<="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '2'
|
61
67
|
- !ruby/object:Gem::Dependency
|
62
68
|
name: rake
|
63
69
|
requirement: !ruby/object:Gem::Requirement
|
64
70
|
requirements:
|
65
71
|
- - "~>"
|
66
72
|
- !ruby/object:Gem::Version
|
67
|
-
version: '
|
73
|
+
version: '12.0'
|
68
74
|
type: :development
|
69
75
|
prerelease: false
|
70
76
|
version_requirements: !ruby/object:Gem::Requirement
|
71
77
|
requirements:
|
72
78
|
- - "~>"
|
73
79
|
- !ruby/object:Gem::Version
|
74
|
-
version: '
|
80
|
+
version: '12.0'
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: minitest
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '5'
|
88
|
+
- - "<"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '6'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '5'
|
98
|
+
- - "<"
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '6'
|
101
|
+
- !ruby/object:Gem::Dependency
|
102
|
+
name: sqlite3
|
103
|
+
requirement: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
type: :development
|
109
|
+
prerelease: false
|
110
|
+
version_requirements: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
75
115
|
description: Explore database without Rails
|
76
116
|
email:
|
77
117
|
- kuwabara@pocke.me
|
@@ -81,6 +121,7 @@ extensions: []
|
|
81
121
|
extra_rdoc_files: []
|
82
122
|
files:
|
83
123
|
- ".gitignore"
|
124
|
+
- ".travis.yml"
|
84
125
|
- Gemfile
|
85
126
|
- LICENSE
|
86
127
|
- README.md
|
@@ -90,10 +131,11 @@ files:
|
|
90
131
|
- bin/setup
|
91
132
|
- exe/arpry
|
92
133
|
- lib/arpry.rb
|
93
|
-
- lib/arpry/
|
134
|
+
- lib/arpry/class_factory.rb
|
94
135
|
- lib/arpry/cli.rb
|
95
136
|
- lib/arpry/logger.rb
|
96
137
|
- lib/arpry/version.rb
|
138
|
+
- tmp/.keep
|
97
139
|
homepage: https://github.com/pocke/arpry
|
98
140
|
licenses: []
|
99
141
|
metadata:
|
@@ -106,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
106
148
|
requirements:
|
107
149
|
- - ">="
|
108
150
|
- !ruby/object:Gem::Version
|
109
|
-
version: '
|
151
|
+
version: '2.4'
|
110
152
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
153
|
requirements:
|
112
154
|
- - ">="
|