Class: Arql::App
Class Attribute Summary collapse
-
.env ⇒ Object
Returns the value of attribute env.
-
.instance ⇒ Object
Returns the value of attribute instance.
-
.log_io ⇒ Object
Returns the value of attribute log_io.
-
.prompt ⇒ Object
Returns the value of attribute prompt.
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.
- #load_initializer! ⇒ Object
- #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.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/arql/app.rb', line 22 def initialize() require 'active_support/all' require 'active_record' require "arql/connection" require "arql/definition" @options = App.env = @options.env Connection.open() @definition = Definition.new(effective_config) load_initializer! App.instance = self end |
Class Attribute Details
.env ⇒ Object
Returns the value of attribute env.
7 8 9 |
# File 'lib/arql/app.rb', line 7 def env @env end |
.instance ⇒ Object
Returns the value of attribute instance.
7 8 9 |
# File 'lib/arql/app.rb', line 7 def instance @instance end |
.log_io ⇒ Object
Returns the value of attribute log_io.
7 8 9 |
# File 'lib/arql/app.rb', line 7 def log_io @log_io end |
.prompt ⇒ Object
Returns the value of attribute prompt.
7 8 9 |
# File 'lib/arql/app.rb', line 7 def prompt @prompt end |
Class Method Details
.config ⇒ Object
9 10 11 |
# File 'lib/arql/app.rb', line 9 def config @@effective_config end |
Instance Method Details
#append_sql ⇒ Object
139 140 141 142 143 144 |
# File 'lib/arql/app.rb', line 139 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 ⇒ Object
68 69 70 |
# File 'lib/arql/app.rb', line 68 def config @config ||= YAML.load(IO.read(File.(@options.config_file))).with_indifferent_access end |
#connect_options ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/arql/app.rb', line 35 def 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 ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/arql/app.rb', line 83 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.(@@effective_config[:database]) if @@effective_config[:adapter] == 'sqlite3' end @@effective_config end |
#load_initializer! ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/arql/app.rb', line 46 def load_initializer! return unless effective_config[:initializer] initializer_file = File.(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! ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/arql/app.rb', line 95 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
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/arql/app.rb', line 72 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? ⇒ Boolean
122 123 124 |
# File 'lib/arql/app.rb', line 122 def should_append_sql? effective_config[:append_sql] end |
#should_show_sql? ⇒ Boolean
114 115 116 |
# File 'lib/arql/app.rb', line 114 def should_show_sql? effective_config[:show_sql] end |
#should_write_sql? ⇒ Boolean
118 119 120 |
# File 'lib/arql/app.rb', line 118 def should_write_sql? effective_config[:write_sql] end |
#show_sql ⇒ Object
126 127 128 129 130 |
# File 'lib/arql/app.rb', line 126 def show_sql App.log_io ||= MultiIO.new ActiveRecord::Base.logger = Logger.new(App.log_io) App.log_io << STDOUT end |
#start_ssh_proxy! ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/arql/app.rb', line 56 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 |