From 3320406b7fe3cf396b9a7a218787a1e3ed55937f Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Sun, 8 Jul 2018 19:21:20 -0400 Subject: [PATCH] removing initialize from Location --- app/models/location.rb | 41 ++++++++++++++++++++--------------------- app/models/person.rb | 28 ++++++++++++---------------- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/app/models/location.rb b/app/models/location.rb index 707a115..74ee6d3 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -1,17 +1,6 @@ class Location - attr_reader :id, :street, :city, :state, :inhabitants - DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts') - - def initialize(opts = {}) - @id = opts["id"].to_i - @street = opts["street"] - @city = opts["city"] - @state = opts["state"] - if opts["inhabitants"] - @inhabitants = opts["inhabitants"] - end - end + DB = PG.connect({:host => "localhost", :port => 5432, :dbname => 'contacts'}) def self.all results = DB.exec( @@ -31,13 +20,13 @@ class Location results.each do |result| if result["id"] != last_location_id locations.push( - Location.new({ - "id" => result["id"], + { + "id" => result["id"].to_i, "street" => result["street"], "city" => result["city"], "state" => result["state"], "inhabitants" => [] - }) + } ) last_location_id = result["id"] end @@ -47,7 +36,7 @@ class Location "name" => result["name"], "age" => result["age"].to_i, } - locations.last.inhabitants.push(new_person) + locations.last["inhabitants"].push(new_person) end end return locations @@ -78,13 +67,13 @@ class Location end end - return Location.new({ - "id" => results.first["id"], + return { + "id" => results.first["id"].to_i, "street" => results.first["street"], "city" => results.first["city"], "state" => results.first["state"], "inhabitants" => inhabitants - }) + } end def self.create(opts={}) @@ -95,7 +84,12 @@ class Location RETURNING id, street, city, state; SQL ) - return Location.new(results.first) + return { + "id" => results.first["id"], + "street" => results.first["street"], + "city" => results.first["city"], + "state" => results.first["state"] + } end def self.delete(id) @@ -112,6 +106,11 @@ class Location RETURNING id, street, city, state; SQL ) - return Location.new(results.first) + return { + "id" => results.first["id"], + "street" => results.first["street"], + "city" => results.first["city"], + "state" => results.first["state"] + } end end diff --git a/app/models/person.rb b/app/models/person.rb index eddfd27..e4a7c6a 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -28,14 +28,12 @@ class Person if result["id"] != last_person_id last_person_id = result["id"] if result["home_id"] - home = Location.new( - { - "id" => result["home_id"], - "street" => result["street"], - "city" => result["city"], - "state" => result["state"], - } - ) + home = { + "id" => result["home_id"], + "street" => result["street"], + "city" => result["city"], + "state" => result["state"], + } end new_person = { "id" => result["id"], @@ -81,14 +79,12 @@ class Person ) result = results.first if result["home_id"] - home = Location.new( - { - "id" => result["home_id"], - "street" => result["street"], - "city" => result["city"], - "state" => result["state"], - } - ) + home = { + "id" => result["home_id"], + "street" => result["street"], + "city" => result["city"], + "state" => result["state"], + } end employers = []