Class: PryRemote::Server

Inherits:
Object show all
Defined in:
lib/pry-remote.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, host = DefaultHost, port = DefaultPort, options = {}) ⇒ Server

Returns a new instance of Server.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pry-remote.rb', line 141

def initialize(object, host = DefaultHost, port = DefaultPort, options = {})
  @host    = host
  @port    = port

  @object  = object
  @options = options

  @client = PryRemote::Client.new
  DRb.start_service uri, @client

  puts "[pry-remote] Waiting for client on #{uri}"
  @client.wait

  puts "[pry-remote] Client received, starting remote session"
end

Instance Attribute Details

#clientPryServer::Client (readonly)

Returns Client connecting to the pry-remote server.

Returns:

  • (PryServer::Client)

    Client connecting to the pry-remote server



218
219
220
# File 'lib/pry-remote.rb', line 218

def client
  @client
end

#hostString (readonly)

Returns Host of the server.

Returns:

  • (String)

    Host of the server



221
222
223
# File 'lib/pry-remote.rb', line 221

def host
  @host
end

#objectObject (readonly)

Returns Object to enter into.

Returns:

  • Object to enter into



215
216
217
# File 'lib/pry-remote.rb', line 215

def object
  @object
end

#portInteger (readonly)

Returns Port of the server.

Returns:

  • (Integer)

    Port of the server



224
225
226
# File 'lib/pry-remote.rb', line 224

def port
  @port
end

Class Method Details

.run(object, host = DefaultHost, port = DefaultPort, options = {}) ⇒ Object



137
138
139
# File 'lib/pry-remote.rb', line 137

def self.run(object, host = DefaultHost, port = DefaultPort, options = {})
  new(object, host, port, options).run
end

Instance Method Details

#runObject

Actually runs pry-remote



206
207
208
209
210
211
212
# File 'lib/pry-remote.rb', line 206

def run
  setup

  Pry.start(@object, @options.merge(:input => client.input_proxy, :output => client.output))
ensure
  teardown
end

#setupObject

Code that has to be called for Pry-remote to work properly



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/pry-remote.rb', line 158

def setup
  # If client passed stdout and stderr, redirect actual messages there.
  @old_stdout, $stdout = if @client.stdout
                           [$stdout, @client.stdout]
                         else
                           [$stdout, $stdout]
                         end

  @old_stderr, $stderr = if @client.stderr
                           [$stderr, @client.stderr]
                         else
                           [$stderr, $stderr]
                         end

  # Before Pry starts, save the pager config.
  # We want to disable this because the pager won't do anything useful in
  # this case (it will run on the server).
  Pry.config.pager, @old_pager = false, Pry.config.pager

  # As above, but for system config
  Pry.config.system, @old_system = PryRemote::System, Pry.config.system
end

#teardownObject

Code that has to be called after setup to return to the initial state



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/pry-remote.rb', line 182

def teardown
  # Reset output streams
  $stdout = @old_stdout
  $stderr = @old_stderr

  # Reset config
  Pry.config.pager = @old_pager

  # Reset sysem
  Pry.config.system = @old_system

  puts "[pry-remote] Remote session terminated"

  begin
    @client.kill
  rescue DRb::DRbConnError
    puts "[pry-remote] Continuing to stop service"
  ensure
    puts "[pry-remote] Ensure stop service"
    DRb.stop_service
  end
end

#uriString

Returns URI for DRb.

Returns:

  • (String)

    URI for DRb



227
228
229
# File 'lib/pry-remote.rb', line 227

def uri
  "druby://#{host}:#{port}"
end