Class: Backspin::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/backspin/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_class:, args:, stdout: nil, stderr: nil, status: nil, result: nil, recorded_at: nil) ⇒ Command

Returns a new instance of Command.



9
10
11
12
13
14
15
16
# File 'lib/backspin/command.rb', line 9

def initialize(method_class:, args:, stdout: nil, stderr: nil, status: nil, result: nil, recorded_at: nil)
  @method_class = method_class
  @args = args
  @recorded_at = recorded_at

  # Accept either a CommandResult or individual stdout/stderr/status
  @result = result || CommandResult.new(stdout: stdout || "", stderr: stderr || "", status: status || 0)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/backspin/command.rb', line 7

def args
  @args
end

#method_classObject (readonly)

Returns the value of attribute method_class.



7
8
9
# File 'lib/backspin/command.rb', line 7

def method_class
  @method_class
end

#recorded_atObject (readonly)

Returns the value of attribute recorded_at.



7
8
9
# File 'lib/backspin/command.rb', line 7

def recorded_at
  @recorded_at
end

#resultObject (readonly)

Returns the value of attribute result.



7
8
9
# File 'lib/backspin/command.rb', line 7

def result
  @result
end

Class Method Details

.from_h(data) ⇒ Object

Create from hash (for loading from YAML)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/backspin/command.rb', line 48

def self.from_h(data)
  # Determine method class from command_type
  method_class = case data["command_type"]
  when "Open3::Capture3"
    Open3::Capture3
  when "Kernel::System"
    ::Kernel::System
  when "Backspin::Capturer"
    Backspin::Capturer
  else
    # Default to capture3 for backwards compatibility
    Open3::Capture3
  end

  new(
    method_class: method_class,
    args: data["args"],
    stdout: data["stdout"],
    stderr: data["stderr"],
    status: data["status"],
    recorded_at: data["recorded_at"]
  )
end

Instance Method Details

#statusObject



26
27
28
# File 'lib/backspin/command.rb', line 26

def status
  @result.status
end

#stderrObject



22
23
24
# File 'lib/backspin/command.rb', line 22

def stderr
  @result.stderr
end

#stdoutObject



18
19
20
# File 'lib/backspin/command.rb', line 18

def stdout
  @result.stdout
end

#to_h(filter: nil) ⇒ Object

Convert to hash for YAML serialization



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/backspin/command.rb', line 31

def to_h(filter: nil)
  data = {
    "command_type" => @method_class.name,
    "args" => scrub_args(@args),
    "stdout" => Backspin.scrub_text(@result.stdout),
    "stderr" => Backspin.scrub_text(@result.stderr),
    "status" => @result.status,
    "recorded_at" => @recorded_at
  }

  # Apply filter if provided
  data = filter.call(data) if filter

  data
end