Class: Arql::App
Class Method Summary collapse
Instance Method Summary collapse
- #append_sql ⇒ Object
- #config ⇒ Object
- #connect_options ⇒ Object
- #effective_config ⇒ Object
-
#initialize(options) ⇒ App
constructor
A new instance of App.
- #run! ⇒ Object
- #run_repl! ⇒ Object
- #selected_config ⇒ Object
- #should_append_sql? ⇒ Boolean
- #should_show_sql? ⇒ Boolean
- #should_write_sql? ⇒ Boolean
- #show_sql ⇒ Object
- #start_ssh_proxy! ⇒ Object
- #write_sql ⇒ Object
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() require 'active_support/all' require 'active_record' require "arql/connection" require "arql/definition" @options = Connection.open() @definition = Definition.new(effective_config) end |
Class Method Details
.config ⇒ Object
7 8 9 |
# File 'lib/arql/app.rb', line 7 def config @@effective_config end |
.local_ssh_proxy_port ⇒ Object
11 12 13 |
# File 'lib/arql/app.rb', line 11 def local_ssh_proxy_port @@local_ssh_proxy_port end |
Instance Method Details
#append_sql ⇒ Object
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 |
#config ⇒ Object
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_options ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/arql/app.rb', line 26 def 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_config ⇒ Object
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 |
#selected_config ⇒ Object
51 52 53 |
# File 'lib/arql/app.rb', line 51 def selected_config config[@options.env] end |
#should_append_sql? ⇒ Boolean
86 87 88 |
# File 'lib/arql/app.rb', line 86 def should_append_sql? effective_config[:append_sql] end |
#should_show_sql? ⇒ Boolean
78 79 80 |
# File 'lib/arql/app.rb', line 78 def should_show_sql? effective_config[:show_sql] end |
#should_write_sql? ⇒ Boolean
82 83 84 |
# File 'lib/arql/app.rb', line 82 def should_write_sql? effective_config[:write_sql] end |
#show_sql ⇒ Object
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 |