Skip to content

Commit

Permalink
add rubocop and apply code style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sufleR committed Mar 30, 2017
1 parent fee7cc2 commit efded26
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ gemfile:
- gemfiles/5.0.gemfile

rvm:
- '2.3.1'
- '2.3.3'

addons:
postgresql: '9.4'
Expand Down
22 changes: 12 additions & 10 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
appraise "3.2" do
gem "activerecord", "~> 3.2.21"
# frozen_string_literal: true

appraise '3.2' do
gem 'activerecord', '~> 3.2.21'
end

appraise "4.0" do
gem "activerecord", "~> 4.0.0"
appraise '4.0' do
gem 'activerecord', '~> 4.0.0'
end

appraise "4.1" do
gem "activerecord", "~> 4.1.0"
appraise '4.1' do
gem 'activerecord', '~> 4.1.0'
end

appraise "4.2" do
gem "activerecord", "~> 4.2.0"
appraise '4.2' do
gem 'activerecord', '~> 4.2.0'
end

appraise "5.0" do
gem "activerecord", "~> 5.0.0"
appraise '5.0' do
gem 'activerecord', '~> 5.0.0'
end
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in sql_query.gemspec
Expand Down
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "bundler/gem_tasks"
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

task :default => :spec
task default: :spec

RSpec::Core::RakeTask.new
29 changes: 15 additions & 14 deletions lib/sql_query.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# frozen_string_literal: true

require 'erb'

class SqlQuery
attr_reader :connection

def initialize(file_name, options = {})
unless file_name.is_a?(String) || file_name.is_a?(Symbol)
fail ArgumentError, 'SQL file name should be String or Symbol'
raise ArgumentError, 'SQL file name should be String or Symbol'
end
@sql_filename = file_name
@options = options
@connection = options.try(:delete, :db_connection) || self.class.config.adapter.connection
@connection = options.try(:delete, :db_connection) ||
self.class.config.adapter.connection
prepare_variables
end

def explain
msg = "EXPLAIN for: \n#{ sql }\n"
msg = "EXPLAIN for: \n#{sql}\n"
msg += connection.explain(sql)
pretty(msg)
end
Expand All @@ -32,22 +35,18 @@ def pretty_sql
pretty(sql.dup)
end

def quote(value)
connection.quote(value)
end
delegate :quote, to: :connection

def prepared_for_logs
sql.gsub(/(\n|\s)+/,' ')
sql.gsub(/(\n|\s)+/, ' ')
end

def partial(partial_name, partial_options = {})
path, file_name = split_to_path_and_name(partial_name)
self.class.new("#{path}/_#{file_name}",
@options.merge(partial_options),
).sql
@options.merge(partial_options)).sql
end


attr_writer :config
def self.config
@config ||= Config.new
Expand Down Expand Up @@ -81,23 +80,25 @@ def pretty(value)
# override inspect to be more human readable from console
# code copy from ActiveRecord
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/explain.rb#L30
def value.inspect; self; end
def value.inspect
self
end
value
end

def prepare_variables
return unless @options.present?
return if @options.blank?
@options.each do |k, v|
instance_variable_set("@#{k}", v)
end
end

def file_path
files = Dir.glob(path)
if files.size == 0
if files.empty?
raise "File not found: #{@sql_filename}"
elsif files.size > 1
raise "More than one file found: #{ files.join(', ')}"
raise "More than one file found: #{files.join(', ')}"
else
files.first
end
Expand Down
2 changes: 2 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe SqlQuery::Config do
Expand Down
10 changes: 6 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

if ENV['BUILDER'] == 'travis'
require "codeclimate-test-reporter"
require 'codeclimate-test-reporter'
CodeClimate::TestReporter.start
else
require 'simplecov'
Expand All @@ -26,15 +28,15 @@
end

connection = if ENV['BUILDER'] == 'travis'
"postgres://postgres@localhost/travis_ci_test"
'postgres://postgres@localhost/travis_ci_test'
else
"postgres://sqlquery:sqlquery@localhost/sqlquery"
'postgres://sqlquery:sqlquery@localhost/sqlquery'
end

ActiveRecord::Base.establish_connection(connection)

ActiveRecord::Base.connection.execute(
"CREATE TABLE IF NOT EXISTS players (email text);"
'CREATE TABLE IF NOT EXISTS players (email text);'
)

config.order = :random
Expand Down
31 changes: 19 additions & 12 deletions spec/sql_query_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

require 'spec_helper'

describe SqlQuery do

let(:options) { { email: '[email protected]' } }
let(:file_name) { :get_player_by_email }
let(:query) { described_class.new(file_name, options) }
Expand All @@ -11,7 +12,6 @@ class Model < ActiveRecord::Base
end

describe '#initialize' do

it 'sets instance variables for all options' do
expect(query.instance_variable_get(:@email)).to eq '[email protected]'
end
Expand All @@ -28,7 +28,9 @@ class Model < ActiveRecord::Base
let(:file_name) { { do: 'something' } }

it 'raises ArgumentError' do
expect{ query }.to raise_error(ArgumentError, 'SQL file name should be String or Symbol')
expect { query }
.to raise_error(ArgumentError,
'SQL file name should be String or Symbol')
end
end

Expand All @@ -51,15 +53,17 @@ class Model < ActiveRecord::Base
let(:file_name) { :not_exists }

it 'raises error' do
expect { query.send(:file_path) }.to raise_error('File not found: not_exists')
expect { query.send(:file_path) }
.to raise_error('File not found: not_exists')
end
end

context 'when there are more than one matching files' do
let(:file_name) { :duplicated }

it 'raises error' do
expect{ query.send(:file_path) }.to raise_error.with_message(/More than one file found:/)
expect { query.send(:file_path) }
.to raise_error.with_message(/More than one file found:/)
end
end
end
Expand All @@ -70,14 +74,15 @@ class Model < ActiveRecord::Base
let(:file_name) { 'fname' }

it 'sets it as dir for file' do
expect(query.send(:path)).to eq("/path_to_file/fname.{sql.erb,erb.sql}")
expect(query.send(:path)).to eq('/path_to_file/fname.{sql.erb,erb.sql}')
end
end
end

describe '#sql' do
it 'returns query string' do
expect(query.sql).to eq "SELECT *\nFROM players\nWHERE email = '[email protected]'\n"
expect(query.sql)
.to eq("SELECT *\nFROM players\nWHERE email = '[email protected]'\n")
end

context 'when file is .erb.sql' do
Expand All @@ -92,15 +97,16 @@ class Model < ActiveRecord::Base

describe '#pretty_sql' do
it 'returns query string' do
expect(query.pretty_sql).to eq "SELECT *\nFROM players\nWHERE email = '[email protected]'\n"
expect(query.pretty_sql)
.to eq("SELECT *\nFROM players\nWHERE email = '[email protected]'\n")
end
end

describe '#explain' do
let(:explain) { query.explain }
it 'returns explain string' do
expect(explain).to include 'EXPLAIN for:'
expect(explain).to include "FROM players"
expect(explain).to include 'FROM players'
expect(explain).to include "WHERE email = '[email protected]'"
expect(explain).to include 'QUERY PLAN'
expect(explain).to include 'Seq Scan on players'
Expand All @@ -116,12 +122,12 @@ class Model < ActiveRecord::Base

after do
ActiveRecord::Base.connection.execute(
"DELETE FROM players"
'DELETE FROM players'
)
end

it 'returns data from database' do
expect(query.execute).to eq [{ 'email' => '[email protected]'}]
expect(query.execute).to eq [{ 'email' => '[email protected]' }]
end

context 'when prepare argument is true' do
Expand All @@ -142,7 +148,8 @@ class Model < ActiveRecord::Base
describe '#partial' do
let(:file_name) { :get_player_by_email_with_template }
it 'resolves partials as parts of sql queries' do
expect(query.sql).to eq("SELECT *\nFROM players\nWHERE players.email = '[email protected]'\n\n")
expect(query.sql)
.to eq("SELECT *\nFROM players\nWHERE players.email = '[email protected]'\n\n")
end

context 'when partial name is string with file path' do
Expand Down
43 changes: 23 additions & 20 deletions sql_query.gemspec
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
# coding: utf-8
# frozen_string_literal: true

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = "sql_query"
spec.version = "0.6.0"
spec.authors = ["sufleR"]
spec.email = ["[email protected]"]
spec.summary = %q{Ruby gem to load and execute SQL queries from `.sql.erb` templates}
spec.description = %q{
spec.name = 'sql_query'
spec.version = '0.6.0'
spec.authors = ['sufleR']
spec.email = ['[email protected]']
spec.summary = 'Ruby gem to load and execute SQL queries from `.sql.erb` templates'
spec.description = "
It makes working with pure SQL easier with syntax highlighting.
Let's you clean your Ruby code from SQL strings.
}
spec.homepage = "https://github.com/sufleR/sql_query"
spec.license = "MIT"
"
spec.homepage = 'https://github.com/sufleR/sql_query'
spec.license = 'MIT'

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.required_ruby_version = ">= 1.9.3"
spec.required_ruby_version = '>= 1.9.3'

spec.add_dependency "activerecord", ">= 3.2"
spec.add_development_dependency "bundler", "~> 1.13"
spec.add_development_dependency "rake", "~> 11.3"
spec.add_development_dependency "rspec", "~> 3.4"
spec.add_development_dependency "pg", "~> 0.18"
spec.add_development_dependency "pry", "~> 0.10"
spec.add_development_dependency "with_model", "~> 1.2"
spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4"
spec.add_development_dependency "appraisal"
spec.add_dependency 'activerecord', '>= 3.2'
spec.add_development_dependency 'bundler', '~> 1.13'
spec.add_development_dependency 'rake', '~> 11.3'
spec.add_development_dependency 'rspec', '~> 3.4'
spec.add_development_dependency 'pg', '~> 0.18'
spec.add_development_dependency 'pry', '~> 0.10'
spec.add_development_dependency 'with_model', '~> 1.2'
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
spec.add_development_dependency 'appraisal'
spec.add_development_dependency 'rubocop'
end

0 comments on commit efded26

Please sign in to comment.