Class: DataRepository
- Inherits:
-
Object
- Object
- DataRepository
- Defined in:
- lib/data/file_storage/data_repository.rb
Overview
This class serves as a data repository mapping (Person => Task). A Person can be the parent class Person or any of its children. To ensure the correct addition of a Person they should be add via #add_person. Persons can be looked up with the object in the hash or by its person id. The Task of a Person can be found by #get_tasks_to_person with an id.
Instance Attribute Summary collapse
-
#mapping ⇒ Hash
readonly
private
The repository mapping (Person => task_ids).
-
#max_person_id ⇒ Integer
readonly
The highest person id from #set_max_ids.
-
#max_task_id ⇒ Integer
readonly
The highest task id from #set_max_ids.
-
#tasks ⇒ Array
readonly
private
The available tasks.
Instance Method Summary collapse
-
#add_person(person) ⇒ Object
adds a person to the repository and also checks for duplicated entries.
-
#add_task_to_person(person_id, task) ⇒ Object
assigns a task to the provided person id.
-
#check_for_existing_task(task) ⇒ boolean
private
method to check if a given task already exists in the repository.
-
#check_for_unique_id(person) ⇒ boolean
private
method to check if the person already exists.
-
#find_person_by_id(person_id) ⇒ Person | nil
query to find a person by the given id.
-
#find_task_to_id(task_id) ⇒ Task
method to find a task specified by its id.
-
#get_persons ⇒ Array
query to return all persons stored in the repository.
-
#get_tasks ⇒ Array
query to return all tasks stored in the repository.
-
#get_tasks_to_person(person_id) ⇒ Array
query to find all tasks to a person specified by its id.
-
#initialize ⇒ DataRepository
constructor
initialization.
-
#return_output_mapping ⇒ Hash
method to return the stored data for the FileWriter.
-
#set_max_ids(ids) ⇒ Object
sets the start values for the two id generators.
Constructor Details
#initialize ⇒ DataRepository
initialization
17 18 19 20 21 22 |
# File 'lib/data/file_storage/data_repository.rb', line 17 def initialize @mapping = Hash.new() @tasks = Array.new() @max_person_id = 0 @max_task_id = 0 end |
Instance Attribute Details
#mapping ⇒ Hash (readonly, private)
Returns the repository mapping (Person => task_ids).
129 130 131 |
# File 'lib/data/file_storage/data_repository.rb', line 129 def mapping @mapping end |
#max_person_id ⇒ Integer (readonly)
Returns the highest person id from #set_max_ids.
12 13 14 |
# File 'lib/data/file_storage/data_repository.rb', line 12 def max_person_id @max_person_id end |
#max_task_id ⇒ Integer (readonly)
Returns the highest task id from #set_max_ids.
14 15 16 |
# File 'lib/data/file_storage/data_repository.rb', line 14 def max_task_id @max_task_id end |
#tasks ⇒ Array (readonly, private)
Returns the available tasks.
131 132 133 |
# File 'lib/data/file_storage/data_repository.rb', line 131 def tasks @tasks end |
Instance Method Details
#add_person(person) ⇒ Object
adds a person to the repository and also checks for duplicated entries
27 28 29 30 31 32 33 34 |
# File 'lib/data/file_storage/data_repository.rb', line 27 def add_person(person) if (check_for_unique_id(person)) @mapping[person] = Array.new() else raise IndexError, " Error: duplicated id found: #{person.id}, " \ " #{person.name} cannot be added.".red end end |
#add_task_to_person(person_id, task) ⇒ Object
assigns a task to the provided person id
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/data/file_storage/data_repository.rb', line 40 def add_task_to_person(person_id, task) person = find_person_by_id(person_id) if (person != nil) @mapping[person] << task.id @tasks << task if(!check_for_existing_task(task)) else raise ArgumentError, " Error: id #{person_id} was not found.".red end nil end |
#check_for_existing_task(task) ⇒ boolean (private)
method to check if a given task already exists in the repository
143 144 145 146 147 148 |
# File 'lib/data/file_storage/data_repository.rb', line 143 def check_for_existing_task(task) @tasks.each { |element| return true if(element.id == task.id) } false end |
#check_for_unique_id(person) ⇒ boolean (private)
method to check if the person already exists
136 137 138 |
# File 'lib/data/file_storage/data_repository.rb', line 136 def check_for_unique_id(person) find_person_by_id(person.id) == nil end |
#find_person_by_id(person_id) ⇒ Person | nil
query to find a person by the given id
54 55 56 57 58 59 |
# File 'lib/data/file_storage/data_repository.rb', line 54 def find_person_by_id(person_id) @mapping.each_key { |key| return key if (key.id == person_id) } nil end |
#find_task_to_id(task_id) ⇒ Task
method to find a task specified by its id
103 104 105 106 107 108 |
# File 'lib/data/file_storage/data_repository.rb', line 103 def find_task_to_id(task_id) @tasks.each { |task| return task if (task_id == task.id) } raise ArgumentError, "Error: Task with #{task_id} does not exist.".red end |
#get_persons ⇒ Array
query to return all persons stored in the repository
63 64 65 |
# File 'lib/data/file_storage/data_repository.rb', line 63 def get_persons @mapping.keys end |
#get_tasks ⇒ Array
query to return all tasks stored in the repository
112 113 114 |
# File 'lib/data/file_storage/data_repository.rb', line 112 def get_tasks @tasks end |
#get_tasks_to_person(person_id) ⇒ Array
query to find all tasks to a person specified by its id
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/data/file_storage/data_repository.rb', line 71 def get_tasks_to_person(person_id) person = find_person_by_id(person_id) puts " Warning: person for id #{person_id} not found." if (person == nil) ids = @mapping[person] results = Array.new() ids.each { |id| results << find_task_to_id(id) } results end |
#return_output_mapping ⇒ Hash
method to return the stored data for the FileWriter
118 119 120 121 122 123 124 |
# File 'lib/data/file_storage/data_repository.rb', line 118 def return_output_mapping output = Hash.new() @mapping.each_pair { |person, task_ids| output[person] = get_tasks_to_person(person.id) } output end |
#set_max_ids(ids) ⇒ Object
sets the start values for the two id generators
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/data/file_storage/data_repository.rb', line 85 def set_max_ids(ids) if (ids[0] > 0) @max_person_id = ids[0] else raise ArgumentError, "Error: given person id #{max_person_id} <= 0".red end if (ids[1] > 0) @max_task_id = ids[1] else raise ArgumentError, "Error: given person id #{max_task_id} <= 0".red end nil end |