Class: DBMapping::TaskMapper

Inherits:
Base
  • Object
show all
Defined in:
lib/data/sqlite/task_mapper.rb

Overview

class to apply ER-mapping for Task::Task objects to a sqlite database

Instance Attribute Summary

Attributes inherited from Base

#db_base

Instance Method Summary collapse

Methods inherited from Base

#check_max_id

Constructor Details

#initialize(database) ⇒ TaskMapper

initialization

Parameters:

  • database (SQLite3::Database)

    a reference of the database



8
9
10
# File 'lib/data/sqlite/task_mapper.rb', line 8

def initialize(database)
  super(database)
end

Instance Method Details

#create_task_from_result(result) ⇒ Object (private)

method to create a Task::Task from a result entry

Parameters:

  • result (Hash)

    an entry from the result set



49
50
51
52
53
# File 'lib/data/sqlite/task_mapper.rb', line 49

def create_task_from_result(result)
  Task::Task.new(result["Id"], Time.parse(result["Start"]),
                 Time.parse(result["End"]),
                 result["Description"])
end

#generate_tasksArray

public method to transform database persons to entity Task::Task

Returns:



14
15
16
17
18
19
20
21
# File 'lib/data/sqlite/task_mapper.rb', line 14

def generate_tasks
  results = @db_base.query_tasks
  tasks = Array.new()
  results.each { |result|
    tasks << create_task_from_result(result)
  }
  return tasks
end

#persist_tasks(tasks) ⇒ Object

method to persist a list of Task::Tasks to the database

Parameters:

  • tasks (Array)

    the Task::Task that should be be persisted



25
26
27
28
29
30
31
# File 'lib/data/sqlite/task_mapper.rb', line 25

def persist_tasks(tasks)
  tasks.each { |task|
    @db_base.insert_task(task.id, task.start_time, task.end_time,
                         task.description)
  }
  nil
end

#query_max_task_idInteger

method to query the max id of the task table

Returns:

  • (Integer)

    the maximal task id



41
42
43
# File 'lib/data/sqlite/task_mapper.rb', line 41

def query_max_task_id
  check_max_id(@db_base.query_max_task_id.next)
end

#query_task(id) ⇒ Task::Task | nil

method to search for a task by its id

Returns:

  • (Task::Task | nil)

    the result, if found or nil



35
36
37
# File 'lib/data/sqlite/task_mapper.rb', line 35

def query_task(id)
  create_task_from_result(@db_base.query_task(id).next)
end