Class: Arql::App
Class Attribute Summary collapse
-
.connect_options ⇒ Object
Returns the value of attribute connect_options.
-
.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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/arql/app.rb', line 20 def initialize() require "arql/connection" require "arql/definition" @options = App.env = @options.env App. = Connection.open(App.) print "Defining models..." @definition = Definition.new(effective_config) print "\u001b[2K" puts "\rModels defined" print "Running initializers..." load_initializer! print "\u001b[2K" puts "\rInitializers loaded" App.instance = self end |
Class Attribute Details
.connect_options ⇒ Object
Returns the value of attribute connect_options.
5 6 7 |
# File 'lib/arql/app.rb', line 5 def @connect_options end |
.env ⇒ Object
Returns the value of attribute env.
5 6 7 |
# File 'lib/arql/app.rb', line 5 def env @env end |
.instance ⇒ Object
Returns the value of attribute instance.
5 6 7 |
# File 'lib/arql/app.rb', line 5 def instance @instance end |
.log_io ⇒ Object
Returns the value of attribute log_io.
5 6 7 |
# File 'lib/arql/app.rb', line 5 def log_io @log_io end |
.prompt ⇒ Object
Returns the value of attribute prompt.
5 6 7 |
# File 'lib/arql/app.rb', line 5 def prompt @prompt end |
Class Method Details
.config ⇒ Object
7 8 9 |
# File 'lib/arql/app.rb', line 7 def config @@effective_config end |
Instance Method Details
#append_sql ⇒ Object
144 145 146 147 148 149 |
# File 'lib/arql/app.rb', line 144 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
71 72 73 74 75 |
# File 'lib/arql/app.rb', line 71 def config @config ||= YAML.load(IO.read(File.(@options.config_file)), aliases: true).with_indifferent_access rescue ArgumentError @config ||= YAML.load(IO.read(File.(@options.config_file))).with_indifferent_access end |
#connect_options ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/arql/app.rb', line 38 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
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/arql/app.rb', line 88 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
49 50 51 52 53 54 55 56 57 |
# File 'lib/arql/app.rb', line 49 def load_initializer! return unless effective_config[:initializer] initializer_file = File.(effective_config[:initializer]) unless File.exist?(initializer_file) STDERR.puts "Specified initializer file not found, #{effective_config[:initializer]}" exit(1) end load(initializer_file) end |
#run! ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/arql/app.rb', line 100 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].first.tap { |file| load(file) } elsif STDIN.isatty run_repl! else eval(STDIN.read) end end |
#selected_config ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/arql/app.rb', line 77 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
127 128 129 |
# File 'lib/arql/app.rb', line 127 def should_append_sql? effective_config[:append_sql] end |
#should_show_sql? ⇒ Boolean
119 120 121 |
# File 'lib/arql/app.rb', line 119 def should_show_sql? effective_config[:show_sql] end |
#should_write_sql? ⇒ Boolean
123 124 125 |
# File 'lib/arql/app.rb', line 123 def should_write_sql? effective_config[:write_sql] end |
#show_sql ⇒ Object
131 132 133 134 135 |
# File 'lib/arql/app.rb', line 131 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
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/arql/app.rb', line 59 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 |