diff options
| author | Szymon Szukalski <szymon@skas.io> | 2024-10-24 21:10:57 +1100 |
|---|---|---|
| committer | Szymon Szukalski <szymon@skas.io> | 2024-10-24 21:10:57 +1100 |
| commit | 9bc26146397acb5a216e20d5eb55bb2a582fdd3e (patch) | |
| tree | 4c02f13ca30e673417870114050f7a3d653ad47d /lib/action_file_executor.rb | |
| parent | 55475178a8c0e610103e37027cc0a7a387d72f91 (diff) | |
Implement key data model
- Added classes for Person, Gender, Family, FamilyTree
- Replaced FamilyTreeManager with FamilyTree
- Add FamilyFactory for seeding the initial FamilyTree for King Arthur and Queen Margaret
- Added a RelationshipManager for linking spouses correctly
- Refactored ActionFileExecutor for readability
- More test coverage
Diffstat (limited to 'lib/action_file_executor.rb')
| -rw-r--r-- | lib/action_file_executor.rb | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/lib/action_file_executor.rb b/lib/action_file_executor.rb index 1cd3a94..49cb4b5 100644 --- a/lib/action_file_executor.rb +++ b/lib/action_file_executor.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require_relative 'family_tree_manager' +require_relative 'family_tree' class ActionFileExecutor def initialize(file_path) @@ -11,15 +11,7 @@ class ActionFileExecutor def execute_actions File.open(@file_path, 'r') do |file| file.each_line do |line| - action, *params = line.split(' ') - case action - when 'ADD_CHILD' - FamilyTreeManager.instance.add_child(*params) - when 'GET_RELATIONSHIP' - FamilyTreeManager.instance.query_hierarchy(*params) - else - puts "Ignoring unsupported action: [#{action}]" - end + process_line(line.strip) end end end @@ -32,4 +24,44 @@ class ActionFileExecutor puts "Error: The file '#{@file_path}' does not exist." exit 1 end + + def process_line(line) + return if line.empty? || comment?(line) + + action, params = extract_action_and_params(line) + execute_action(action, params) if action + end + + def comment?(line) + line.start_with?('#') + end + + def extract_action_and_params(line) + match = line.match(/^(\S+)(.*)$/) + return unless match + + action = match[1] + params = match[2].scan(/"([^"]+)"|(\S+)/).flatten.compact + [action, params] + end + + def execute_action(action, params) + case action + when 'ADD_CHILD' + handle_add_child(*params) + when 'GET_RELATIONSHIP' + handle_get_relationship(*params) + else + puts "Ignoring unsupported action: [#{action}]" + 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(', ') + end end |
