Class: DataHandler::FileHandler

Inherits:
BaseHandler show all
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

Attributes inherited from BaseHandler

#filename

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ FileHandler

initialization

Parameters:

  • filename (String)

    the filename from where the data should be loaded or to where the data should be saved



16
17
18
19
# File 'lib/handler/file_handler.rb', line 16

def initialize(filename)
  @repository=DataRepository.new()
  super(filename)
end

Instance Attribute Details

#repositoryDataRepository (readonly, private)

Returns the repository.

Returns:



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

Parameters:

  • person (Person)

    the person that be added



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

Parameters:

  • person_id (Integer)

    the provided person id

  • task (Task)

    the task that should be added



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

Parameters:

  • id (Integer)

    the provided person id

Returns:

  • (Person | nil)

    the person if found or nil



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

Parameters:

  • id (Integer)

    the provided task id

Returns:

  • (Task)

    the task with the given 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_personsArray

method to return all stored persons

Returns:

  • (Array)

    all stored persons



32
33
34
# File 'lib/handler/file_handler.rb', line 32

def get_persons
  @repository.get_persons
end

#get_tasksArray

method to return all stored tasks

Returns:

  • (Array)

    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

Parameters:

  • id (Integer)

    the provided person id

Returns:

  • (Array)

    the tasks of the person with the given id



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_generatorsObject (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_dataObject

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_dataObject (private)

method to load the content into the repository from the path specified by the filename and based on the used adapter

Raises:

  • (IOError)

    if an error occurs during the loading process



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.message}".red
  end
end