removing initialize from Location

master
Matt Huntington 8 years ago
parent 477f329600
commit 3320406b7f

@ -1,17 +1,6 @@
class Location class Location
attr_reader :id, :street, :city, :state, :inhabitants
DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts') 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
def self.all def self.all
results = DB.exec( results = DB.exec(
@ -31,13 +20,13 @@ class Location
results.each do |result| results.each do |result|
if result["id"] != last_location_id if result["id"] != last_location_id
locations.push( locations.push(
Location.new({ {
"id" => result["id"], "id" => result["id"].to_i,
"street" => result["street"], "street" => result["street"],
"city" => result["city"], "city" => result["city"],
"state" => result["state"], "state" => result["state"],
"inhabitants" => [] "inhabitants" => []
}) }
) )
last_location_id = result["id"] last_location_id = result["id"]
end end
@ -47,7 +36,7 @@ class Location
"name" => result["name"], "name" => result["name"],
"age" => result["age"].to_i, "age" => result["age"].to_i,
} }
locations.last.inhabitants.push(new_person) locations.last["inhabitants"].push(new_person)
end end
end end
return locations return locations
@ -78,13 +67,13 @@ class Location
end end
end end
return Location.new({ return {
"id" => results.first["id"], "id" => results.first["id"].to_i,
"street" => results.first["street"], "street" => results.first["street"],
"city" => results.first["city"], "city" => results.first["city"],
"state" => results.first["state"], "state" => results.first["state"],
"inhabitants" => inhabitants "inhabitants" => inhabitants
}) }
end end
def self.create(opts={}) def self.create(opts={})
@ -95,7 +84,12 @@ class Location
RETURNING id, street, city, state; RETURNING id, street, city, state;
SQL 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
def self.delete(id) def self.delete(id)
@ -112,6 +106,11 @@ class Location
RETURNING id, street, city, state; RETURNING id, street, city, state;
SQL 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
end end

@ -28,14 +28,12 @@ class Person
if result["id"] != last_person_id if result["id"] != last_person_id
last_person_id = result["id"] last_person_id = result["id"]
if result["home_id"] if result["home_id"]
home = Location.new( home = {
{
"id" => result["home_id"], "id" => result["home_id"],
"street" => result["street"], "street" => result["street"],
"city" => result["city"], "city" => result["city"],
"state" => result["state"], "state" => result["state"],
} }
)
end end
new_person = { new_person = {
"id" => result["id"], "id" => result["id"],
@ -81,14 +79,12 @@ class Person
) )
result = results.first result = results.first
if result["home_id"] if result["home_id"]
home = Location.new( home = {
{
"id" => result["home_id"], "id" => result["home_id"],
"street" => result["street"], "street" => result["street"],
"city" => result["city"], "city" => result["city"],
"state" => result["state"], "state" => result["state"],
} }
)
end end
employers = [] employers = []

Loading…
Cancel
Save