diff --git a/.travis.yml b/.travis.yml index ff713f4..d5aa731 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ gemfile: - gemfiles/5.0.gemfile rvm: - - '2.3.1' + - '2.3.3' addons: postgresql: '9.4' diff --git a/Appraisals b/Appraisals index fa38d84..b0f396e 100644 --- a/Appraisals +++ b/Appraisals @@ -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 diff --git a/Gemfile b/Gemfile index fab945d..ab412d0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'https://rubygems.org' # Specify your gem's dependencies in sql_query.gemspec diff --git a/Rakefile b/Rakefile index 45a9ca3..97edc26 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/lib/sql_query.rb b/lib/sql_query.rb index 5ba4a41..b374c92 100644 --- a/lib/sql_query.rb +++ b/lib/sql_query.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'erb' class SqlQuery @@ -5,16 +7,17 @@ class SqlQuery 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 @@ -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 @@ -81,12 +80,14 @@ 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 @@ -94,10 +95,10 @@ def prepare_variables 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 diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 4b6eb1c..75a088b 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe SqlQuery::Config do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b19fef1..814b801 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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' @@ -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 diff --git a/spec/sql_query_spec.rb b/spec/sql_query_spec.rb index 7250209..8af50a9 100644 --- a/spec/sql_query_spec.rb +++ b/spec/sql_query_spec.rb @@ -1,7 +1,8 @@ +# frozen_string_literal: true + require 'spec_helper' describe SqlQuery do - let(:options) { { email: 'e@mail.dev' } } let(:file_name) { :get_player_by_email } let(:query) { described_class.new(file_name, options) } @@ -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 'e@mail.dev' end @@ -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 @@ -51,7 +53,8 @@ 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 @@ -59,7 +62,8 @@ class Model < ActiveRecord::Base 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 @@ -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 = 'e@mail.dev'\n" + expect(query.sql) + .to eq("SELECT *\nFROM players\nWHERE email = 'e@mail.dev'\n") end context 'when file is .erb.sql' do @@ -92,7 +97,8 @@ class Model < ActiveRecord::Base describe '#pretty_sql' do it 'returns query string' do - expect(query.pretty_sql).to eq "SELECT *\nFROM players\nWHERE email = 'e@mail.dev'\n" + expect(query.pretty_sql) + .to eq("SELECT *\nFROM players\nWHERE email = 'e@mail.dev'\n") end end @@ -100,7 +106,7 @@ class Model < ActiveRecord::Base 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 = 'e@mail.dev'" expect(explain).to include 'QUERY PLAN' expect(explain).to include 'Seq Scan on players' @@ -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' => 'e@mail.dev'}] + expect(query.execute).to eq [{ 'email' => 'e@mail.dev' }] end context 'when prepare argument is true' do @@ -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 = 'e@mail.dev'\n\n") + expect(query.sql) + .to eq("SELECT *\nFROM players\nWHERE players.email = 'e@mail.dev'\n\n") end context 'when partial name is string with file path' do diff --git a/sql_query.gemspec b/sql_query.gemspec index 4fe3ed4..18fca9e 100644 --- a/sql_query.gemspec +++ b/sql_query.gemspec @@ -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 = ["szymon.fracczak@gmail.com"] - 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 = ['szymon.fracczak@gmail.com'] + 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