Class: DataHandler::SqliteHandler
- Inherits:
-
BaseHandler
- Object
- BaseHandler
- DataHandler::SqliteHandler
- 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
-
#change_queue ⇒ Hash
readonly
private
A hash to map the change operations for the database.
-
#database ⇒ SQLite3::Database
readonly
private
A reference to the database.
-
#mapper ⇒ Hash
readonly
private
The mapping Symbol => Mapper.
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) ⇒ SqliteHandler
constructor
initialization.
-
#initialize_id_generators ⇒ Object
private
method to initialize the required id generators.
-
#open_database(db_path) ⇒ Object
private
method to create a new database at the given path @ param [String] db_path the given file path.
-
#persist_data ⇒ Object
method to persis the newly created or updated data to the database.
-
#prepare_data ⇒ Object
private
method to initialize the required data objects.
Constructor Details
#initialize(filename) ⇒ SqliteHandler
initialization
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_queue ⇒ Hash (readonly, private)
Returns 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 |
#database ⇒ SQLite3::Database (readonly, private)
Returns a reference to the database.
90 91 92 |
# File 'lib/handler/sqlite_handler.rb', line 90 def database @database end |
#mapper ⇒ Hash (readonly, private)
Returns 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
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
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
54 55 56 |
# File 'lib/handler/sqlite_handler.rb', line 54 def find_task_to_id(id) @mapper[:task].query_task(id) end |
#get_persons ⇒ Array
method to return all stored persons
36 37 38 |
# File 'lib/handler/sqlite_handler.rb', line 36 def get_persons @mapper[:person].generate_persons end |
#get_tasks ⇒ Array
method to return 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
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_generators ⇒ Object (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_data ⇒ Object
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_data ⇒ Object (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 |