Class: PryRemoteEm::Sandbox

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

Overview

See Readme for Sandbox using guide

Constant Summary collapse

@@last_errors =

Working with errors

[]
@@ignore_errors =
[]
@@error_classes =
Hash.new { |hash, key| hash[key] = 0 }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pryObject

Returns the value of attribute pry.



6
7
8
# File 'lib/pry-remote-em/sandbox.rb', line 6

def pry
  @pry
end

#serverObject

Returns the value of attribute server.



6
7
8
# File 'lib/pry-remote-em/sandbox.rb', line 6

def server
  @server
end

Class Method Details

.add_error(exception, source_binding = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pry-remote-em/sandbox.rb', line 32

def self.add_error(exception, source_binding = nil)
  unless exception.kind_of?(Exception) && (source_binding.nil? || source_binding.kind_of?(Binding))
    raise ArgumentError, 'exception and optional binding expected'
  end

  return if @@last_errors.map(&:object_id).include?(exception.object_id) || @@ignore_errors.include?(exception.class)

  timestamp = Time.now
  exception.define_singleton_method(:source_timestamp) { timestamp }

  exception.define_singleton_method(:source_binding) { source_binding } if source_binding

  @@last_errors.push(exception)
  @@error_classes[exception.class] += 1
  Metrics.add(:errors)

  maximum_errors = ENV['PRYEMSANDBOXERRORS'].nil? || ENV['PRYEMSANDBOXERRORS'].empty? ? MAXIMUM_ERRORS_IN_SANDBOX : ENV['PRYEMSANDBOXERRORS'].to_i
  @@last_errors.shift if @@last_errors.size > maximum_errors
end

.any_errors?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/pry-remote-em/sandbox.rb', line 56

def self.any_errors?
  @@last_errors.any?
end

.ignore_errorsObject



64
65
66
# File 'lib/pry-remote-em/sandbox.rb', line 64

def self.ignore_errors
  @@ignore_errors
end

.last_errorObject



60
61
62
# File 'lib/pry-remote-em/sandbox.rb', line 60

def self.last_error
  @@last_errors.last
end

.last_errorsObject



52
53
54
# File 'lib/pry-remote-em/sandbox.rb', line 52

def self.last_errors
  @@last_errors
end

Instance Method Details

#error_classesObject



22
23
24
25
# File 'lib/pry-remote-em/sandbox.rb', line 22

def error_classes
  return puts 'No errors, yay!' if @@error_classes.empty?
  puts @@error_classes.map { |key, value| "#{key}: #{value}" }
end

#error_historyObject



27
28
29
30
# File 'lib/pry-remote-em/sandbox.rb', line 27

def error_history
  return puts 'No errors, yay!' unless any_errors?
  puts @@last_errors.map { |error| "#{error.source_timestamp} #{"#{error.class}: #{error.message}".sub(/(?<=^.{51}).{4,}$/, '...')}" }
end

#inspectObject

Safely show in Pry prompt



82
83
84
# File 'lib/pry-remote-em/sandbox.rb', line 82

def inspect
  'sandbox'
end

#show_metricsObject

Metrics related methods



76
77
78
# File 'lib/pry-remote-em/sandbox.rb', line 76

def show_metrics
  puts Metrics.list.map { |key, value| "#{key}: #{value}" }
end