Class: DataHandler::SqliteHandler

Inherits:
BaseHandler show all
Defined in:
lib/handler/sqlite_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) ⇒ SqliteHandler

initialization

Parameters:

  • filename (String)

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



20
21
22
23
# File 'lib/handler/sqlite_handler.rb', line 20

def initialize(filename)
  open_database(filename)
  super(filename)
end

Instance Attribute Details

#change_queueHash (readonly, private)

Returns a hash to map the change operations for the database.

Returns:

  • (Hash)

    a hash to map the change operations for the database



94
95
96
# File 'lib/handler/sqlite_handler.rb', line 94

def change_queue
  @change_queue
end

#databaseSQLite3::Database (readonly, private)

Returns a reference to the database.

Returns:

  • (SQLite3::Database)

    a reference to the database



90
91
92
# File 'lib/handler/sqlite_handler.rb', line 90

def database
  @database
end

#mapperHash (readonly, private)

Returns the mapping Symbol => Mapper.

Returns:

  • (Hash)

    the mapping Symbol => Mapper



92
93
94
# File 'lib/handler/sqlite_handler.rb', line 92

def mapper
  @mapper
end

Instance Method Details

#add_person(person) ⇒ Object

method to add a person to the transient storage

Parameters:



72
73
74
75
# File 'lib/handler/sqlite_handler.rb', line 72

def add_person(person)
  @change_queue[:person] << person
  nil
end

#add_task_to_person(person_id, task) ⇒ Object

method to add a task for a person to the transient storage



78
79
80
81
82
83
84
85
# File 'lib/handler/sqlite_handler.rb', line 78

def add_task_to_person(person_id, task)
  @change_queue[:task] << task
  if (@change_queue[:relation][person_id] == nil)
    @change_queue[:relation][person_id] = Array.new()
  end
  @change_queue[:relation][person_id] << task.id
  nil
end

#find_person_by_id(id) ⇒ Person | nil

method to search for a person by its id

Returns:

  • (Person | nil)

    the person if found or nil



48
49
50
# File 'lib/handler/sqlite_handler.rb', line 48

def find_person_by_id(id)
  @mapper[:person].query_person(id)
end

#find_task_to_id(id) ⇒ Task

method to search for a person by its id

Returns:

  • (Task)

    the task with the given id



54
55
56
# File 'lib/handler/sqlite_handler.rb', line 54

def find_task_to_id(id)
  @mapper[:task].query_task(id)
end

#get_personsArray

method to return all stored persons

Returns:

  • (Array)

    all stored persons



36
37
38
# File 'lib/handler/sqlite_handler.rb', line 36

def get_persons
  @mapper[:person].generate_persons
end

#get_tasksArray

method to return all stored tasks

Returns:

  • (Array)

    all stored tasks



42
43
44
# File 'lib/handler/sqlite_handler.rb', line 42

def get_tasks
  @mapper[:task].generate_tasks
end

#get_tasks_to_person(id) ⇒ Array

method to search for all tasks associated

Parameters:

  • id (Integer)

    the id of the person

Returns:

  • (Array)

    the tasks of the person with the given id



61
62
63
64
65
66
67
68
# File 'lib/handler/sqlite_handler.rb', line 61

def get_tasks_to_person(id)
  task_ids = @mapper[:relation].retrieve_tasks_for_person(id)
  tasks = Array.new()
  task_ids.each { |task_id|
    tasks << find_task_to_id(task_id)
  }
  tasks
end

#initialize_id_generatorsObject (private)

method to initialize the required id generators



109
110
111
112
113
# File 'lib/handler/sqlite_handler.rb', line 109

def initialize_id_generators
  Person::PersonIDGenerator.new(@mapper[:person].query_max_person_id)
  Task::TaskIDGenerator.new(@mapper[:task].query_max_task_id)
  nil
end

#open_database(db_path) ⇒ Object (private)

method to create a new database at the given path @ param [String] db_path the given file path



117
118
119
120
121
122
123
124
125
# File 'lib/handler/sqlite_handler.rb', line 117

def open_database(db_path)
  begin
    @database = SQLite3::Database.open(db_path)
    @database.results_as_hash = true
    nil
  rescue IOError || StandardError
    raise ArgumentError, "Error [DBCreator]: invalid path to database."
  end
end

#persist_dataObject

method to persis the newly created or updated data to the database



26
27
28
29
30
31
32
# File 'lib/handler/sqlite_handler.rb', line 26

def persist_data
  @mapper[:person].persist_persons(@change_queue[:person])
  @mapper[:task].persist_tasks(@change_queue[:task])
  @mapper[:relation].
         generate_person_and_task_relations(@change_queue[:relation])
  nil
end

#prepare_dataObject (private)

method to initialize the required data objects



97
98
99
100
101
102
103
104
105
106
# File 'lib/handler/sqlite_handler.rb', line 97

def prepare_data
  @mapper = { :person => DBMapping::StudentMapper.new(@database),
              :task => DBMapping::TaskMapper.new(@database),
              :relation => DBMapping::RelationsMapper.new(@database)}
  @change_queue = { :person => Array.new(),
                    :task => Array.new(),
                    :relation => Hash.new()}
  initialize_id_generators
  nil
end