Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ GEM
pp (0.6.2)
prettyprint
prettyprint (0.2.0)
prism (1.4.0)
prism (1.9.0)
psych (5.2.3)
date
stringio
Expand Down
4 changes: 3 additions & 1 deletion lib/taql/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ class Cli
attr_reader :options, :query

def initialize(argv)
@argv = argv
@options = {markdown: false}
@query = parse!
raise ArgumentError, "Usage: taql [--markdown] QUERY" if @query.empty?
end

def run
Expand All @@ -24,7 +26,7 @@ def environment_path
def parse!
OptionParser.new do |parser|
parser.on("-m", "--markdown", TrueClass, "Output table in Markdown")
end.parse!(into: options)
end.parse!(@argv, into: options)
end

def silence
Expand Down
10 changes: 9 additions & 1 deletion lib/taql/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ class Railtie < Rails::Railtie

initializer "taql.initialize" do
ActiveSupport.on_load(:active_record) do
Taql.instance_variable_set(:@default_connection, -> { ActiveRecord::Base.connection_pool.connection })
Taql.instance_variable_set(:@default_connection, method(:connection))
end
end

def self.connection
pool.respond_to?(:lease_connection) ? pool.lease_connection : pool.connection
end

def self.pool
ActiveRecord::Base.connection_pool
end
end
end
6 changes: 3 additions & 3 deletions lib/taql/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ def body
end

def columns
headers.map do |header|
@columns ||= headers.map do |header|
[header, *entries.map { |entry| entry[header] }]
end
end

def headers
entries.map(&:keys).uniq.flatten
@headers ||= entries.flat_map(&:keys).uniq
end

def print
Expand All @@ -43,7 +43,7 @@ def border
end

def column_widths
columns.map { |column| column.map(&:length).max }
@column_widths ||= columns.map { |column| column.map(&:length).max }
end

def edge
Expand Down
12 changes: 6 additions & 6 deletions test/test_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

class TestCli < Minitest::Test
def test_that_it_parses_queries
ARGV.replace(["SELECT * FROM users"])

cli = Taql::Cli.new(ARGV)
cli = Taql::Cli.new(["SELECT * FROM users"])

assert_equal "SELECT * FROM users", *cli.query
assert_equal false, cli.options[:markdown]
end

def test_that_it_parses_options
ARGV.replace(["--markdown", "SELECT * FROM users"])

cli = Taql::Cli.new(ARGV)
cli = Taql::Cli.new(["--markdown", "SELECT * FROM users"])

assert_equal "SELECT * FROM users", *cli.query
assert_equal true, cli.options[:markdown]
end

def test_that_it_raises_without_query
assert_raises(ArgumentError) { Taql::Cli.new([]) }
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

require "taql"
require "minitest/autorun"
require "minitest/mock"
9 changes: 9 additions & 0 deletions test/test_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,13 @@ def test_that_it_renders_nothing

assert_output("\n") { puts empty_table.print }
end

def test_headers_with_heterogeneous_entries
table = Taql::Table.new([
{"name" => "Alice", "age" => 30},
{"name" => "Bob", "email" => "bob@example.com"}
])

assert_equal ["name", "age", "email"], table.headers
end
end
Loading