From 6a192c4555438c29338131e8332c0dfe4969fa49 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Sun, 8 Jul 2018 18:20:23 -0400 Subject: [PATCH] removing initialize from Person --- app/models/company.rb | 16 ++++----- app/models/location.rb | 14 ++++---- app/models/person.rb | 74 ++++++++++++++++++++---------------------- 3 files changed, 51 insertions(+), 53 deletions(-) diff --git a/app/models/company.rb b/app/models/company.rb index 903d154..eecfa76 100644 --- a/app/models/company.rb +++ b/app/models/company.rb @@ -41,11 +41,11 @@ class Company last_company_id = result["id"] end if result["person_id"] - companies.last.employees.push(Person.new({ - "id" => result["person_id"], + companies.last.employees.push({ + "id" => result["person_id"].to_i, "name" => result["person_name"], - "age" => result["age"] - })) + "age" => result["age"].to_i, + }) end end return companies @@ -69,11 +69,11 @@ class Company employees = []; results.each do |result| if result["person_id"] - employees.push(Person.new({ - "id" => result["person_id"], + employees.push({ + "id" => result["person_id"].to_i, "name" => result["person_name"], - "age" => result["age"] - })) + "age" => result["age"].to_i, + }) end end return Company.new({ diff --git a/app/models/location.rb b/app/models/location.rb index 433985d..707a115 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -42,11 +42,11 @@ class Location last_location_id = result["id"] end if result["person_id"] - new_person = Person.new({ - "id" => result["person_id"], + new_person = { + "id" => result["person_id"].to_i, "name" => result["name"], - "age" => result["age"], - }) + "age" => result["age"].to_i, + } locations.last.inhabitants.push(new_person) end end @@ -70,10 +70,10 @@ class Location inhabitants = [] results.each do |result| if result["person_id"] - inhabitants.push Person.new({ - "id" => result["person_id"], + inhabitants.push({ + "id" => result["person_id"].to_i, "name" => result["name"], - "age" => result["age"] + "age" => result["age"].to_i, }) end end diff --git a/app/models/person.rb b/app/models/person.rb index 47c3479..9112b30 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -1,19 +1,6 @@ class Person - attr_reader :id, :name, :age, :home, :employers # connect to postgres - DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts') - - def initialize(opts = {}) - @id = opts["id"].to_i - @name = opts["name"] - @age = opts["age"].to_i - if opts["home"] - @home = opts["home"] - end - if opts["employers"] - @employers = opts["employers"] - end - end + DB = PG.connect({:host => "localhost", :port => 5432, :dbname => 'contacts'}) def self.all results = DB.exec( @@ -50,15 +37,13 @@ class Person } ) end - new_person = Person.new( - { - "id" => result["id"], - "name" => result["name"], - "age" => result["age"], - "home" => home, - "employers" => [] - } - ) + new_person = { + "id" => result["id"], + "name" => result["name"], + "age" => result["age"], + "home" => home, + "employers" => [] + } people.push(new_person) end if result["company_id"] @@ -67,7 +52,7 @@ class Person "name" => result["company"], "industry" => result["industry"] }) - people.last.employers.push(employer) + people.last["employers"].push(employer) end end return people @@ -116,15 +101,13 @@ class Person })) end end - person = Person.new( - { - "id" => result["id"], - "name" => result["name"], - "age" => result["age"], - "home" => home, - "employers" => employers - } - ) + person = { + "id" => result["id"], + "name" => result["name"], + "age" => result["age"], + "home" => home, + "employers" => employers + } return person end @@ -136,12 +119,17 @@ class Person RETURNING id, name, age, home_id; SQL ) - return Person.new(results.first) + return { + "id" => results.first["id"].to_i, + "name" => results.first["name"], + "age" => results.first["age"].to_i, + "home_id" => results.first["home_id"].to_i, + } end def self.delete(id) results = DB.exec("DELETE FROM people WHERE id=#{id};") - return { deleted: true } + return { "deleted" => true } end def self.update(id, opts={}) @@ -153,7 +141,12 @@ class Person RETURNING id, name, age, home_id; SQL ) - return Person.new(results.first) + return { + "id" => results.first["id"].to_i, + "name" => results.first["name"], + "age" => results.first["age"].to_i, + "home_id" => results.first["home_id"].to_i, + } end def self.setHome(person_id, home) @@ -162,9 +155,14 @@ class Person UPDATE people SET home_id = #{home.id} WHERE id = #{person_id} - RETURNING id, name, age; + RETURNING id, name, age, home_id; SQL ) - return Person.new(results.first) + return { + "id" => results.first["id"].to_i, + "name" => results.first["name"], + "age" => results.first["age"].to_i, + "home_id" => results.first["home_id"].to_i, + } end end