Class: DBMapping::PersonMapper

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

Overview

Class to apply ER-mapping for Person::Person objects to a sqlite database

Direct Known Subclasses

StudentMapper

Instance Attribute Summary

Attributes inherited from Base

#db_base

Instance Method Summary collapse

Methods inherited from Base

#check_max_id

Constructor Details

#initialize(database) ⇒ PersonMapper

initialization

Parameters:

  • database (String)

    the path to the database



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

def initialize(database)
  super(database)
end

Instance Method Details

#generate_personsArray

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

Returns:



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

def generate_persons
  results = @db_base.query_persons
  persons = Array.new()
  results.each { |result|
    persons << Person::Person.new(result["Name"], Integer(result["Id"]))
  }
  return persons
end

#persist_persons(persons) ⇒ Object

method to persist a list of Person::Persons to the database

Parameters:



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

def persist_persons(persons)
  persons.each { |person|
    @db_base.insert_person(person.id, person.name)
  }
  nil
end

#query_max_person_idInteger

method to query the max id of the person table

Returns:

  • (Integer)

    the maximal person id



44
45
46
# File 'lib/data/sqlite/person_mapper.rb', line 44

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

#query_person(id) ⇒ Person::Person | nil

method to search for a person by its id

Returns:



34
35
36
37
38
39
40
# File 'lib/data/sqlite/person_mapper.rb', line 34

def query_person(id)
  result = @db_base.query_person_by_id(id).next
  if (result != nil)
    return Person::Person.new(result["Name"], Integer(result["Id"]))
  end
  nil
end