ar_pretty_sql 0.1.0

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
+ SHA256:
3
+ metadata.gz: bf0035acb3f3a16f52bab63c891c8677afaacb1bae30168cc907edf2afa35109
4
+ data.tar.gz: 579f0610275fab6de4b315b9939941dd49f586d4392abd9f90e0291b1c292d87
5
+ SHA512:
6
+ metadata.gz: 0f0afc932b6d9f33b8c4b53b4d7af089a2ca2d0cb5bd8b2ce5d6d04639295a1285dd75af9e01d4ae70b289e4e502c7cdee5cfec89293ad528fdf8d478a984b3d
7
+ data.tar.gz: 8ed3d244dfe85e23deba4adb3a5227dfa7e5941151efef9ef7c810e4ecb17119dcc746518320a1d5d7438e446074ef4d07a70c8d4e02cc9b32e21d7981090cf9
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/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ar_pretty_sql.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ar_pretty_sql (0.1.0)
5
+ anbt-sql-formatter (~> 0.1)
6
+ rouge (~> 3.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ anbt-sql-formatter (0.1.0)
12
+ diff-lcs (1.4.4)
13
+ rake (13.0.6)
14
+ rouge (3.28.0)
15
+ rspec (3.10.0)
16
+ rspec-core (~> 3.10.0)
17
+ rspec-expectations (~> 3.10.0)
18
+ rspec-mocks (~> 3.10.0)
19
+ rspec-core (3.10.1)
20
+ rspec-support (~> 3.10.0)
21
+ rspec-expectations (3.10.1)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.10.0)
24
+ rspec-mocks (3.10.2)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-support (3.10.2)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ ar_pretty_sql!
34
+ rake (~> 13.0)
35
+ rspec (~> 3.0)
36
+
37
+ BUNDLED WITH
38
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Joseph Johansen
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,93 @@
1
+ # ArPrettySql
2
+
3
+ Inspect the SQL behind your Active Record queries, in a human-friendly manner.
4
+
5
+ ```SQL
6
+ [1] pry(main)> User.where(first_name: "Geoff").order(id: :asc).limit(10).pp
7
+ SELECT
8
+ "users" . *
9
+ FROM
10
+ "users"
11
+ WHERE
12
+ "users"."first_name" = 'Geoff'
13
+ ORDER BY
14
+ "users"."id" ASC LIMIT 10
15
+ => nil
16
+ ```
17
+
18
+ ### Features
19
+
20
+ - Syntax highlighting
21
+ - Indentation and line breaks
22
+ - Customisable colour schemes
23
+ - Support for extended SQL function highlighting
24
+
25
+ ## Installation
26
+
27
+ Add this line to your application's Gemfile:
28
+
29
+ ```ruby
30
+ group :development, :test do
31
+ gem 'ar_pretty_sql'
32
+ end
33
+ ```
34
+
35
+ And then execute:
36
+
37
+ $ bundle install
38
+
39
+ Or install it yourself as:
40
+
41
+ $ gem install ar_pretty_sql
42
+
43
+ ## Usage
44
+
45
+ ```ruby
46
+ relation = User.where(age: 50)
47
+
48
+ relation.to_pretty_sql # generates a pretty string
49
+ relation.pp # same as `puts relation.to_pretty_sql`
50
+ ```
51
+
52
+ Both of the methods can accept one-off options:
53
+
54
+ ```ruby
55
+ relation.to_pretty_sql(color: false)
56
+ ```
57
+
58
+ Configuration can also be set globally:
59
+
60
+ ```ruby
61
+ # config/initializers/ar_pretty_sql.rb
62
+
63
+ ArPrettySql.configure do |config|
64
+ # enable syntax highlighting for the output (also accepts `colour`)
65
+ config.colour = true
66
+ # prettify the SQL output
67
+ config.format = true
68
+ # the colour theme to use for highlighting the SQL output (provided `color` is true)
69
+ config.theme = Rouge::Themes::Colorful.new
70
+ # extra SQL functions you want to provide highlighting for
71
+ config.addition_sql_functions += %w[jsonb_build_object]
72
+ # keyword case (:upper, :lower, :unchanged)
73
+ config.keyword_case = :upper
74
+ end
75
+ ```
76
+
77
+ - These same options can all be passed in as one-offs as keyword arguments.
78
+ - Colour themes are provided by [rouge](https://github.com/rouge-ruby/rouge) - see their docs for
79
+ more options.
80
+
81
+ ## Development
82
+
83
+ 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.
84
+
85
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
86
+
87
+ ## Contributing
88
+
89
+ Bug reports and pull requests are welcome on GitHub at https://github.com/johansenja/ar_pretty_sql.
90
+
91
+ ## License
92
+
93
+ 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,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ar_pretty_sql/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ar_pretty_sql"
7
+ spec.version = ArPrettySql::VERSION
8
+ spec.authors = ["Joseph Johansen"]
9
+ spec.email = ["joe@stotles.com"]
10
+
11
+ spec.summary = "Debug your active record queries fast"
12
+ spec.description = "This is a debugging tool to inspect your Active Record queries in a developer-friendly way"
13
+ spec.homepage = "https://github.com/johansenja/ar_pretty_sql"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ spec.add_dependency "rouge", "~> 3.2"
33
+ spec.add_dependency "anbt-sql-formatter", "~> 0.1"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ar_pretty_sql"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
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,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ArPrettySql
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ostruct"
4
+ require "rouge"
5
+ require "anbt-sql-formatter/formatter"
6
+
7
+ require_relative "ar_pretty_sql/version"
8
+
9
+ module ArPrettySql
10
+ Config = OpenStruct.new(
11
+ # the colour theme to use for highlighting the SQL output (provided `color` is true)
12
+ theme: Rouge::Themes::Colorful.new,
13
+ # enable syntax highlighting for the output (also accepts `colour`)
14
+ color: true,
15
+ # prettify the SQL output
16
+ format: true,
17
+ # extra SQL functions you want to provide highlighting for
18
+ additional_sql_functions: %w[count sum substr date],
19
+ # keyword case (:upper, :lower, :unchanged)
20
+ keyword_case: :upper,
21
+ )
22
+
23
+ def self.configure
24
+ yield Config
25
+ end
26
+
27
+ module InstanceMethods
28
+ def to_pretty_sql(**options)
29
+ conf = ArPrettySql::Config.to_h.merge! options
30
+
31
+ result = to_sql.dup
32
+
33
+ if conf[:format]
34
+ rule = AnbtSql::Rule.new
35
+
36
+ rule.keyword = case conf[:keyword_case].to_sym
37
+ when :upper then AnbtSql::Rule::KEYWORD_UPPER_CASE
38
+ when :lower then AnbtSql::Rule::KEYWORD_LOWER_CASE
39
+ when :unchanged then AnbtSql::Rule::KEYWORD_NONE
40
+ end
41
+
42
+ conf[:additional_sql_functions]&.each do |func_name|
43
+ rule.function_names << func_name.upcase
44
+ end
45
+
46
+ formatter = AnbtSql::Formatter.new(rule)
47
+ result = formatter.format(result)
48
+ end
49
+
50
+ if conf[:color] || conf[:colour]
51
+ lexer = Rouge::Lexers::SQL.new
52
+ formatter = Rouge::Formatters::Terminal256.new(conf[:theme])
53
+ result = formatter.format(lexer.lex(result))
54
+ end
55
+
56
+ result
57
+ end
58
+
59
+ def pp(**options)
60
+ puts to_pretty_sql(**options)
61
+ end
62
+ end
63
+ end
64
+
65
+ if Object.const_defined? "ActiveRecord::Relation"
66
+ ActiveRecord::Relation.prepend(ArPrettySql::InstanceMethods)
67
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_pretty_sql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Johansen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rouge
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: anbt-sql-formatter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ description: This is a debugging tool to inspect your Active Record queries in a developer-friendly
42
+ way
43
+ email:
44
+ - joe@stotles.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - ar_pretty_sql.gemspec
57
+ - bin/console
58
+ - bin/setup
59
+ - lib/ar_pretty_sql.rb
60
+ - lib/ar_pretty_sql/version.rb
61
+ homepage: https://github.com/johansenja/ar_pretty_sql
62
+ licenses:
63
+ - MIT
64
+ metadata:
65
+ allowed_push_host: https://rubygems.org/
66
+ homepage_uri: https://github.com/johansenja/ar_pretty_sql
67
+ source_code_uri: https://github.com/johansenja/ar_pretty_sql
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.4.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.1.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Debug your active record queries fast
87
+ test_files: []