pry-golumn 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3d71045695f26c88b8000e98cb693b41b5001d76
4
+ data.tar.gz: 21c8cb81a4d34a20b20770c6afef093ee0ffedd7
5
+ SHA512:
6
+ metadata.gz: f8287a89ba2bdd10a6a1578401661a82fdad72fc5717e2e8d617328b2924bdc8b791bbabd77178a5edd8fcd4ca9533a138142c14c89c5334bca8cd7bdfdaff5a
7
+ data.tar.gz: acb7ba1b71a462982262e01ab1cd8081f0204d095be0848bdf86a4670cb4181b41cf0e108502cec343216107faaea30ecfcff3b71fe180c0025791ad70af26b8
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.16.0.pre.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in pry-golumn.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Scott Pierce
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,72 @@
1
+ # Pry Golumn
2
+
3
+ Send results of raw SQL or command which responds to `to_sql` or `all.to_sql` to `golumn`.
4
+
5
+ Note: This only works under Postgres additional DB adapters maybe be created in the future.
6
+
7
+ ## Usage
8
+
9
+ ```ruby
10
+ # run plan SQL
11
+ golumn select * from pg_tables
12
+
13
+
14
+ # run model class
15
+ golumn People
16
+
17
+ # run model scope
18
+ golumn People.addresses
19
+ golumn People.where(zipcode: '60000')
20
+ ```
21
+
22
+ <img src="https://user-images.githubusercontent.com/1975119/32610351-21120398-c528-11e7-8fab-5526988cc515.gif" alt="demo"/>
23
+
24
+ ## Install Golumn Command
25
+
26
+ You'll need a working Python environment to get the `golumn` command
27
+ ```sh
28
+ pip install golumn --upgrade
29
+ ```
30
+
31
+ ## Installation without Gemfile
32
+
33
+ Clone this repository to local file system and load the file via `.pryrc`
34
+
35
+ ```sh
36
+ # clone the repo
37
+ cd /path/to/code
38
+ git clone https://github.com/ddrscott/pry-golumn.git
39
+
40
+ # be sure to replace `path/to/pry-golumn` with the real path
41
+ tee -a ~/.pryrc <<EOF
42
+ load '/path/to/pry-golumn/lib/pry-golumn.rb'
43
+ EOF
44
+ ```
45
+
46
+ ## Installation via Rubygems
47
+
48
+ ```ruby
49
+ gem 'pry-golumn'
50
+ ```
51
+
52
+ And then execute:
53
+
54
+ $ bundle
55
+
56
+ Or install it yourself as:
57
+
58
+ $ gem install pry-golumn
59
+
60
+ ## Development
61
+
62
+ 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.
63
+
64
+ 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).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ddrscott/pry-golumn.
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](https://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/lib/pry-golumn.rb ADDED
@@ -0,0 +1,62 @@
1
+ require 'tempfile'
2
+
3
+ Pry::Commands.create_command 'golumn', keep_retval: true, use_shellwords: false do |text|
4
+ description 'Send SQL or scope to `psql` and then to `golumn`: golumn Model.all'
5
+
6
+ banner <<-BANNER
7
+ Usage: golumn [SQL or Ruby which evaluates to String or a Rails Model]
8
+
9
+ Saves SQL to Dir.tmpdir/pry-psql.sql with adds basic formatting.
10
+ Then sends statemnet to `psql` or Rails determined system command.
11
+ Then send CSV data to column.
12
+ BANNER
13
+
14
+ def process
15
+ sql = sql_from_args
16
+ output.puts('Executing SQL:', sql)
17
+ { duration: exec_to_golumn(sql) }
18
+ end
19
+
20
+ # Parse the Pry args into a SQL statement.
21
+ # The Pry args maybe a Rails statement that generates SQL, so we try
22
+ # called `to_sql` first or `all.to_sql`, otherwise the whole evaled
23
+ # result is considered the SQL.
24
+ def sql_from_args
25
+ # Remove all the flags.
26
+ # Sample Regex: /\b?\\-[xth]+\s*/
27
+ argless = arg_string.gsub(%r{\b?-[#{opts.options.map(&:short).join}]+\s*}, '')
28
+ evaled = begin
29
+ target.eval(argless)
30
+ rescue Exception
31
+ # If there are any problems evaluating, consider the whole thing as raw SQL
32
+ argless
33
+ end
34
+ evaled.try(:to_sql) || evaled.try(:all).try(:to_sql) || evaled
35
+ end
36
+
37
+ def exec_to_golumn(sql)
38
+ conn = ActiveRecord::Base.connection.instance_variable_get(:@connection) || raise("@connection not found! This only works with PG connections or the PG adatper changed. Try using a different way to get the raw PG connection.")
39
+ query_time = Time.current
40
+ copy_cmd = 'COPY (%s) TO STDOUT CSV HEADER'
41
+ tmp_file = File.join(Dir.tmpdir, 'pry-golumn.csv')
42
+ conn.copy_data(copy_cmd % sql) do
43
+ File.open(tmp_file, 'wb') do |f|
44
+ while (row = conn.get_copy_data)
45
+ f << row
46
+ end
47
+ end
48
+ end
49
+ begin
50
+ table_name = sql.tr(%("'), '')[/from\s+(\w+)/i, 1]
51
+ rescue
52
+ table_name = '-'
53
+ output.puts "Could not determine table name. Defaulting to '-'"
54
+ end
55
+ table_name = table_name.blank? ? '-' : table_name
56
+ cmd = "golumn --title #{table_name} #{tmp_file} &"
57
+ output.puts "> #{cmd}"
58
+ system(cmd)
59
+ duration = Time.current - query_time
60
+ (duration * 1000).to_i
61
+ end
62
+ end
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "pry-golumn"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Scott Pierce"]
9
+ spec.email = ["ddrscott@gmail.com"]
10
+
11
+ spec.summary = %q{`golumn` command for Pry}
12
+ spec.homepage = "https://github.com/ddrscott/pry-golumn"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.16.a"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+
26
+ spec.add_dependency "pry"
27
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pry-golumn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Scott Pierce
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.16.a
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.16.a
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - ddrscott@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/pry-golumn.rb
84
+ - pry-golumn.gemspec
85
+ homepage: https://github.com/ddrscott/pry-golumn
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.6.13
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: "`golumn` command for Pry"
109
+ test_files: []