summaryrefslogtreecommitdiff
path: root/lib/action_file_executor.rb
diff options
context:
space:
mode:
authorSzymon Szukalski <szymon@skas.io>2024-10-25 01:19:25 +1100
committerSzymon Szukalski <szymon@skas.io>2024-10-25 01:19:25 +1100
commitfa50df797d8fcb1e3636683cf2ff1582f35bc181 (patch)
treed5a68c46854fcfecf548e0f13a438bc01f8e490c /lib/action_file_executor.rb
parenta89fa6a27bb0e2b9797dd01052aaf22b38296821 (diff)
Implement extended relationships
- Support uncle, aunt, in-law, son, daughter relationships
Diffstat (limited to 'lib/action_file_executor.rb')
-rw-r--r--lib/action_file_executor.rb38
1 files changed, 18 insertions, 20 deletions
diff --git a/lib/action_file_executor.rb b/lib/action_file_executor.rb
index 49cb4b5..d8e583c 100644
--- a/lib/action_file_executor.rb
+++ b/lib/action_file_executor.rb
@@ -5,31 +5,31 @@ require_relative 'family_tree'
class ActionFileExecutor
def initialize(file_path)
@file_path = file_path
- validate_file
+ validate_file!
end
def execute_actions
- File.open(@file_path, 'r') do |file|
- file.each_line do |line|
- process_line(line.strip)
- end
+ File.foreach(@file_path) do |line|
+ process_line(line.strip)
end
end
private
- def validate_file
+ def validate_file!
return if File.exist?(@file_path)
- puts "Error: The file '#{@file_path}' does not exist."
- exit 1
+ abort("Error: The file '#{@file_path}' does not exist.")
end
def process_line(line)
return if line.empty? || comment?(line)
action, params = extract_action_and_params(line)
- execute_action(action, params) if action
+
+ return unless action && valid_action?(action, params)
+
+ execute_action(action, params)
end
def comment?(line)
@@ -42,26 +42,24 @@ class ActionFileExecutor
action = match[1]
params = match[2].scan(/"([^"]+)"|(\S+)/).flatten.compact
+ params.map! { |param| param&.strip }
[action, params]
end
def execute_action(action, params)
case action
when 'ADD_CHILD'
- handle_add_child(*params)
+ puts FamilyTree.instance.add_child(*params)
when 'GET_RELATIONSHIP'
- handle_get_relationship(*params)
- else
- puts "Ignoring unsupported action: [#{action}]"
+ puts FamilyTree.instance.get_relationship(*params)
end
end
- def handle_add_child(*params)
- FamilyTree.instance.add_child(*params)
- end
-
- def handle_get_relationship(*params)
- result = FamilyTree.instance.get_relationship(*params)
- puts result.empty? ? 'NONE' : result.map(&:name).join(', ')
+ def valid_action?(action, params)
+ case action
+ when 'ADD_CHILD' then params.size == 3
+ when 'GET_RELATIONSHIP' then params.size == 2
+ else false # Ignore unsupported actions
+ end
end
end