Class: DataRepository

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

Instance Method Summary collapse

Constructor Details

#initializeDataRepository

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

#mappingHash (readonly, private)

Returns the repository mapping (Person => task_ids).

Returns:

  • (Hash)

    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_idInteger (readonly)

Returns the highest person id from #set_max_ids.

Returns:



12
13
14
# File 'lib/data/file_storage/data_repository.rb', line 12

def max_person_id
  @max_person_id
end

#max_task_idInteger (readonly)

Returns the highest task id from #set_max_ids.

Returns:



14
15
16
# File 'lib/data/file_storage/data_repository.rb', line 14

def max_task_id
  @max_task_id
end

#tasksArray (readonly, private)

Returns the available tasks.

Returns:

  • (Array)

    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

Parameters:

  • person (Person)

    the person that should be added

Raises:

  • (IndexError)

    if a person with this id already exists



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

Parameters:

  • person_id (Integer)

    the id of the person which should get the task

  • task (Task)

    the task that should be assigned

Raises:

  • (ArgumentError)

    if no person could be found to the given 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

Parameters:

  • task (Task)

    the given task

Returns:

  • (boolean)

    true, if a task with the same id is found, false if not



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

Parameters:

  • person (Person)

    the person that should be checked

Returns:

  • (boolean)

    true, if not in the list, false, if in the list



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

Parameters:

  • person_id (Integer)

    the id of the searched person

Returns:

  • (Person | nil)

    the person if found or nil



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

Parameters:

  • task_id (Integer)

    the id of the searched task

Returns:

  • (Task)

    the task with the given id

Raises:

  • (ArgumentError)

    if no task can be found for the given 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_personsArray

query to return all persons stored in the repository

Returns:

  • (Array)

    an array with all the persons of the repository



63
64
65
# File 'lib/data/file_storage/data_repository.rb', line 63

def get_persons
  @mapping.keys
end

#get_tasksArray

query to return all tasks stored in the repository

Returns:

  • (Array)

    an array with all the tasks of 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

Parameters:

  • person_id (Integer)

    the id of the searched person

Returns:

  • (Array)

    the tasks of the person or nil if the person was not found



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_mappingHash

method to return the stored data for the FileWriter

Returns:

  • (Hash)

    the mapping of persons to their tasks



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

Parameters:

  • ids (Array)

    the two generator values

Raises:

  • (ArgumentError)

    if one of the id values is <= 0



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