Class: DataHandler::FileHandler
- Inherits:
-
BaseHandler
- Object
- BaseHandler
- DataHandler::FileHandler
- Defined in:
- lib/handler/file_handler.rb
Overview
This class serves as a handler between the repositories and the queries. It also takes care about the initialization of the repositories and the id generators. The handler also provides methods to save and load data from and to a repository.
Instance Attribute Summary collapse
-
#repository ⇒ DataRepository
readonly
private
The repository.
Attributes inherited from BaseHandler
Instance Method Summary collapse
-
#add_person(person) ⇒ Object
method to add a person to the transient storage.
-
#add_task_to_person(person_id, task) ⇒ Object
method to add a task for a person to the transient storage.
-
#find_person_by_id(id) ⇒ Person | nil
method to search for a person by its id.
-
#find_task_to_id(id) ⇒ Task
method to search for a person by its id.
-
#get_persons ⇒ Array
method to return all stored persons.
-
#get_tasks ⇒ Array
method to return all stored tasks.
-
#get_tasks_to_person(id) ⇒ Array
method to search for all tasks associated.
-
#initialize(filename) ⇒ FileHandler
constructor
initialization.
-
#initialize_id_generators ⇒ Object
private
method to initialize the required id generators.
-
#persist_data ⇒ Object
method to save the content of the repository to the path specified by the filename and based on the used adapter.
-
#prepare_data ⇒ Object
private
method to load the content into the repository from the path specified by the filename and based on the used adapter.
Constructor Details
#initialize(filename) ⇒ FileHandler
initialization
16 17 18 19 |
# File 'lib/handler/file_handler.rb', line 16 def initialize(filename) @repository=DataRepository.new() super(filename) end |
Instance Attribute Details
#repository ⇒ DataRepository (readonly, private)
Returns the repository.
81 82 83 |
# File 'lib/handler/file_handler.rb', line 81 def repository @repository end |
Instance Method Details
#add_person(person) ⇒ Object
method to add a person to the transient storage
65 66 67 68 |
# File 'lib/handler/file_handler.rb', line 65 def add_person(person) @repository.add_person(person) nil end |
#add_task_to_person(person_id, task) ⇒ Object
method to add a task for a person to the transient storage
73 74 75 76 |
# File 'lib/handler/file_handler.rb', line 73 def add_task_to_person(person_id, task) @repository.add_task_to_person(person_id, task) nil end |
#find_person_by_id(id) ⇒ Person | nil
method to search for a person by its id
45 46 47 |
# File 'lib/handler/file_handler.rb', line 45 def find_person_by_id(id) @repository.find_person_by_id(id) end |
#find_task_to_id(id) ⇒ Task
method to search for a person by its id
52 53 54 |
# File 'lib/handler/file_handler.rb', line 52 def find_task_to_id(id) @repository.find_task_to_id(id) end |
#get_persons ⇒ Array
method to return all stored persons
32 33 34 |
# File 'lib/handler/file_handler.rb', line 32 def get_persons @repository.get_persons end |
#get_tasks ⇒ Array
method to return all stored tasks
38 39 40 |
# File 'lib/handler/file_handler.rb', line 38 def get_tasks @repository.get_tasks end |
#get_tasks_to_person(id) ⇒ Array
method to search for all tasks associated
59 60 61 |
# File 'lib/handler/file_handler.rb', line 59 def get_tasks_to_person(id) @repository.get_tasks_to_person(id) end |
#initialize_id_generators ⇒ Object (private)
method to initialize the required id generators
98 99 100 101 102 |
# File 'lib/handler/file_handler.rb', line 98 def initialize_id_generators Person::PersonIDGenerator.new(@repository.max_person_id) Task::TaskIDGenerator.new(@repository.max_task_id) nil end |
#persist_data ⇒ Object
method to save the content of the repository to the path specified by the filename and based on the used adapter
23 24 25 26 27 28 |
# File 'lib/handler/file_handler.rb', line 23 def persist_data writer = FileWriter.new(filename) writer.write_all_persons(@repository.get_persons) writer.write_all_tasks(@repository.return_output_mapping) nil end |
#prepare_data ⇒ Object (private)
method to load the content into the repository from the path specified by the filename and based on the used adapter
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/handler/file_handler.rb', line 86 def prepare_data begin @repository = FileReader.read_file(filename) initialize_id_generators nil rescue StandardError => e raise IOError, " An Error occurred while loading the database: #{e.}".red end end |