lib/arql/app.rb in arql-0.1.15 vs lib/arql/app.rb in arql-0.1.16

- old
+ new

@@ -2,10 +2,12 @@ module Arql class App class << self + attr_accessor :log_io + def config @@effective_config end def local_ssh_proxy_port @@ -19,23 +21,33 @@ require "arql/connection" require "arql/definition" @options = options Connection.open(connect_options) @definition = Definition.new(effective_config) + load_initializer! end def connect_options connect_conf = effective_config.slice(:adapter, :host, :username, :password, :database, :encoding, - :pool, :port) + :pool, :port, :socket) if effective_config[:ssh].present? connect_conf.merge!(start_ssh_proxy!) end connect_conf end + def load_initializer! + return unless effective_config[:initializer] + unless File.exists?(effective_config[:initializer]) + STDERR.puts "Specified initializer file not found, #{effective_config[:initializer]}" + exit(1) + end + load(effective_config[:initializer]) + end + def start_ssh_proxy! ssh_config = effective_config[:ssh] @ssh_gateway = Net::SSH::Gateway.new(ssh_config[:host], ssh_config[:user], ssh_config.slice(:port, :password).symbolize_keys) @@local_ssh_proxy_port = @ssh_gateway.open(effective_config[:host], effective_config[:port], ssh_config[:local_port]) { @@ -47,10 +59,13 @@ def config @config ||= YAML.load(IO.read(@options.config_file)).with_indifferent_access end def selected_config + if @options.env.present? && !config[@options.env].present? + STDERR.puts "Specified ENV `#{@options.env}' not exists" + end config[@options.env] end def effective_config @@effective_config ||= selected_config.deep_merge(@options.to_h) @@ -86,25 +101,25 @@ def should_append_sql? effective_config[:append_sql] end def show_sql - @log_io ||= MultiIO.new - ActiveRecord::Base.logger = Logger.new(@log_io) - @log_io << STDOUT + App.log_io ||= MultiIO.new + ActiveRecord::Base.logger = Logger.new(App.log_io) + App.log_io << STDOUT end def write_sql write_sql_file = effective_config[:write_sql] - @log_io ||= MultiIO.new - ActiveRecord::Base.logger = Logger.new(@log_io) - @log_io << File.new(write_sql_file, 'w') + App.log_io ||= MultiIO.new + ActiveRecord::Base.logger = Logger.new(App.log_io) + App.log_io << File.new(write_sql_file, 'w') end def append_sql write_sql_file = effective_config[:append_sql] - @log_io ||= MultiIO.new - ActiveRecord::Base.logger = Logger.new(@log_io) - @log_io << File.new(write_sql_file, 'a') + App.log_io ||= MultiIO.new + ActiveRecord::Base.logger = Logger.new(App.log_io) + App.log_io << File.new(write_sql_file, 'a') end end end