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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc115cecaf26c426cc0eb12660c865579941de482cd5c04b1b757eb62038ca6e
4
- data.tar.gz: e29210c78f1a7d2685f14d1ac14f2ac2029e7b3ea25f298d64fed038eb12ec41
3
+ metadata.gz: a10e37ba15471d56dfb1847fae73470610997d3f5210a766456ab2f717ac6ebf
4
+ data.tar.gz: eabcd01f8365e9562844cba4b893d17c72770928c41d8a1fc1b56ce9684a7493
5
5
  SHA512:
6
- metadata.gz: 19b2a6032392028d8efa6bf37265b6fbc4fed08bdaa65a0b4486d4d7f877f15e016c79e3079aa4520e3865c385214d8ad16293be8080f891cfa13f333d18c388
7
- data.tar.gz: 12794da7d0774a2c48fda0df77fd03bd5de3dc9bd68ac86a24ec97dc18948941e1496e2a1894a922e0131cff6e89b713ffb8a0af5e13293ce0700a4d4387c742
6
+ metadata.gz: 06ea25b67324d0e7f7290841ebbdb48067d7ef60058149d9fc155ab120a173c70708b0ae790f9f3cf2f33e32c190d55be3c10b00a8b1ee289dc28a930fd9807d
7
+ data.tar.gz: b1e58271d44691ebb9906e662e17717efd9258a22d880937c495413a89eee793490f62c7f33d1d63cb5b8c6becd6f9c8d0b7db72406ae45b8841bf61b858ccd7
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4.5
4
+ - 2.5.3
5
+ - 2.6.0
data/README.md CHANGED
@@ -11,6 +11,8 @@ You can get the same experience as Rails Console to explore database with not-Ra
11
11
 
12
12
  ## Installation
13
13
 
14
+ Requirements: Ruby 2.4 or higher
15
+
14
16
  ```bash
15
17
  $ gem install arpry
16
18
 
data/Rakefile CHANGED
@@ -1,2 +1,9 @@
1
1
  require "bundler/gem_tasks"
2
- task :default => :spec
2
+ require 'rake/testtask'
3
+ task :default => :test
4
+
5
+ Rake::TestTask.new do |test|
6
+ test.libs << 'test'
7
+ test.test_files = Dir['test/**/test_*.rb']
8
+ test.verbose = true
9
+ end
@@ -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", "~> 1.17"
38
- spec.add_development_dependency "rake", "~> 10.0"
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
@@ -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/application_record'
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
@@ -9,12 +9,8 @@ module Arpry
9
9
  def run
10
10
  parse_options
11
11
 
12
- ApplicationRecord.establish_connection(@params)
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
@@ -1,3 +1,3 @@
1
1
  module Arpry
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
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.3.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-04 00:00:00.000000000 Z
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: '1.17'
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.17'
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: '10.0'
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: '10.0'
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/application_record.rb
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: '0'
151
+ version: '2.4'
110
152
  required_rubygems_version: !ruby/object:Gem::Requirement
111
153
  requirements:
112
154
  - - ">="
@@ -1,5 +0,0 @@
1
- module Arpry
2
- class ApplicationRecord < ActiveRecord::Base
3
- self.logger = Arpry::Logger.logger
4
- end
5
- end