Class: Arql::App

Inherits:
Object show all
Defined in:
lib/arql/app.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ App

Returns a new instance of App.



16
17
18
19
20
21
22
23
24
# File 'lib/arql/app.rb', line 16

def initialize(options)
  require 'active_support/all'
  require 'active_record'
  require "arql/connection"
  require "arql/definition"
  @options = options
  Connection.open(connect_options)
  @definition = Definition.new(effective_config)
end

Class Method Details

.configObject



7
8
9
# File 'lib/arql/app.rb', line 7

def config
  @@effective_config
end

.local_ssh_proxy_portObject



11
12
13
# File 'lib/arql/app.rb', line 11

def local_ssh_proxy_port
  @@local_ssh_proxy_port
end

Instance Method Details

#append_sqlObject



103
104
105
106
107
108
# File 'lib/arql/app.rb', line 103

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')
end

#configObject



47
48
49
# File 'lib/arql/app.rb', line 47

def config
  @config ||= YAML.load(IO.read(@options.config_file)).with_indifferent_access
end

#connect_optionsObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/arql/app.rb', line 26

def connect_options
  connect_conf = effective_config.slice(:adapter, :host, :username,
                         :password, :database, :encoding,
                         :pool, :port)
  if effective_config[:ssh].present?
    connect_conf.merge!(start_ssh_proxy!)
  end

  connect_conf
end

#effective_configObject



55
56
57
# File 'lib/arql/app.rb', line 55

def effective_config
  @@effective_config ||= selected_config.deep_merge(@options.to_h)
end

#run!Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/arql/app.rb', line 59

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!Object



74
75
76
# File 'lib/arql/app.rb', line 74

def run_repl!
  Repl.new
end

#selected_configObject



51
52
53
# File 'lib/arql/app.rb', line 51

def selected_config
  config[@options.env]
end

#should_append_sql?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/arql/app.rb', line 86

def should_append_sql?
  effective_config[:append_sql]
end

#should_show_sql?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/arql/app.rb', line 78

def should_show_sql?
  effective_config[:show_sql]
end

#should_write_sql?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/arql/app.rb', line 82

def should_write_sql?
  effective_config[:write_sql]
end

#show_sqlObject



90
91
92
93
94
# File 'lib/arql/app.rb', line 90

def show_sql
  @log_io ||= MultiIO.new
  ActiveRecord::Base.logger = Logger.new(@log_io)
  @log_io << STDOUT
end

#start_ssh_proxy!Object



37
38
39
40
41
42
43
44
45
# File 'lib/arql/app.rb', line 37

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])
  {
    host: '127.0.0.1',
    port: @@local_ssh_proxy_port
  }
end

#write_sqlObject



96
97
98
99
100
101
# File 'lib/arql/app.rb', line 96

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')
end