summaryrefslogtreecommitdiff
path: root/lib/action_file_executor.rb
diff options
context:
space:
mode:
authorSzymon Szukalski <szymon@skas.io>2024-10-25 10:00:36 +1100
committerSzymon Szukalski <szymon@skas.io>2024-10-25 10:00:36 +1100
commit05fda6c29f0fe4742b7ea6b237ea98f737b68b8b (patch)
tree3fca7845fc80f0d93370d2bdadb647f3f9c65d25 /lib/action_file_executor.rb
parentd41d881cf8af8cb8b6cb89b87a351585ee46063c (diff)
Document code with YARD
Diffstat (limited to 'lib/action_file_executor.rb')
-rw-r--r--lib/action_file_executor.rb30
1 files changed, 28 insertions, 2 deletions
diff --git a/lib/action_file_executor.rb b/lib/action_file_executor.rb
index d8e583c..2354dd7 100644
--- a/lib/action_file_executor.rb
+++ b/lib/action_file_executor.rb
@@ -2,6 +2,9 @@
require_relative 'family_tree'
+# ActionFileExecutor class to handle the execution of actions from a file.
+# This class processes each line of the file, extracts actions and their parameters,
+# and executes the corresponding actions. It handles both quoted and unquoted parameters.
class ActionFileExecutor
def initialize(file_path)
@file_path = file_path
@@ -22,20 +25,33 @@ class ActionFileExecutor
abort("Error: The file '#{@file_path}' does not exist.")
end
+ # Processes a line from the action file.
+ # Extracts the action and parameters, and executes the action if valid.
+ #
+ # @param line [String] The line to process.
+ # @return [void]
def process_line(line)
- return if line.empty? || comment?(line)
+ return if comment?(line)
action, params = extract_action_and_params(line)
-
return unless action && valid_action?(action, params)
execute_action(action, params)
end
+ # Checks if a line is a comment.
+ #
+ # @param line [String] The line to check.
+ # @return [Boolean] True if the line is a comment, false otherwise.
def comment?(line)
line.start_with?('#')
end
+ # Extracts the action and parameters from a line.
+ # Handles both quoted and unquoted parameters.
+ #
+ # @param line [String] The line to extract from.
+ # @return [Array<String, Array<String>>] An array containing the action and parameters.
def extract_action_and_params(line)
match = line.match(/^(\S+)(.*)$/)
return unless match
@@ -46,6 +62,11 @@ class ActionFileExecutor
[action, params]
end
+ # Executes the action with the given parameters.
+ #
+ # @param action [String] The action to execute.
+ # @param params [Array<String>] The parameters for the action.
+ # @return [void]
def execute_action(action, params)
case action
when 'ADD_CHILD'
@@ -55,6 +76,11 @@ class ActionFileExecutor
end
end
+ # Validates the action and its parameters.
+ #
+ # @param action [String] The action to validate.
+ # @param params [Array<String>] The parameters to validate.
+ # @return [Boolean] True if the action and parameters are valid, false otherwise.
def valid_action?(action, params)
case action
when 'ADD_CHILD' then params.size == 3