Class: Prize::App

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

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ App

Returns a new instance of App.



11
12
13
14
15
16
17
18
19
# File 'lib/prize/app.rb', line 11

def initialize(options)
  require 'active_support/all'
  @options = options
  load_initializer!
  @redis = Redis.new(connect_options)
  App.options = @options
  App.redis = @redis
  App.redis_client = @redis.instance_variable_get('@client')
end

Class Attribute Details

.optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/prize/app.rb', line 8

def options
  @options
end

.redisObject

Returns the value of attribute redis.



8
9
10
# File 'lib/prize/app.rb', line 8

def redis
  @redis
end

.redis_clientObject

Returns the value of attribute redis_client.



8
9
10
# File 'lib/prize/app.rb', line 8

def redis_client
  @redis_client
end

Instance Method Details

#connect_optionsObject



21
22
23
24
25
26
27
28
29
# File 'lib/prize/app.rb', line 21

def connect_options
  opts = @options.to_h.slice(:url, :host, :port, :path, :db, :password, :timeout,
                               :connect_timeout, :replica, :cluster)
  if @options.ssh_host
    proxy = start_ssh_proxy!
    opts.merge!(proxy)
  end
  opts
end

#load_initializer!Object



31
32
33
34
35
# File 'lib/prize/app.rb', line 31

def load_initializer!
  if File.exist?(File.expand_path('~/.prizerc'))
    load(File.expand_path('~/.prizerc'))
  end
end

#run!Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/prize/app.rb', line 54

def run!
  if @options.code.present?
    @redis.instance_eval(@options.code)
  elsif @options.args.present?
    @options.args.each do |rb|
      @redis.instance_eval(IO.read(rb))
    end
  elsif STDIN.isatty
    run_repl!
  else
    @redis.instance_eval(STDIN.read)
  end
end

#run_repl!Object



68
69
70
# File 'lib/prize/app.rb', line 68

def run_repl!
  Repl.new
end

#start_ssh_proxy!Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/prize/app.rb', line 37

def start_ssh_proxy!
  ssh_config = {
    host: @options.ssh_host,
    forward_host: @options.host || '127.0.0.1',
    forward_port: @options.port || 6379
  }
  @options.ssh_user.try { |e| ssh_config.merge!(user: e) }
  @options.ssh_port.try { |e| ssh_config.merge!(port: e) }
  @options.ssh_password.try { |e| ssh_config.merge!(password: e) }
  @options.ssh_local_port.try { |e| ssh_config.merge!(local_port: e) }
  local_ssh_proxy_port = Prize::SSHProxy.connect(ssh_config)
  {
    host: '127.0.0.1',
    port: local_ssh_proxy_port
  }
end