FileControlledSpinner

InfoGenerateCreated ByPackages

This Ruby code defines a `SpinnerAgent` class within the `Sublayer::Agents` module. The class inherits from a `Base` class and is designed to manage the state of a spinner based on commands read from a control file.

Key functionalities include:

1. **Initialization**: The `initialize` method sets up instance variables for the paths to the spinner file and control command file, and initializes the spinning state to false.

2. **Trigger**: The `trigger_on_files_changed` block specifies the control command file as the file to monitor for changes.

3. **Check Status**: The `check_status` block reads the command from the control command file, starts or stops the spinner based on the command, and prints the current spinner status.

4. **Goal Condition**: The `goal_condition` block evaluates if the spinner is currently active.

5. **Step**: The `step` block performs actions related to the spinner, writing either 'Spinning...' or 'Stopped' to the spinner file based on the spinner's state.

Noteworthy details include:

- The agent reacts to changes in the control command file.
- The spinner's state is managed via simple 'start' and 'stop' commands.
- The `step` method contains placeholder logic, indicating where the actual spinner functionalities would be implemented.

module Sublayer
  module Agents
    class SpinnerAgent < Base
      def initialize(spinner_file_path:, control_command_file_path:)
        @spinner_file_path = spinner_file_path
        @control_command_file_path = control_command_file_path
        @spinning = false
      end

      trigger_on_files_changed do
        [@control_command_file_path]
      end

      check_status do
        command = File.read(@control_command_file_path).strip

        if command == "start"
          @spinning = true
        elsif command == "stop"
          @spinning = false
        end

        @spinning ? puts("Spinner started") : puts("Spinner stopped")
      end

      goal_condition do
        @spinning == true
      end

      step do
        puts "Taking a step"
        # Placeholder for spinner logic
        spinner_contents = @spinning ? 'Spinning...' : 'Stopped'

        Sublayer::Actions::WriteFileAction.new(
          file_contents: spinner_contents, file_path: @spinner_file_path
        ).call
      end
    end
  end
end