Class: DBMapping::StudentMapper
- Inherits:
-
PersonMapper
- Object
- Base
- PersonMapper
- DBMapping::StudentMapper
- Defined in:
- lib/data/sqlite/student_mapper.rb
Overview
Class to apply ER-mapping for Person::Person and Person::Student objects to a sqlite database
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#generate_persons ⇒ Array
public method to transform database persons to entity Person.
-
#initialize(database) ⇒ StudentMapper
constructor
initialization.
-
#persist_persons(persons) ⇒ Object
method to persist a list of Persons to the database.
-
#query_person(id) ⇒ Person::Person | nil
method to search for a person by its id.
Methods inherited from PersonMapper
Methods inherited from Base
Constructor Details
#initialize(database) ⇒ StudentMapper
initialization
9 10 11 |
# File 'lib/data/sqlite/student_mapper.rb', line 9 def initialize(database) @db_base = SqliteDatabase::DBStudent.new(database) end |
Instance Method Details
#generate_persons ⇒ Array
public method to transform database persons to entity Person
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/data/sqlite/student_mapper.rb', line 15 def generate_persons results = @db_base.query_persons persons = Array.new() results.each { |result| id = Integer(result["Id"]) mat_nr = @db_base.query_matnr_for_student(id).next if (mat_nr != nil) persons << Person::Student.new(result["Name"], id, mat_nr["Mat_Nr"]) else persons << Person::Person.new(result["Name"], id) end } return persons end |
#persist_persons(persons) ⇒ Object
method to persist a list of Persons to the database
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/data/sqlite/student_mapper.rb', line 32 def persist_persons(persons) persons.each { |person| case (person) when Person::Student @db_base.insert_student(person.id, person.name, person.mat_nr) when Person::Person @db_base.insert_person(person.id, person.name) else raise ArgumentError, "Cannot persist object. Class not found." end } nil end |
#query_person(id) ⇒ Person::Person | nil
method to search for a person by its id
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/data/sqlite/student_mapper.rb', line 48 def query_person(id) result = @db_base.query_student_by_id(id).next if (result != nil) return Person::Student.new(result["Name"], Integer(result["Id"]), Integer(result["Mat_Nr"])) end result = @db_base.query_person_by_id(id).next if (result != nil) return Person::Person.new(result["Name"], Integer(result["Id"])) end nil end |