class Arql::App

Attributes

connect_options[RW]
env[RW]
instance[RW]
log_io[RW]
prompt[RW]

Public Class Methods

config() click to toggle source
# File lib/arql/app.rb, line 9
def config
  @@effective_config
end
new(options) click to toggle source
# File lib/arql/app.rb, line 22
def initialize(options)
  require 'active_support/all'
  require 'active_record'
  require 'composite_primary_keys'
  require "arql/connection"
  require "arql/definition"
  @options = options
  App.env = @options.env
  App.connect_options = connect_options
  Connection.open(App.connect_options)
  @definition = Definition.new(effective_config)
  load_initializer!
  App.instance = self
end

Public Instance Methods

append_sql() click to toggle source
# File lib/arql/app.rb, line 141
def append_sql
  write_sql_file = effective_config[:append_sql]
  App.log_io ||= MultiIO.new
  ActiveRecord::Base.logger = Logger.new(App.log_io)
  App.log_io << File.new(write_sql_file, 'a')
end
config() click to toggle source
# File lib/arql/app.rb, line 70
def config
  @config ||= YAML.load(IO.read(File.expand_path(@options.config_file))).with_indifferent_access
end
connect_options() click to toggle source
# File lib/arql/app.rb, line 37
def connect_options
  connect_conf = effective_config.slice(:adapter, :host, :username,
                         :password, :database, :encoding,
                         :pool, :port, :socket)
  if effective_config[:ssh].present?
    connect_conf.merge!(start_ssh_proxy!)
  end

  connect_conf
end
effective_config() click to toggle source
# File lib/arql/app.rb, line 85
def effective_config
  @@effective_config ||= nil
  unless @@effective_config
    @@effective_config = selected_config.deep_merge(@options.to_h)
    if @@effective_config[:adapter].blank?
      @@effective_config[:adapter] = 'sqlite3'
    end
    @@effective_config[:database] = File.expand_path(@@effective_config[:database]) if @@effective_config[:adapter] == 'sqlite3'
  end
  @@effective_config
end
load_initializer!() click to toggle source
# File lib/arql/app.rb, line 48
def load_initializer!
  return unless effective_config[:initializer]
  initializer_file = File.expand_path(effective_config[:initializer])
  unless File.exists?(initializer_file)
    STDERR.puts "Specified initializer file not found, #{effective_config[:initializer]}"
    exit(1)
  end
  load(initializer_file)
end
run!() click to toggle source
# File lib/arql/app.rb, line 97
def run!
  show_sql if should_show_sql?
  write_sql if should_write_sql?
  append_sql if should_append_sql?
  if effective_config[:code].present?
    eval(effective_config[:code])
  elsif effective_config[:args].present?
    effective_config[:args].each { |rb| load(rb) }
  elsif STDIN.isatty
    run_repl!
  else
    eval(STDIN.read)
  end
end
run_repl!() click to toggle source
# File lib/arql/app.rb, line 112
def run_repl!
  Repl.new
end
selected_config() click to toggle source
# File lib/arql/app.rb, line 74
def selected_config
  if @options.env.present? && !config[@options.env].present?
    STDERR.puts "Specified ENV `#{@options.env}' not exists"
  end
  if env = @options.env
    config[env]
  else
    {}
  end
end
should_append_sql?() click to toggle source
# File lib/arql/app.rb, line 124
def should_append_sql?
  effective_config[:append_sql]
end
should_show_sql?() click to toggle source
# File lib/arql/app.rb, line 116
def should_show_sql?
  effective_config[:show_sql]
end
should_write_sql?() click to toggle source
# File lib/arql/app.rb, line 120
def should_write_sql?
  effective_config[:write_sql]
end
show_sql() click to toggle source
# File lib/arql/app.rb, line 128
def show_sql
  App.log_io ||= MultiIO.new
  ActiveRecord::Base.logger = Logger.new(App.log_io)
  App.log_io << STDOUT
end
start_ssh_proxy!() click to toggle source
# File lib/arql/app.rb, line 58
def start_ssh_proxy!
  ssh_config = effective_config[:ssh]
  local_ssh_proxy_port = Arql::SSHProxy.connect(ssh_config.slice(:host, :user, :port, :password).merge(
                                                                                                       forward_host: effective_config[:host],
                                                                                                       forward_port: effective_config[:port],
                                                                                                       local_port: ssh_config[:local_port]))
  {
    host: '127.0.0.1',
    port: local_ssh_proxy_port
  }
end
write_sql() click to toggle source
# File lib/arql/app.rb, line 134
def write_sql
  write_sql_file = effective_config[:write_sql]
  App.log_io ||= MultiIO.new
  ActiveRecord::Base.logger = Logger.new(App.log_io)
  App.log_io << File.new(write_sql_file, 'w')
end