Class: RailsSpotlight::RailsCommandExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_spotlight/rails_command_executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#consoleObject (readonly)

Returns the value of attribute console.



32
33
34
# File 'lib/rails_spotlight/rails_command_executor.rb', line 32

def console
  @console
end

#errorObject (readonly)

Returns the value of attribute error.



32
33
34
# File 'lib/rails_spotlight/rails_command_executor.rb', line 32

def error
  @error
end

#resultObject (readonly)

Returns the value of attribute result.



32
33
34
# File 'lib/rails_spotlight/rails_command_executor.rb', line 32

def result
  @result
end

#syntax_errorObject (readonly)

Returns the value of attribute syntax_error.



32
33
34
# File 'lib/rails_spotlight/rails_command_executor.rb', line 32

def syntax_error
  @syntax_error
end

Instance Method Details

#execute(command) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rails_spotlight/rails_command_executor.rb', line 5

def execute(command)
  output_stream = StringIO.new # Create a new StringIO object to capture output
  @result = nil
  @error = nil
  @syntax_error = false

  begin
    original_stdout = $stdout
    $stdout = output_stream
    @result = eval(command) # rubocop:disable Security/Eval
  rescue SyntaxError => e
    @error = e
    @syntax_error = true
  rescue => e # rubocop:disable Style/RescueStandardError
    @error = e
  ensure
    $stdout = original_stdout
  end

  @console = output_stream.string
  self
rescue => e # rubocop:disable Style/RescueStandardError
  @error = e
ensure
  self
end

#execution_successful?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rails_spotlight/rails_command_executor.rb', line 34

def execution_successful?
  error.nil?
end

#result_as_json(inspect_types: false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rails_spotlight/rails_command_executor.rb', line 38

def result_as_json(inspect_types: false)
  if error
    {
      status: :error,
      syntax_error: syntax_error,
      error: error.respond_to?(:message) ? error.message : error.to_s,
      backtrace: error.respond_to?(:backtrace) ? error.backtrace : nil
    }
  else
    {
      status: :ok,
      inspect: result.inspect,
      raw: result,
      type: result.class.name,
      types: result_inspect_types(inspect_types, result),
      console: console
    }
  end
end